Struts1 多文件上传时,放在list的数据,不会清空

使用struts1 多文件上传后,arrayList的数据不会清空,下次上传时,还有前一次的数据,这是为啥啊??

AlbumForm类的代码

private List<FormFile> photos = new ArrayList<FormFile>();
public AlbumForm(){
    this.clearPhotos();
}
public List<FormFile> getAllPhoto() {
    return photos;
}
public FormFile getPhotos(int i) {
    return photos.get(i);
}
public void setPhotos(int i,FormFile myPhoto) {        
    this.photos.add(myPhoto);
}
//请求完成后清空photos
public void clearPhotos(){
    this.photos.clear();
}

action里的操作:

    AlbumForm  af = (AlbumForm) form;
    Users u= (Users)request.getSession().getAttribute("loginInfo");
    String userId =u.getId().toString();
    String albumId = request.getParameter("albumId");
    Album  album = (Album) photoServiceImp.findById(Album.class, Integer.valueOf(albumId));
    
    String webSavePath = "/images/upload/"+u.getId()+"/album/"+albumId;        
    String upPath = request.getSession().getServletContext().getRealPath(webSavePath);
    System.out.println(albumId+"--"+userId+"\r\n"+upPath+"\r\n");
    System.out.println("文件个数:"+af.getAllPhoto().size());
    //循环上传照片
    for (int i = 0; i < af.getAllPhoto().size(); i++) {
        Photo p = new Photo();
        FormFile photo = af.getPhotos(i);
        System.out.println(photo.getFileName());
        try {
            String saveName = MyFileStream.uploadFile(photo, upPath);                 
            p.setAddTime(new Date());
            p.setAlbum(album);
            p.setPhoto(webSavePath+"/"+saveName); 
            System.out.println(webSavePath+"/"+saveName);
            photoServiceImp.save(p);
            System.out.println("添加的信息"+p.getId()+"--"+p.getAddTime());
            System.out.println("Hashcode"+p.hashCode());                 
        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e.getMessage());
        }
        
    }
    //清空form中的photos数组列表
    //af.clearPhotos();
    return mapping.findForward("goOneAlbumUI");

况且,在AlbumForm里写了构造函数,进行清空,也不管用,如:第一次上传了2张图片,第二次上传3张,在第二次上传时 af.getAllPhoto().size()获取到的总数就是5;
但在action中进行调用AlbumForm类的方法af.clearPhotos()时,可以清空掉。这是我代码问题还是struts1问题??
求大神不吝赐教。。。

阅读 2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进