上传图片

时间:2015-09-16 11:23:18

标签: java hibernate

我的控制器类是

@RequestMapping(value = "/profilePictureUpload", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("fileExtension") String fileExtension, @RequestParam("file") MultipartFile file,HttpServletRequest request) {

     logger.info("In add profile Picture Upload");  
     String mediaResponse=null;;
     try {
         String token = request.getHeader("authToken");
         System.out.println("+++++++++++++++token+++++++++++++++++++"+token );
            User user = userDao.findUserByAuthToken(token);
            System.out.println("+++++++++++++++USER+++++++++++++++++++"+user );
            System.out.println("+++++++++++++++file.getBytes()+++++++++++++++++++"+file.getBytes() );
            if (user != null) {

                  Physician physicain=physicainDao.findPhysicianById(user.getPhysicianId().getPhysicianId());
                  String fileStoragPath=userOriginalServerPath+"/"+physicain.getPhysicianId();
                  File file1=new File(fileStoragPath);
                  file1.mkdirs();
                  String filePath=fileStoragPath+"/"+physicain.getPhysicianId()+System.currentTimeMillis()+fileExtension;
                  FileOutputStream fileOuputStream =new FileOutputStream(filePath); 
                  fileOuputStream.write(file.getBytes());
                  fileOuputStream.close();  
                  /**
                   * Creating thumbnail for media upload
                   */
                  File thumbnailPath=new File(userThumbnailFilePath+physicain.getPhysicianId());
                  thumbnailPath.mkdirs();
                  String thumbnail_path=physicain.getPhysicianId()+"/"+physicain.getPhysicianId()+System.currentTimeMillis()+fileExtension;

                  Thumbnails.of(new File(filePath)).size(75, 75).toFile(new File(userThumbnailFilePath+thumbnail_path));
                  physicain.setProfileImage("MedikmUserPicture/thumbnail/"+thumbnail_path);
                  physicainDao.update(physicain);   
                  mediaResponse="MedikmUserPicture/thumbnail/"+thumbnail_path;

             }else{
                  mediaResponse=MedikmConstants.USER_INVALID_AUTHENTICATION__MESSAGE;

             } 
            System.out.println("================mediaResponse============"+mediaResponse);
            return mediaResponse;

        }
        catch(Exception ex){
            ex.printStackTrace();
            logger.error("Error in media tag Method :"+ex);
            return mediaResponse=MedikmConstants.USER_INVALID_AUTHENTICATION__MESSAGE;
        }

 }

通过上传图像获取hibernate异常,如

org.hibernate.exception.DataException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)

图片上传成功,但我不知道为什么我得到这个例外字段名称的医师类是我映射

 @Column(name = "Profile_Image")
private String profileImage;

请帮助我让我离开这个例外。

1 个答案:

答案 0 :(得分:2)

Profile_Image sholud为 TINYBLOB BLOB MEDIUMBLOB LONGBLOB

根据您的需要,如:

  • TINYBLOB:最大长度为255个字节
  • BLOB:最大长度为65,535字节
  • MEDIUMBLOB:最大长度为16,777,215字节
  • LONGBLOB:最大长度为4,294,967,295字节

因此您必须更新 Profile_Image

示例:如果您设置了MEDIUMBLOB列,那么它的映射应该是: -

@Lob
@Column(name="Profile_Image",columnDefinition="mediumblob")
private byte[] profileImage;

注意:我在保存非常大的图片时收到此错误,我的数据类型为 Blob ,而我使用的是 mysql {{1 }}

经过一番搜索,我发现了

  

如果使用大型BLOB列,则必须增加此值   长串。它应该与您想要使用的最大BLOB一样大。   max_allowed_pa​​cket的协议限制为1GB。价值应该是   1024的倍数; nonmultiples向下舍入到最近   数倍。

所以我找到了Packet for query is too large . You can change this value on the server by setting the max_allowed_packet variable.

SET GLOBAL max_allowed_packet = 1024*1024*number of MB

这将设置允许数据包的最大大小为14 GB,请确保重新启动MySQL以使更改生效

请参阅thisthis完整示例以在hibernate中保存图像

相关问题