如何将MultipartFile转换为字节流

时间:2013-08-06 10:03:26

标签: java spring hibernate

//或者将multipartfile保存到DB中的任何其他解决方案。 我试过这种方式,但收到错误。

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);

2 个答案:

答案 0 :(得分:26)

MultipartFile  file;
byte [] byteArr=file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);

答案 1 :(得分:-2)

    //Start Photo Upload with Adhaar No// 
    if (simpleLoanDto.getPic() != null && simpleLoanDto.getAdharNo() != null) {
        String ServerDirPath = globalVeriables.getAPath() + "\\";
        File ServerDir = new File(ServerDirPath);
        if (!ServerDir.exists()) {
            ServerDir.mkdirs();
        }
        // Giving File operation permission for LINUX//
        IOperation.setFileFolderPermission(ServerDirPath);
        MultipartFile originalPic = simpleLoanDto.getPic();
        byte[] ImageInByte = originalPic.getBytes();
        FileOutputStream fosFor = new FileOutputStream(
                new File(ServerDirPath + "\\" + simpleLoanDto.getAdharNo() + "_"+simpleLoanDto.getApplicantName()+"_.jpg"));
        fosFor.write(ImageInByte);
        fosFor.close();
    }
    //End  Photo Upload with Adhaar No//