Java大文件上傳詳解及實(shí)例代碼
Java大文件上傳詳解
前言:
上周遇到這樣一個(gè)問(wèn)題,客戶上傳高清視頻(1G以上)的時(shí)候上傳失敗。
一開始以為是session過(guò)期或者文件大小受系統(tǒng)限制,導(dǎo)致的錯(cuò)誤。查看了系統(tǒng)的配置文件沒(méi)有看到文件大小限制,web.xml中seesiontimeout是30,我把它改成了120。但還是不行,有時(shí)候10分鐘就崩了。
同事說(shuō),可能是客戶這里服務(wù)器網(wǎng)絡(luò)波動(dòng)導(dǎo)致網(wǎng)絡(luò)連接斷開,我覺(jué)得有點(diǎn)道理。但是我在本地測(cè)試的時(shí)候發(fā)覺(jué)上傳也失敗,網(wǎng)絡(luò)原因排除。
看了日志,錯(cuò)誤為:
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;
}
乍一看沒(méi)什么大問(wèn)題,我在 stream.write(bytes); 這句加了斷點(diǎn),發(fā)覺(jué)根本就沒(méi)走到。而是在 bytes = file.getBytes(); 就報(bào)錯(cuò)了。
原因應(yīng)該是文件太大的話,字節(jié)數(shù)超過(guò)Integer(Bytes[]數(shù)組)的最大值,導(dǎo)致的問(wèn)題。
既然這樣,把文件一點(diǎn)點(diǎn)的讀進(jìn)來(lái)即可。
修改上傳代碼如下:
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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
利用Java編寫24點(diǎn)小游戲的實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于如何利用Java編寫24點(diǎn)小游戲的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
windows下vscode+vs2019開發(fā)JNI的示例
本文給大家普及windows下vscode+vs2019開發(fā)JNI的示例以及各個(gè)環(huán)節(jié)的注意事項(xiàng),文章通過(guò)示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-06-06
基于java中的null類型---有關(guān)null的9件事
這篇文章主要介紹了java中的null類型---有關(guān)null的9件事,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil
今天小編就為大家分享一篇關(guān)于兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
java導(dǎo)出數(shù)據(jù)庫(kù)的全部表到excel
這篇文章主要為大家詳細(xì)介紹了java導(dǎo)出數(shù)據(jù)庫(kù)的全部表到excel的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
Java正則驗(yàn)證IP的方法實(shí)例分析【測(cè)試可用】
這篇文章主要介紹了Java正則驗(yàn)證IP的方法,結(jié)合實(shí)例形式對(duì)比分析了網(wǎng)上常見的幾種針對(duì)IP的正則驗(yàn)證方法,最終給出了一個(gè)比較靠譜的IP正則驗(yàn)證表達(dá)式,需要的朋友可以參考下2017-08-08

