读书人

strust2 空文件下传有关问题

发布时间: 2012-09-27 11:11:17 作者: rapoo

strust2 空文件上传问题
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;}

读书人网 >软件架构设计

热点推荐