[英]Java file upload - inputstream issue
當我讀取輸入流時,我必須從jsp頁面上傳一些文件。 我正在檢查它的真實性,它是否是一個有效的文件,因為我正在使用jmimemagic
並且它將輸入流作為參數,現在當我嘗試上傳它時,它只上傳1個字節的數據?
我覺得任何解決方案都有輸入流的問題嗎?
//For checking the file type
InputStream loIns = aoFileStream.getInputStreamToReadFile();
byte[] fileArray = IOUtils.toByteArray(loIns);
mimeType = Magic.getMagicMatch(fileArray, true).getMimeType();
if(!loAllowedFileTypesMimeList.contains(mimeType)){
return false;
}else{
String lsFileName = aoFileStream.getFileName();
String lsFileExt = lsFileName.substring(lsFileName.lastIndexOf(".") + 1);
if(loAllowedFileTypesList.contains(lsFileExt.toLowerCase())){
return true;
}
return false;
}
// For uploading the content
File loOutputFile = new File(asFilePathToUpload);
if(!loOutputFile.exists()){
FileOutputStream loOutput = new FileOutputStream(loOutputFile);
while (liEnd != -1) {
liEnd = inputStreamToReadFile.read();
loOutput.write(liEnd);
}
inputStreamToReadFile.close();
loOutput.close();
}
在
byte[] fileArray = IOUtils.toByteArray(loIns);
你已經耗盡了你的輸入流,所以當你想把它寫入文件時,你應該使用fileArray
的內容,而不是你的loIns
:
FileUtils.writeByteArrayToFile(loOutput, fileArray);
FileUtils由apache-commons提供
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.