* @param
* @return
*/
public ActionForward doUpload(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Dto outDto = new BaseDto();
BaseActionForm cForm = (BaseActionForm) form;
// 单个文件,如果是多个就cForm.getFile2()....支持最多5个文件
FormFile myFile = cForm.getFile1();
// 获取web应用根路径,也可以直接指定服务器任意盘符路径
String savePath = getServlet().getServletContext().getRealPath("/") + "/upload/demo/";
//String savePath = "d:/upload/";
// 检查路径是否存在,如果不存在则创建之
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String type = myFile.getFileName().substring(myFile.getFileName().lastIndexOf("."));
// 文件真实文件名
String fileName = getSessionContainer(request).getUserInfo().getUserid();
fileName = fileName + "_" + G4Utils.getCurrentTime("yyyyMMddhhmmss") + type;
// 我们一般会根据某种命名规则对其进行重命名
// String fileName = ;
File fileToCreate = new File(savePath, fileName);
if (myFile.getFileSize() > 204800) {
outDto.put("success", new Boolean(true));
outDto.put("msg", "文件上传失败,你只能上传小于100KB的图片文件");
outDto.put("state", "error");
}else {
// 检查同名文件是否存在,不存在则将文件流写入文件磁盘系统
if (!fileToCreate.exists()) {
FileOutputStream os = new FileOutputStream(fileToCreate);
os.write(myFile.getFileData());
os.flush();
os.close();
}
outDto.put("success", new Boolean(true));
outDto.put("msg", "文件上传成功");
outDto.put("state", "ok");
outDto.put("aUrl", request.getContextPath() + "/upload/demo/" + fileName);
}
write(outDto.toJson(), response);
return mapping.findForward(null);
}