0

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();
            }
2
  • 1
    Can you please provide us with the code that you are using so that we can have a look? It is hard to diagnose without any error message or code. Commented Apr 17, 2012 at 9:51
  • in both the cases same stream is been used to perform actions Commented Apr 17, 2012 at 10:14

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.