strust2 空文件下传有关问题
strust2 空文件上传问题
struts2 当上传文件为空时不会在临时目录创建该文件,应用会抛出FileNotFoundException 下面是一种替代方法 创建后也不必删除,因为是空文件。
struts2 当上传文件为空时不会在临时目录创建该文件,应用会抛出FileNotFoundException 下面是一种替代方法 创建后也不必删除,因为是空文件。
public String saveCreateDoc(){ try{ this.doc.setUser(this.getCurrentUser()); //文档创建人 this.doc.setDocLatestVersion(1l); //新建档案时的版本为第一个版本 this.docService.txStore(doc); //保存文档 //获取系统配置文件 ProjectConfig pc = ProjectConfig.getInstance(); //保存上传文件 InputStream is; //此处的try catch 处理是为了处理struts2 上传文件大小为0KB的bug, 当文件为空时,在临时目录创建一个同名文件 try{ is = new FileInputStream(this.docFile); }catch(FileNotFoundException e) { File tempfile = new File(pc.getFileSaveTempDir() + pc.getFileDirSeparator() + this.docFileFileName); if(!tempfile.exists()){ tempfile.createNewFile(); } is = new FileInputStream(tempfile); } OutputStream os = new FileOutputStream(pc.getFileSaveDir() + pc.getFileDirSeparator() + docFileFileName); byte[] buffer = new byte[400]; int length = 0; while((length = is.read(buffer)) > 0){ os.write(buffer, 0, length); } //关闭输出流 os.close(); }catch(Exception e){ LOG.error("新建档案--保存档案发生错误--档案名称为:" + doc.getDocName(), e); } return SUCCESS; }