java+上傳大視頻文件斷點續傳


 上周遇到這樣一個問題,客戶上傳高清視頻(1G以上)的時候上傳失敗。
一開始以為是session過期或者文件大小受系統限制,導致的錯誤。
查看了系統的配置文件沒有看到文件大小限制,
web.xml中seesiontimeout是30,我把它改成了120。
但還是不行,有時候10分鍾就崩了。
同事說,可能是客戶這里服務器網絡波動導致網絡連接斷開,我覺得有點道理。
但是我在本地測試的時候發覺上傳也失敗,網絡原因排除。
看了日志,錯誤為:
java.lang.OutOfMemoryError Java heap space
上傳文件代碼如下:
    public static String uploadSingleFile(String path,MultipartFile file) {
        
        if (!file.isEmpty()) {
            
                byte[] bytes;
                try {
                    bytes = file.getBytes();
                    
                    // Create the file on server
                    File serverFile = createServerFile(path,file.getOriginalFilename());
                    BufferedOutputStream stream = new BufferedOutputStream(
                            new FileOutputStream(serverFile));
                    stream.write(bytes);
                    stream.flush();
                    stream.close();
    
                    logger.info("Server File Location="
                            + serverFile.getAbsolutePath());
 
                    return getRelativePathFromUploadDir(serverFile).replaceAll("\\\\", "/");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println(e.getMessage());
                }
            
        }else{
            System.out.println("文件內容為空");
        }
        return null;    
    }
乍一看沒什么大問題,我在 stream.write(bytes); 這句加了斷點,發覺根本就沒走到。
而是在 bytes = file.getBytes(); 就報錯了。
原因應該是文件太大的話,字節數超過Integer(Bytes[]數組)的最大值,導致的問題。
既然這樣,把文件一點點的讀進來即可。
修改上傳代碼如下:
 
   public static String uploadSingleFile(String path,MultipartFile file) {
        
        if (!file.isEmpty()) {
            
                //byte[] bytes;
                try {
                    //bytes = file.getBytes();
                    
                    // Create the file on server
                    File serverFile = createServerFile(path,file.getOriginalFilename());
                    BufferedOutputStream stream = new BufferedOutputStream(
                            new FileOutputStream(serverFile));
                    int length=0;
                    byte[] buffer = new byte[1024];
                    InputStream inputStream = file.getInputStream();
                    while ((length = inputStream.read(buffer)) != -1) {
                        stream.write(buffer, 0, length);
                    }
                    //stream.write(bytes);
                    stream.flush();
                    stream.close();
    
                    logger.info("Server File Location="
                            + serverFile.getAbsolutePath());
 
                    return getRelativePathFromUploadDir(serverFile).replaceAll("\\\\", "/");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println(e.getMessage());
                }
            
        }else{
            System.out.println("文件內容為空");
        }
        return null;    
    }
    
效果展示:




詳細代碼可以參考一下這篇文章:http://blog.ncmem.com/wordpress/2019/08/09/java%e4%b8%8a%e4%bc%a0%e5%a4%a7%e8%a7%86%e9%a2%91%e6%96%87%e4%bb%b6%e6%96%ad%e7%82%b9%e7%bb%ad%e4%bc%a0/


免責聲明!

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



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