spring mvc 上传文件

1、spring-servlet.xml配置:

    <!-- 处理文件上传 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="uft-8" /> <!-- 默认编码 (ISO-8859-1) -->
        <property name="maxInMemorySize" value="10240" /> <!-- 最大内存大小 (10240)-->
        <property name="uploadTempDir" value="/upload/" /> <!-- 上传后的目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
        <property name="maxUploadSize" value="-1" /> <!-- 最大文件大小,-1为无限止(-1) -->
    </bean>

 第一种方法:采用流的方式上传文件

jsp文件:

   <form name="Form1" action="/spmvc/upload/file1" method="post"

      enctype="multipart/form-data">

      <h3>采用流的方式上传文件</h3>

      <input type="file" name="file">

      <input type="submit" value="upload" />

   </form>

java文件:

@Controller

@RequestMapping("upload")

public class FileUploadController6 {

   //采用流的方式上传文件

   @RequestMapping("file1")

   public String file1(@RequestParam("file")CommonsMultipartFile file){

       //用来检测程序运行时间

        long  startTime=System.currentTimeMillis();

        System.out.println("fileName:"+file.getOriginalFilename()); 

      String name = file.getOriginalFilename();

      if(!file.isEmpty()){

         try {

            String path = "D:/"+new Date().getTime()+"-"+name;

            FileOutputStream  fos = new FileOutputStream(new File(path));

            InputStream is = file.getInputStream();

            int temp;

            byte[] b = new byte[1024];

            while((temp = is.read(b))!=-1){

                fos.write(b);

            }

            fos.flush();

            fos.close();

            is.close();

            long  endTime=System.currentTimeMillis();

            System.out.println("方法一的运行时间:"+String.valueOf(endTime-startTime)+"ms");

           

            return "/6-upload/upload-success";

         } catch (FileNotFoundException e) {

            e.printStackTrace();

         } catch (IOException e) {

            e.printStackTrace();

         }

      }

      return "/6-upload/upload-error";

   }

}

运行时间:

fileName:java_ee_javadocs.zip

方法一的运行时间:128ms

第二种方法:采用multipart提供的file.transfer方法上传文件

jsp文件:

   <form name="Form2" action="/spmvc/upload/file2" method="post"

      enctype="multipart/form-data">

      <h3>采用multipart提供的file.transfer方法上传文件</h3>

      <input type="file" name="file">

      <input type="submit" value="upload" />

   </form>

java文件:

@Controller

@RequestMapping("upload")

public class FileUploadController6 {

   //采用multipart提供的file.transfer方法上传文件

   @RequestMapping("file2")

   public String file2(@RequestParam("file") CommonsMultipartFile file){

      long startTime = System.currentTimeMillis();

      System.out.println("fileName:"+file.getOriginalFilename());

      String path = "D:/"+new Date().getTime()+"-"+file.getOriginalFilename();

      File newFile = new File(path);

      try {

         file.transferTo(newFile);

         long endTime = System.currentTimeMillis();

         System.out.println("方法二的运行时间:"+String.valueOf(endTime-startTime)+"ms");        

         return "/6-upload/upload-success";

      } catch (IllegalStateException e) {

         e.printStackTrace();

      } catch (IOException e) {

         e.printStackTrace();

      }    

      return "/6-upload/upload-error";

   }

}

运行时间:

fileName:java_ee_javadocs.zip

方法二的运行时间:1ms

第三种方法:使用spring mvc提供的类的方法上传文件

jsp文件:

   <form name="Form3" action="/spmvc/upload/file3" method="post"

      enctype="multipart/form-data">

      <h3>使用spring mvc提供的类的方法上传文件</h3>

      <input type="file" name="file">

      <input type="submit" value="upload" />

   </form>

java文件:

@Controller

@RequestMapping("upload")

public class FileUploadController6 {

   //使用spring mvc提供的类的方法上传文件

   @RequestMapping("file3")

   public String file3(HttpServletRequest request){

      long startTime = System.currentTimeMillis();

      //将当前上下文初始化给  CommonsMutipartResolver (多部分解析器)

//    ServletContext c1 = request.getSession().getServletContext();

//    ServletContext c2 = request.getServletContext();

//    System.out.println(c1==c2);//从request和session中获取到的servletContext是同一个对象

      CommonsMultipartResolver  multipartResolver = new CommonsMultipartResolver(request.getServletContext());

       //检查form中是否有enctype="multipart/form-data"

      if(multipartResolver.isMultipart(request)){

         MultipartHttpServletRequest  multipartRequest = (MultipartHttpServletRequest)request;

         Iterator<String> iterator = multipartRequest.getFileNames();

         while(iterator.hasNext()){

            MultipartFile  file  = multipartRequest.getFile(iterator.next());

            String path = "D:/"+new Date().getTime()+"-"+file.getOriginalFilename();

            try {

                file.transferTo(new File(path));

            } catch (IllegalStateException e) {

                e.printStackTrace();

                return "/6-upload/upload-error";

            } catch (IOException e) {

                e.printStackTrace();

                return "/6-upload/upload-error";

            }

         }

      }

      long endTime = System.currentTimeMillis();

      System.out.println("方法三的运行时间:"+String.valueOf(endTime-startTime)+"ms");

      return "/6-upload/upload-success";

   }

}

运行时间:

fileName:java_ee_javadocs.zip

方法三的运行时间:1ms

 一次上传多个文件

jsp文件:

   <form name="Form4" action="/spmvc/upload/file3" method="post"

      enctype="multipart/form-data">

      <h3>使用spring mvc提供的类的方法上传多个文件</h3>

      <input type="file" name="file1" ><br/>

      <input type="file" name="file2" ><br/>

      <input type="file" name="file3" ><br/>

      <input type="file" name="file4" ><br/>

      <input type="submit" value="upload" />

   </form> 

java文件:

@Controller

@RequestMapping("upload")

public class FileUploadController6 {

   //使用spring mvc提供的类的方法上传文件

   @RequestMapping("file3")

   public String file3(HttpServletRequest request){

      long startTime = System.currentTimeMillis();

      //将当前上下文初始化给  CommonsMutipartResolver (多部分解析器)

//    ServletContext c1 = request.getSession().getServletContext();

//    ServletContext c2 = request.getServletContext();

//    System.out.println(c1==c2);//从request和session中获取到的servletContext是同一个对象

      CommonsMultipartResolver  multipartResolver = new CommonsMultipartResolver(request.getServletContext());

       //检查form中是否有enctype="multipart/form-data"

      if(multipartResolver.isMultipart(request)){

         MultipartHttpServletRequest  multipartRequest = (MultipartHttpServletRequest)request;

         Iterator<String> iterator = multipartRequest.getFileNames();

         while(iterator.hasNext()){

            MultipartFile  file  = multipartRequest.getFile(iterator.next());

            String path = "D:/"+new Date().getTime()+"-"+file.getOriginalFilename();

            try {

                file.transferTo(new File(path));

            } catch (IllegalStateException e) {

                e.printStackTrace();

                return "/6-upload/upload-error";

            } catch (IOException e) {

                e.printStackTrace();

                return "/6-upload/upload-error";

            }

         }

      }

      long endTime = System.currentTimeMillis();

      System.out.println("方法三的运行时间:"+String.valueOf(endTime-startTime)+"ms");

      return "/6-upload/upload-success";

   }

}

运行时间:

fileName:java_ee_javadocs.zip

方法三的运行时间:1ms

 

从上面运行的情况来看,最后采用后两种方法。

 

posted @ 2014-10-23 15:50  勿妄  阅读(1007)  评论(0编辑  收藏  举报