java上傳超大文件


上周遇到這樣一個問題,客戶上傳高清視頻(1G以上)的時候上傳失敗。

一開始以為是session過期或者文件大小受系統限制,導致的錯誤。
查看了系統的配置文件沒有看到文件大小限制,
web.xml中seesiontimeout是30,我把它改成了120。
但還是不行,有時候10分鍾就崩了。

同事說,可能是客戶這里服務器網絡波動導致網絡連接斷開,我覺得有點道理。
但是我在本地測試的時候發覺上傳也失敗,網絡原因排除。

看了日志,錯誤為:
java.lang.OutOfMemoryError Java heap space
上傳文件代碼如下:


  
  
  
          
  1.      public static String uploadSingleFile(String path,MultipartFile file) {
  2.         
  3.          if (!file.isEmpty()) {
  4.             
  5.                  byte[] bytes;
  6.                  try {
  7.                     bytes = file.getBytes();
  8.                     
  9.                      // Create the file on server
  10.                     File serverFile = createServerFile(path,file.getOriginalFilename());
  11.                     BufferedOutputStream stream = new BufferedOutputStream(
  12.                              new FileOutputStream(serverFile));
  13.                     stream.write(bytes);
  14.                     stream.flush();
  15.                     stream.close();
  16.     
  17.                     logger.info( "Server File Location="
  18.                             + serverFile.getAbsolutePath());
  19.  
  20.                     return getRelativePathFromUploadDir(serverFile).replaceAll( "\\\\", "/");
  21.                 } catch (IOException e) {
  22.                      // TODO Auto-generated catch block
  23.                     e.printStackTrace();
  24.                     System. out.println(e.getMessage());
  25.                 }
  26.             
  27.         } else{
  28.             System. out.println( "文件內容為空");
  29.         }
  30.          return null;    
  31.     }

乍一看沒什么大問題,我在 stream.write(bytes); 這句加了斷點,發覺根本就沒走到。
而是在 bytes = file.getBytes(); 就報錯了。

原因應該是文件太大的話,字節數超過Integer(Bytes[]數組)的最大值,導致的問題。
既然這樣,把文件一點點的讀進來即可。

修改上傳代碼如下:

 


  
  
  
          
  1.     public static String uploadSingleFile(String path,MultipartFile file) {
  2.         
  3.          if (!file.isEmpty()) {
  4.             
  5.                  //byte[] bytes;
  6.                  try {
  7.                      //bytes = file.getBytes();
  8.                     
  9.                      // Create the file on server
  10.                     File serverFile = createServerFile(path,file.getOriginalFilename());
  11.                     BufferedOutputStream stream = new BufferedOutputStream(
  12.                              new FileOutputStream(serverFile));
  13.                      int length= 0;
  14.                      byte[] buffer = new byte[ 1024];
  15.                     InputStream inputStream = file.getInputStream();
  16.                      while ((length = inputStream.read(buffer)) != -1) {
  17.                         stream.write(buffer, 0, length);
  18.                     }
  19.                      //stream.write(bytes);
  20.                     stream.flush();
  21.                     stream.close();
  22.     
  23.                     logger.info( "Server File Location="
  24.                             + serverFile.getAbsolutePath());
  25.  
  26.                     return getRelativePathFromUploadDir(serverFile).replaceAll( "\\\\", "/");
  27.                 } catch (IOException e) {
  28.                      // TODO Auto-generated catch block
  29.                     e.printStackTrace();
  30.                     System. out.println(e.getMessage());
  31.                 }
  32.             
  33.         } else{
  34.             System. out.println( "文件內容為空");
  35.         }
  36.          return null;    
  37.     }

 

效果展示:

 說明: http://bbsres2.ncmem.com/731fda07.png 

Mac控件安裝教程與演示說明:

http://t.cn/AijgiFgW

http://t.cn/Aijg6z08

 

Linux控件安裝教程與演示說明:

http://t.cn/Aijg6Lv3

http://t.cn/Aijg64k6

 

控件包下載:

MacOShttp://t.cn/Aijg65dZ

Linuxhttp://t.cn/Aijg6fRV

cab(x86)http://t.cn/Ai9pmG8S 

cab(x64)http://t.cn/Ai9pm04B 

xpihttp://t.cn/Ai9pubUc 

crxhttp://t.cn/Ai9pmrcy 

exehttp://t.cn/Ai9puobe   

 

示例下載: 

jsp-eclipsehttp://t.cn/Ai9p3LSx 

jsp-myeclipsehttp://t.cn/Ai9p3IdC    

 

在線教程: 

jsp-文件管理器教程:http://t.cn/AiNhmilv

 

個人博客:http://t.cn/AiY7heL0

 

www.webuploader.net

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM