JAVA Server上传文件 Spring MultipartResolver 或者 ServletFileUpload

转载自http://blog.csdn.net/cctt_1/article/details/8800964

如果想上传文件,那么有两种方法可以解决。一种使用Spring框架中的东西。另外一种是使用原生的代码。

使用Spring框架非常简单。将如下xml放入到servlet.xml中。

  1. <bean id=“multipartResolver”  
  2.         class=“org.springframework.web.multipart.commons.CommonsMultipartResolver”>  
  3.         <property name=“maxUploadSize”  
  4.             value=“20000000” />  
  5.    <!– 上传限制 20M –>  

  6. 然后在代码中这样写:

    1. @RequestMapping(value = “upload”, method = RequestMethod.POST)  
    2.     public void upload(HttpServletRequest req, HttpServletResponse response,  
    3.             @RequestParam(value=“file”, required=true) MultipartFile file,  
    4.             )  
    5.             throws IOException {  
    6. // 1. upload file, 获取bytes内容  
    7.   
    8.        ByteArrayOutputStream bytes = new ByteArrayOutputStream();  
    9.         IOUtils.copy(file.getInputStream(), bytes);  
    10.         byte[] byteArray = bytes.toByteArray();}  
    @RequestMapping(value = "upload", method = RequestMethod.POST)
        public void upload(HttpServletRequest req, HttpServletResponse response,
                @RequestParam(value="file", required=true) MultipartFile file,
                )
                throws IOException {
    // 1. upload file, 获取bytes内容
    
           ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            IOUtils.copy(file.getInputStream(), bytes);
            byte[] byteArray = bytes.toByteArray();}
    
    
    Html上传页面这样写:
    1. <form name=“uploadForm” action=“upload” method=“POST” enctype=“multipart/form-data” >  
    2.           
    3.         <label>上传文件 </label><br/>  
    4.         <input type=“file” name=“file” size=“100” style=“width:300px;” /> <br />  
    5.           
    6.         <input type=“submit” name=“submit” value=“submit”/>  
    7.     </form>  

    这样写完后,就可以上传一个不超过20M的文件了。


    第二种方式不使用Spring框架。代码稍微复杂一些。HTML同上。但是没有那段XML内容。JAVA代码需要改为:

    1. @RequestMapping(value = “upload”, method = RequestMethod.POST)  
    2.     public void upload(HttpServletRequest req, HttpServletResponse resp  
    3.             ) throws ServletException, IOException, FileUploadException,  
    4.             {  
    5.         boolean isMultipart = ServletFileUpload.isMultipartContent(req);  
    6.         HashMap<String, String> request = new HashMap<String, String>();  
    7.         if (isMultipart) {  
    8.             DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024 * 20null);  
    9.             ServletFileUpload upload = new ServletFileUpload(factory);  
    10.             upload.setHeaderEncoding(”UTF-8”);  
    11.             upload.setSizeMax(1024 * 1024 * 20);  
    12.   
    13.             List<FileItem> fileItems = upload.parseRequest(req);  
    14.             Iterator<FileItem> iter = fileItems.iterator();  
    15.             while (iter.hasNext()) {  
    16.                 FileItem item = (FileItem) iter.next();  
    17.                 if (item.isFormField()) {  
    18.                     String name = item.getFieldName();  
    19.                     String value = item.getString(”UTF-8”);  
    20.                     request.put(name, value);  
    21.                 } else {  
    22.                     byte[] filebytes = item.get();  
    23.                     if (StringUtils.isBlank(item.getName())) {  
    24.                         continue;  
    25.                     }  
    26.                       
    27.                     request.put(item.getFieldName(), new String(filebyte, “utf-8”));  
    28.                 }  
    29.             }  
    30.         }  
    31. }  

    但是这两者不能同时使用,否则会有冲突。下一篇将介绍 如何解决两者之间的冲突





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值