【问题标题】:How to convert MultipartFile into byte stream如何将 MultipartFile 转换为字节流
【发布时间】:2013-08-06 10:03:26
【问题描述】:

//或任何其他将多部分文件保存到数据库的解决方案。 我尝试过这种方式,但出现错误。

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

【问题讨论】:

  • 你遇到了什么错误?
  • 您遇到什么错误?它是如何保存在数据库中的(是否使用 ORM 映射?)正在使用哪种 Multipart 文件(MIME、JMS、..)?
  • 我正在尝试使用 ORM 映射来获取图像文件(png),它会工作吗

标签: java spring hibernate


【解决方案1】:
MultipartFile  file;
byte [] byteArr=file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);

【讨论】:

  • 嗨@sanjaya,当我尝试获取字节时,它抛出错误:org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [receiveFailedMDN] 在上下文中路径 [/IHEProfilesServices] 抛出异常 [处理程序调度失败;嵌套异常是 java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.readFully(Ljava/io/InputStream;[B)V] 根本原因是 java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils .readFully(Ljava/io/InputStream;[B)V
【解决方案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// 

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 2020-12-24
    • 2019-08-12
    • 2020-05-07
    • 2018-12-10
    • 2013-08-10
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多