简体   繁体   English

如何将MultipartFile转换为字节流

[英]How to convert MultipartFile into byte stream

//Or any other solution to saving multipartfile into DB. //或者将多部分文件保存到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);
MultipartFile  file;
byte [] byteArr=file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);
    //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// 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM