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);
}