S2SH整合过程中 文件上传后不能删除temp文件的问题(Resource Leaking: Could not remove uploaded file )

帮朋友做一个文件上传的小功能开始

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;


    }


问题解决!


GC Root: Global variable in native code ─ dalvik.system.PathClassLoader instance Leaking: NO (NetSpeedManager$b↓ is not leaking and A ClassLoader is never leaking) ↓ ClassLoader.runtimeInternalObjects ─ java.lang.Object[] array Leaking: NO (NetSpeedManager$b↓ is not leaking) ↓ Object[1626] ─ com.konka.apkhall.edu.module.album.player.presenter.NetSpeedManager$b class Leaking: NO (a class is never leaking) ↓ static NetSpeedManager$b.a ~ ─ com.konka.apkhall.edu.module.album.player.presenter.NetSpeedManager instance Leaking: UNKNOWN Retaining 6.7 MB in 75213 objects ↓ NetSpeedManager.listeners ~~~~~~~~~ ─ java.util.Collections$SynchronizedRandomAccessList instance Leaking: UNKNOWN Retaining 6.7 MB in 75212 objects ↓ Collections$SynchronizedCollection.c ~ ─ java.util.ArrayList instance Leaking: UNKNOWN Retaining 6.7 MB in 75211 objects ↓ ArrayList[8] ~~~ ─ com.konka.apkhall.edu.module.album.player.widgets.VideoStatusView instance Leaking: YES (View.mContext references a destroyed activity) Retaining 1.3 MB in 15235 objects View not part of a window view hierarchy View.mAttachInfo is null (view detached) View.mID = R.id.video_status View.mWindowAttachCount = 1 mContext instance of com.konka.apkhall.edu.module.album.AlbumActivity with mDestroyed = true ↓ View.mContext → com.konka.apkhall.edu.module.album.AlbumActivity instance Leaking: YES (ObjectWatcher was watching this because com.konka.apkhall.edu.module.album.AlbumActivity received ​ Activity#onDestroy() callback and Activity#mDestroyed is true) ​ Retaining 1.0 MB in 11484 objects ​ key = 9ca61e77-8cfc-471d-94d2-e64c3b173343 ​ watchDurationMillis = 32423 retainedDurationMillis = 27421 mApplication instance of com.konka.apkhall.edu.app.EduApplication ​ mBase instance of android.app.ContextImpl分析原因,给出具体解决代码
最新发布
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值