Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert MultipartFile into byte stream

//Or any other solution to saving multipartfile into DB. I tried with this way but getting error.

File fileOne = new File("file.getOrignalFileName");//what should be kept inside this method 
byte[] bFile = new byte[(int) fileOne.length()];
try {
    FileInputStream fileInputStream = new FileInputStream(fileOne);
    //convert file into array of bytes
    fileInputStream.read(bFile);
    fileInputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}
questionDao.saveImage(bFile);
like image 501
Ankit Tater Avatar asked Aug 06 '13 10:08

Ankit Tater


People also ask

What is MultipartFile?

A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired.

How do you create a multipart file from a byte array?

Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example: FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile. getName(), 100000000, new java.


1 Answers

MultipartFile  file;
byte [] byteArr=file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);
like image 140
Sanjaya Liyanage Avatar answered Sep 19 '22 18:09

Sanjaya Liyanage