I have to upload some files from jsp page, when i read the inputstream for the same.
I am checking for its authenticity that its a valid file or not, for that I am using jmimemagic
and it takes input stream as argument and now when I am trying to upload it, its only uploading 1 byte of data?
I feel like there is some issue with inputstream any solution pls?
//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();
}
In
byte[] fileArray = IOUtils.toByteArray(loIns);
you've already exhausted your inputstream, so when you want to write it to a file, you should use the content of the fileArray
, not your loIns
:
FileUtils.writeByteArrayToFile(loOutput, fileArray);
FileUtils is provided by apache-commons