Java大文件上傳詳解及實例代碼
Java大文件上傳詳解
前言:
上周遇到這樣一個問題,客戶上傳高清視頻(1G以上)的時候上傳失敗。
一開始以為是session過期或者文件大小受系統(tǒng)限制,導(dǎo)致的錯誤。查看了系統(tǒng)的配置文件沒有看到文件大小限制,web.xml中seesiontimeout是30,我把它改成了120。但還是不行,有時候10分鐘就崩了。
同事說,可能是客戶這里服務(wù)器網(wǎng)絡(luò)波動導(dǎo)致網(wǎng)絡(luò)連接斷開,我覺得有點道理。但是我在本地測試的時候發(fā)覺上傳也失敗,網(wǎng)絡(luò)原因排除。
看了日志,錯誤為:
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("文件內(nèi)容為空"); } return null; }
乍一看沒什么大問題,我在 stream.write(bytes); 這句加了斷點,發(fā)覺根本就沒走到。而是在 bytes = file.getBytes(); 就報錯了。
原因應(yīng)該是文件太大的話,字節(jié)數(shù)超過Integer(Bytes[]數(shù)組)的最大值,導(dǎo)致的問題。
既然這樣,把文件一點點的讀進來即可。
修改上傳代碼如下:
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("文件內(nèi)容為空"); } return null; }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
java導(dǎo)出到excel常用的幾種方式總結(jié)
導(dǎo)出excel是咱Java開發(fā)的必備技能啦,之前項目有這個功能,現(xiàn)在將其獨立出來,分享一下,下面這篇文章主要給大家介紹了關(guān)于java導(dǎo)出到excel常用的幾種方式,需要的朋友可以參考下2023-05-05Springboot應(yīng)用中線程池配置詳細教程(最新2021版)
這篇文章主要介紹了Springboot應(yīng)用中線程池配置教程(2021版),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Spring中@RequestMapping、@RestController和Postman
本文介紹了Spring框架中常用的@RequestMapping和@RestController注解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10Java?web訪問http://localhost:8080/xx/xx.jsp報404錯誤問題的解決方法
這篇文章主要給大家介紹了關(guān)于Java?web訪問http://localhost:8080/xx/xx.jsp報404錯誤問題的解決方法,很多小伙伴在剛開始用Springboot整合jsp開發(fā)時都會遇到這個問題, 按照別人的教程一步一步搭建,但就是會報404,文中介紹的非常詳細,需要的朋友可以參考下2023-04-04使用Feign調(diào)用注解組件(實現(xiàn)字段賦值功能)
這篇文章主要介紹了使用Feign調(diào)用注解組件(實現(xiàn)字段賦值功能),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03