/**
* 上传附件
*
* @return
*/
public String addAttachment() {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
String message = new String();
// String file1FileName="";
PrintWriter pw = null;
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getRealPath("/uploadFiles/");
try {
pw = response.getWriter();
String ret = checkFile();
if (null != ret) {
message = "{success:false,msg:'" + ret + "'}";
} else {
//String date = s_para_reportDate.substring(0, 4);
// String path = "/uploadFiles";
String localPath = request
.getRealPath("/uploadFiles");
File pathDir = new File(localPath);
if (pathDir.isDirectory()) {
System.out.println("文件夹已经存在");
} else {
if (pathDir.mkdirs()) {
System.out.println("新建目录成功");
} else {
System.out.println("新建目录失败");
}
}
// String fileName = s_para_fileName.substring(s_para_fileName
// .lastIndexOf('.'));
// uploadFileName = new Date().getTime() +"";
// file1FileName =uploadFileName + fileName;
try {
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(localPath
+ File.separator + uploadFileName);
FileInputStream fis = new FileInputStream(getFile1());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// String upload = path + "/" + file1FileName;
message = "{success:true,msg:'上传成功',area:'" + file1FileName
+ "'}";
}
} catch (Exception e) {
message = "{success:false,msg:'服务器运行时错误,请联系管理员!'}";
e.printStackTrace();
} finally {
pw.write(message);
pw.flush();
pw.close();
pw = null;
}
// String pathTmp = request.getContextPath();
//article.setAuthor(userName);
//article.setFileName(uploadFileName);
//article.setUploadDate(new Date());
//article.setFilePath(request.getScheme() + "://"
// + request.getServerName() + ":" + request.getServerPort()
// + pathTmp + "/uploadFiles/" + file1FileName);
int beginIndex = file1FileName.indexOf(".");
String append = file1FileName.substring(beginIndex);
String nowTime = new SimpleDateFormat("yyyyMMdd-HH-mm-ss")
.format(new Date());
File Old = new File(path+File.separator+uploadFileName);
File New = new File(path+File.separator+nowTime+append);
Old.renameTo(New);
Dictionary dictionary = dictionaryService.findById(new Long(117));
String keyNo = dictionary.getKeyNo();
Long idTmp = new Long(keyNo);
idTmp++;
dictionary.setKeyNo(idTmp.toString());
dictionaryService.updateDictionary(dictionary, new Long(117));//117对应dictionary表中id为117的记录
//article.setListId(idTmp);
ActionContext actionContext = ActionContext.getContext();
Map<String, String> session = actionContext.getSession();
session.put("fileName",file1FileName);//fileName保存的是处理后的名,因为在JSP中,超链接是打不开中文文件的
session.put("filePath",nowTime+append);//filePath保存的是文件的真实名
session.put("listId",idTmp.toString());
return SUCCESS;
}