springMVC学习之路2-文件上传

时间:2021-04-22 21:03:16

springMVC的上传文件方式一共有两种,下面听我简单介绍一下:

1.1 首先配置springAnnotation-servlet.xml,新增一个multipartResolver处理器,并定义默认编码为utf-8,最大上传大小 和 最大内存大小

     <!-- 文件上传 -->
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
           <property name="defaultEncoding" value="utf-8" />
           <property name=" />
           <property name=" />
     </bean>

1.2 创建上传文件的jsp页面

     <form name="userForm" action="/SpringMVC6/file/upload2" method="post"
         enctype="multipart/form-data">
         选择文件:<input type="file" name="file">
         <input type="submit" value="上传">
     </form>

1.3 创建一个java类

 package com.juin.upload;

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Iterator;
 import javax.servlet.http.HttpServletRequest;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
 import org.springframework.web.multipart.commons.CommonsMultipartResolver;

 @Controller
 @RequestMapping("/file")
 public class uploadController {

     @RequestMapping("/upload")
     // 效率比较低
     public String addUser(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request)
             throws IOException {
         System.out.println("文件名为: " + file.getOriginalFilename());
         if (!file.isEmpty()) {
             try {
                 FileOutputStream os = new FileOutputStream("F:/upload/" + file.getOriginalFilename());
                 InputStream in = file.getInputStream();
                 int b = 0;
                 while ((b = in.read()) != -1) {
                     os.write(b);
                 }
                 os.flush();
                 os.close();
                 in.close();
             } catch (FileNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         }
         return "/success";
     }

     //效率较高的上传方法
     @RequestMapping("/upload2")
     public String upload2(HttpServletRequest request) throws IllegalStateException, IOException{
         CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
         if(commonsMultipartResolver.isMultipart(request)){
             //是否为Multipart类型
             MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)(request);
             Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
             while(iterator.hasNext()){
                 MultipartFile file = multipartHttpServletRequest.getFile(iterator.next());
                 if(file!=null){
                     String fileName = file.getOriginalFilename();
                     String path = "F:/upload/"+fileName;
                     File localFile = new File(path);
                     file.transferTo(localFile);
                 }
             }
         }
         return "/success";
     }

     @RequestMapping("/toUpload")
     public String toUpload() {
         return "/upload";
     }
 }