spring mvc 上傳文件

一、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文件:java

   <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文件:web

@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";

   }

}

運行時間:spring

fileName:java_ee_javadocs.zipsession

方法一的運行時間:128msmvc

第二種方法:採用multipart提供的file.transfer方法上傳文件

jsp文件:app

   <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文件:jsp

@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";

   }

}

運行時間:post

fileName:java_ee_javadocs.zip編碼

方法二的運行時間:1msspa

第三種方法:使用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

 

從上面運行的狀況來看,最後採用後兩種方法。

相關文章
相關標籤/搜索