2013-08-06 115 views
5

//或者将多部分文件保存到DB的任何其他解决方案。 我试过这种方式,但得到错误。如何将MultipartFile转换为字节流

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); 
} 
+0

你得到什么错误? –

+0

你有什么错误?它如何保存在数据库中(使用ORM映射?)哪种Multipart文件正在使用(MIME,JMS,..)? – Filip

+0

我正在尝试使用ORM映射将图像文件(PNG)它的工作 –

回答

16
MultipartFile file; 
byte [] byteArr=file.getBytes(); 
InputStream inputStream = new ByteArrayInputStream(byteArr); 
-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//