zoukankan      html  css  js  c++  java
  • Spring中MultipartHttpServletRequest实现文件上传 生成缩略图

    转贴自:http://my.oschina.net/nyniuch/blog/185266

    实现图片上传 
      用户必须能够上传图片,因此需要文件上传的功能。比较常见的文件上传组件有Commons FileUpload(http://jakarta.apache.org/commons/fileupload/a>)和COS FileUpload(http://www.servlets.com/cos),Spring已经完全集成了这两种组件,这里我们选择Commons FileUpload。 
      由于Post一个包含文件上传的Form会以multipart/form-data请求发送给服务器,必须明确告诉DispatcherServlet如何处理MultipartRequest。首先在dispatcher-servlet.xml中声明一个MultipartResolver:

    xml 代码
    1. <bean id="multipartResolver"  
    2.     class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    3.     <!-- 设置上传文件的最大尺寸为1MB -->  
    4.     <property name="maxUploadSize">  
    5.         <value>1048576</value>  
    6.     </property>  
    7. </bean>  

     这样一旦某个Request是一个MultipartRequest,它就会首先被MultipartResolver处理,然后再转发相应的Controller。 
    在UploadImageController中,将HttpServletRequest转型为MultipartHttpServletRequest,就能非常方便地得到文件名和文件内容:

    java 代码
    1. public ModelAndView handleRequest(HttpServletRequest request,   
    2.             HttpServletResponse response) throws Exception {   
    3.         // 转型为MultipartHttpRequest:   
    4.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;   
    5.         // 获得文件:   
    6.         MultipartFile file = multipartRequest.getFile(" file ");   
    7.         // 获得文件名:   
    8.         String filename = file.getOriginalFilename();   
    9.         // 获得输入流:   
    10.         InputStream input = file.getInputStream();   
    11.         // 写入文件   
    12.   
    13.         // 或者:   
    14.         File source = new File(localfileName.toString());   
    15.         multipartFile.transferTo(source);   
    16.     }  

    生成缩略图 (目录)
      当用户上传了图片后,必须生成缩略图以便用户能快速浏览。我们不需借助第三方软件,JDK标准库就包含了图像处理的API。我们把一张图片按比例缩放到120X120大小,以下是关键代码:

    java 代码
    1. public static void createPreviewImage(String srcFile, String destFile) {   
    2.         try {   
    3.             File fi = new File(srcFile); // src   
    4.             File fo = new File(destFile); // dest   
    5.             BufferedImage bis = ImageIO.read(fi);   
    6.   
    7.             int w = bis.getWidth();   
    8.             int h = bis.getHeight();   
    9.             double scale = (double) w / h;   
    10.             int nw = IMAGE_SIZE; // final int IMAGE_SIZE = 120;   
    11.             int nh = (nw * h) / w;   
    12.             if (nh > IMAGE_SIZE) {   
    13.                 nh = IMAGE_SIZE;   
    14.                 nw = (nh * w) / h;   
    15.             }   
    16.             double sx = (double) nw / w;   
    17.             double sy = (double) nh / h;   
    18.   
    19.             transform.setToScale(sx, sy);   
    20.             AffineTransformOp ato = new AffineTransformOp(transform, null);   
    21.             BufferedImage bid = new BufferedImage(nw, nh,   
    22.                     BufferedImage.TYPE_3BYTE_BGR);   
    23.             ato.filter(bis, bid);   
    24.             ImageIO.write(bid, " jpeg ", fo);   
    25.         } catch (Exception e) {   
    26.             e.printStackTrace();   
    27.             throw new RuntimeException(   
    28.                     " Failed in create preview image. Error:  "  
    29.                             + e.getMessage());   
    30.         }   
    31.     }  
     
     
  • 相关阅读:
    51nod 1087 1 10 100 1000(找规律+递推+stl)
    51nod 1082 与7无关的数 (打表预处理)
    51 nod 1080 两个数的平方和
    1015 水仙花数(水题)
    51 nod 1003 阶乘后面0的数量
    51nod 1002 数塔取数问题
    51 nod 1001 数组中和等于K的数对
    51 nod 1081 子段求和
    51nod 1134 最长递增子序列 (O(nlogn)算法)
    51nod 1174 区间中最大的数(RMQ)
  • 原文地址:https://www.cnblogs.com/yhtboke/p/5719116.html
Copyright © 2011-2022 走看看