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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值