近期总结 MultipartHttpServletRequest 使用

1、文件上传

下面代码,应该

@Action
public Renderer savedata() throws IOException{
HttpServletRequest request = ActionContext.getActionContext().getHttpServletRequest();
//获取到承载文件的请求体
MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
InputStream fs;
OutputStream out = null;
Map<String, Object> map = new HashMap<String, Object>();
String batchname = request.getParameter("batchname");
String noticeid = request.getParameter("noticeid");
String departmentid = this.userSession.getCurrentUserBmID();
try {
fs = mrequest.getFileInputStream("fileup");
String[] fldcodName = mrequest.getFileFields();
String fileName = mrequest.getFileName(fldcodName[0]).toString();
fileName = fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());
String relativePath = "/theme/web_files/tzfbfile/"+fileName;
//创建文件路径
String filePath = sourceSrc+relativePath;
File filenew = new File(filePath);
//文件夹创建
String upFilepath=sourceSrc+"/theme/web_files/tzfbfile/";
File upFilenew = new File(upFilepath);
judeDirExists(upFilenew);
judeFileExists(filenew);
out = new FileOutputStream(filePath);
//判断是增加还是修改
if(StringUtils.isNotBlank(noticeid)){
String updateSql = " update TAL_BATCH_NOTICE set notice_titile = '%s', notice_file_url = '%s' where noticeid = '%s' ";
updateSql = String.format(updateSql, batchname,relativePath,noticeid);
this.jdbcOperations.update(updateSql);
}else{
String insertSql = " insert into TAL_BATCH_NOTICE(noticeid,notice_titile,notice_file_url,pubstatus,batchid,departmentid) ";
insertSql +=" values(GET_UUID,?,?,null,null,?) ";
this.jdbcOperations.update(insertSql,batchname,relativePath,departmentid);
}
FileUtils.write(fs, out);
map.put("message", "保存成功!");
map.put("success", "true");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
map.put("message", "保存失败!");
map.put("success", "false");
}finally{
out.flush();
out.close();
}
return new TemplateRenderer(this.getClass(), "savedata", map);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值