S2SH整合过程中 文件上传后不能删除temp文件的问题(Resource Leaking: Could not remove uploaded file )
2016-12-19 10:21
471 查看
帮朋友做一个文件上传的小功能开始
Action中是这样处理的
Service中
这样写 上传文件后 就会报一个警告!:could not remove uploaded file
后来检查了一下 发现
于是修改Service文件为:
经分析 应该是事务没有提交的时候就关闭了文件流 是不可以的
是不可以的
最后修改为:
Action:
service:
问题解决!
Action中是这样处理的
public class EditPhotoAction extends BaseHttpAwareAction{ private IUserPhotoService userPhotoService; private File userPhoto; @Override public String execute() throws Exception { //System.out.println("执行EditPhotoAction"); message=userPhotoService.addUserPhoto((User)session.get(LOGINUSER_STRING), userPhoto); session.put(LOGINUSER_STRING, ((UserPhoto)message.getMessageObject()).getOwner()); return SUCCESS; } /** * @return the userPhotoService */ public IUserPhotoService getUserPhotoService() { return userPhotoService; } /** * @param userPhotoService the userPhotoService to set */ public void setUserPhotoService(IUserPhotoService userPhotoService) { this.userPhotoService = userPhotoService; } /** * @return the userPhoto */ public File getUserPhoto() { return userPhoto; } /** * @param userPhoto the userPhoto to set */ public void setUserPhoto(File userPhoto) { this.userPhoto = userPhoto; } }
Service中
public BaseMessage addUserPhoto(User user, File photoFile) throws Exception { message = new UserMessage(false, "更新图片失败"); UserPhoto photo = null; if (user != null && user.getPhoto() != null) { photo = user.getPhoto(); } else { photo = new UserPhoto(); photo.setOwner(user); user.setPhoto(photo); } photo.setPhotoBlob(Hibernate.createBlob(new FileInputStream(photoFile))); getUserPhotoDAO().saveUserPhoto(photo); message.setMessageFlag(true); message.setMessageContent("更新图片成功!"); message.setMessageObject(photo); return message; }
这样写 上传文件后 就会报一个警告!:could not remove uploaded file
后来检查了一下 发现
photo.setPhotoBlob(Hibernate.createBlob(new FileInputStream(photoFile)));这句之后没有关闭文件流的操作
于是修改Service文件为:
public BaseMessage addUserPhoto(User user, File photoFile) throws Exception { message = new UserMessage(false, "更新图片失败"); UserPhoto photo = null; if (user != null && user.getPhoto() != null) { photo = user.getPhoto(); } else { photo = new UserPhoto(); photo.setOwner(user); user.setPhoto(photo); } FileInputStream inputStream=new FileInputStream(photoFile); photo.setPhotoBlob(Hibernate.createBlob(inputStream)); getUserPhotoDAO().saveUserPhoto(photo); inputStream.close(); message.setMessageFlag(true); message.setMessageContent("更新图片成功!"); message.setMessageObject(photo); return message; }但是这样文件上传后,事务提交时 hibernate 会报异常,无法读取文件流。
经分析 应该是事务没有提交的时候就关闭了文件流 是不可以的
inputStream.close();
是不可以的
最后修改为:
Action:
public class EditPhotoAction extends BaseHttpAwareAction{ private IUserPhotoService userPhotoService; private File userPhoto; @Override public String execute() throws Exception { //System.out.println("执行EditPhotoAction"); FileInputStream inputStream=new FileInputStream(userPhoto); message=userPhotoService.addUserPhoto((User)session.get(LOGINUSER_STRING), inputStream); session.put(LOGINUSER_STRING, ((UserPhoto)message.getMessageObject()).getOwner()); inputStream.close(); return SUCCESS; } /** * @return the userPhotoService */ public IUserPhotoService getUserPhotoService() { return userPhotoService; } /** * @param userPhotoService the userPhotoService to set */ public void setUserPhotoService(IUserPhotoService userPhotoService) { this.userPhotoService = userPhotoService; } /** * @return the userPhoto */ public File getUserPhoto() { return userPhoto; } /** * @param userPhoto the userPhoto to set */ public void setUserPhoto(File userPhoto) { this.userPhoto = userPhoto; } }
service:
public BaseMessage addUserPhoto(User user, File photoFile) throws Exception { message = new UserMessage(false, "更新图片失败"); UserPhoto photo = null; if (user != null && user.getPhoto() != null) { photo = user.getPhoto(); } else { photo = new UserPhoto(); photo.setOwner(user); user.setPhoto(photo); } photo.setPhotoBlob(Hibernate.createBlob(new FileInputStream(photoFile))); getUserPhotoDAO().saveUserPhoto(photo); message.setMessageFlag(true); message.setMessageContent("更新图片成功!"); message.setMessageObject(photo); return message; }
问题解决!
相关文章推荐
- 由通过WebService上传附件给SharePoint的List时文件不能太大的问题说起…
- [导入]IIS+Tomcat整合上传以及静态文件缓存问题!
- CFtpConnection上传、下载、删除文件的问题
- 解决sql2008 r2 还原数据库文件不能删除用户的问题
- 使用edtftpnet上传文件到Serv-U出现乱码的问题,并且不能创建中文文件名或文件夹。
- 自定义安装过程当中遇到的不能找到installstate文件问题
- 解决firefox3.0,ie8 在上传文件时只能获得文件名,而不能获得文件路径的问题
- Tomcat进行卸载部署undeploy时不能删除jar文件的问题
- vsftpd不能上传文件的问题
- ssh 整合ckediter ckfinder 文件上传问题
- 一个我上传的文件,不能解压的问题!
- 我的SQL server和Visual Studio出了问题 在控制面板 安装不了 说有问题 在360里面提示的是安装 老是提示不能删除 结果卸到最后一个程序的时候 要给卸了重来装 怎么样强行删除文件
- 不能上传文件问题的总结
- win2003上传文件不能超过200k问题解决方法
- File.Delete不能删除文件的问题
- 解决asp.net不能上传大文件的问题
- 恼人的Eclipse和Tomcat整合后,不能自动编译Tomcat Project的java文件的问题终于解决啦
- centos上和vsftpd相关的防火墙与SElinux设置(本地用户不能上传文件的问题)
- 上传文件细节处理问题(包括中文乱码、限制文件大小、显示上传速度、删除临时文件,随机生成文件夹等)
- 整合ckeditor+ckfinder,解决上传文件路径问题