帮朋友做一个文件上传的小功能开始
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;
}
问题解决!