参考文章:
http://www.cnblogs.com/interdrp/p/6734033.html
方法一:
1)没有配置org.springframework.web.multipart.commons.CommonsMultipartResolver
2)MultipartFile转化为File的方式为:
MultipartFile file = multiRequest.getFile("imgFile");
CommonsMultipartFile cf= (CommonsMultipartFile) file;
DiskFileItem fi = (DiskFileItem ) cf.getFileItem();
File fs = fi.getStoreLocation();
代码例子1:保存用户信息,用户有照片
//增加用户 @RequestMapping("/addUser") @ResponseBody public String addUser(HttpServletRequest request, HttpServletResponse response){ String username = ""; int age = 0; String photoName = ""; byte[] photo = null; //创建一个通用的多部分解析器. CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //判断 request 是否有文件上传,即多部分请求... if(multipartResolver.isMultipart(request)){ //转换成多部分request MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request); //获取普通表单的属性 username = multiRequest.getParameter("username"); String ageStr = multiRequest.getParameter("age"); if(ageStr!=null){ try{ age = Integer.parseInt(ageStr); }catch(Exception e){ e.printStackTrace(); } } //取得request中的所有文件名 Iterator<String> iter = multiRequest.getFileNames(); while(iter.hasNext()){ //取得上传文件 MultipartFile file = multiRequest.getFile(iter.next()); if(file != null){ //取得当前上传文件的文件名称 photoName = file.getOriginalFilename(); try { InputStream inputStream = file.getInputStream(); photo = new byte[inputStream.available()]; inputStream.read(photo); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } User user = new User(username, age, photoName, photo); int count = userService.insertUser(user); if(count > 0){ return "success"; }else{ return "failed"; } }
代码例子2:上传压缩包zip或者rar;这里是单包上传(single),每个zip/rar下面直接是一个一个的jpg图片;
//上传图片zip/rar文件 -- @RequestMapping("/uploadImageFile") @ResponseBody public String uploadImageFile(HttpServletRequest request, HttpServletResponse response){ CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); if(multipartResolver.isMultipart(request)){ MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request); String packageType = multiRequest.getParameter("packageType"); if(packageType.equals("single")){ //单包 MultipartFile file = multiRequest.getFile("imgFile"); if(file != null){ String packageName = file.getOriginalFilename(); //上传的包名 String iconName = productService.getIconNameByPackage(packageName); //获取该包下面的缩略图名称 if(packageName.matches(".*\.zip")){ //是zip压缩文件 try{ ZipInputStream zs = new ZipInputStream(file.getInputStream()); BufferedInputStream bs = new BufferedInputStream(zs); ZipEntry ze; byte[] picture = null; while((ze = zs.getNextEntry()) != null){ //获取zip包中的每一个zip file entry String fileName = ze.getName(); //pictureName picture = new byte[(int) ze.getSize()]; //读一个文件大小 bs.read(picture, 0, (int) ze.getSize()); ImageFile image = new ImageFile(packageName, "N", fileName, picture); //保存image,非缩略图 productService.insertImage(image); if(fileName.equals(iconName)){ //这个picture要作为缩略图,进行裁剪、保存 String thumbName = ImageUtil.createThumbFileName(fileName); //生成缩略图名称 InputStream is = new ByteArrayInputStream(picture); byte[] thumbPic = ImageUtil.clipImage(is, 100, 100, "jpg"); //进行裁剪 ImageFile thumbImage = new ImageFile(packageName, "Y", thumbName, thumbPic); productService.insertImage(thumbImage); } } bs.close(); zs.close(); return "success"; }catch(IOException e){ e.printStackTrace(); } }else if(packageName.matches(".*\.rar")){ //是rar压缩文件 try { //MultipartFile file 转化为File 有临时文件产生: CommonsMultipartFile cf= (CommonsMultipartFile) file; DiskFileItem fi = (DiskFileItem ) cf.getFileItem(); File fs = fi.getStoreLocation(); Archive archive = new Archive(fs); ByteArrayOutputStream bos = null; byte[] picture = null; FileHeader fh = archive.nextFileHeader(); while(fh!=null){ String fileName = fh.getFileNameString(); bos = new ByteArrayOutputStream(); archive.extractFile(fh, bos); picture = bos.toByteArray(); ImageFile image = new ImageFile(packageName, "N", fileName, picture); //保存image,非缩略图 productService.insertImage(image); fh = archive.nextFileHeader(); } bos.close(); archive.close(); return "success"; } catch (RarException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } } return "failed"; }
方法二:配置org.springframework.web.multipart.commons.CommonsMultipartResolver
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="10485760000"></property> <property name="maxInMemorySize" value="40960"></property> </bean>
代码例子:
//上传文件zip/rar 方法二 配置org.springframework.web.multipart.commons.CommonsMultipartResolver @RequestMapping("/uploadImageFile2") @ResponseBody public String uploadImageFile2(HttpServletRequest request, HttpServletResponse response){ MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; String packageType = multiRequest.getParameter("packageType"); if(packageType.equals("single")){ //单包 MultipartFile file = multiRequest.getFile("imgFile"); if(file != null){ String packageName = file.getOriginalFilename(); //上传的包名 String iconName = productService.getIconNameByPackage(packageName); //获取该包下面的缩略图名称 if(packageName.matches(".*\.zip")){ //是zip压缩文件 try{ ZipInputStream zs = new ZipInputStream(file.getInputStream()); BufferedInputStream bs = new BufferedInputStream(zs); ZipEntry ze; byte[] picture = null; while((ze = zs.getNextEntry()) != null){ //获取zip包中的每一个zip file entry String fileName = ze.getName(); //pictureName picture = new byte[(int) ze.getSize()]; //读一个文件大小 bs.read(picture, 0, (int) ze.getSize()); ImageFile image = new ImageFile(packageName, "N", fileName, picture); //保存image,非缩略图 productService.insertImage(image); if(fileName.equals(iconName)){ //这个picture要作为缩略图,进行裁剪、保存 String thumbName = ImageUtil.createThumbFileName(fileName); //生成缩略图名称 InputStream is = new ByteArrayInputStream(picture); byte[] thumbPic = ImageUtil.clipImage(is, 100, 100, "jpg"); //进行裁剪 ImageFile thumbImage = new ImageFile(packageName, "Y", thumbName, thumbPic); productService.insertImage(thumbImage); } } bs.close(); zs.close(); return "success"; }catch(IOException e){ e.printStackTrace(); } }else if(packageName.matches(".*\.rar")){ //是rar压缩文件 //....和上面一样。 } } } return "failed"; }
最后补充上面用到的工具类:ImageUtil.java(里面有对图片裁剪的方法)
package com.cy.util; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; public class ImageUtil { /** * 判断压缩文件的类型 */ /** * 生成缩略图名称 * @param srcFileName * @return */ public static String createThumbFileName(String srcFileName){ StringBuffer thumbFileName = new StringBuffer(); int pos = srcFileName.lastIndexOf("."); thumbFileName.append(srcFileName.substring(0, pos)); thumbFileName.append("_small"); thumbFileName.append(srcFileName.substring(pos, srcFileName.length())); return thumbFileName.toString(); } /** * 对图片进行剪裁 * @param is 图片输入流 * @param width 裁剪图片的宽 * @param height 裁剪图片的高 * @param imageFormat 输出图片的格式 * @return */ public static byte[] clipImage(InputStream is,int width, int height, String imageFormat){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { // 构造Image对象 BufferedImage src = javax.imageio.ImageIO.read(is); // 缩小边长 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 绘制 缩小 后的图片 tag.getGraphics().drawImage(src, 0, 0, width, height, null); ImageIO.write(tag, imageFormat, bos); } catch (IOException e) { e.printStackTrace(); }finally{ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return bos.toByteArray(); } }
------------