action模板修改的一点建议
对于action模板中的upload方法,建议从模板中删掉,将代码移入BaseAction,包括如下代码:/**
* 文件上传
*/
public void upload() {
String savePath = ServletActionContext.getServletContext().getRealPath(
"/")
+ ResourceUtil.getUploadDirectory() + "/";// 文件保存目录路径
String saveUrl = "/" + ResourceUtil.getUploadDirectory() + "/";// 文件保存目录URL
String contentDisposition = ServletActionContext.getRequest()
.getHeader("Content-Disposition");// 如果是HTML5上传文件,那么这里有相应头的
if (contentDisposition != null) {// HTML5拖拽上传文件
Long fileSize = Long.valueOf(ServletActionContext.getRequest()
.getHeader("Content-Length"));// 上传的文件大小
String fileName = contentDisposition.substring(contentDisposition
.lastIndexOf("filename=\""));// 文件名称
fileName = fileName.substring(fileName.indexOf("\"") + 1);
fileName = fileName.substring(0, fileName.indexOf("\""));
ServletInputStream inputStream;
try {
inputStream = ServletActionContext.getRequest()
.getInputStream();
} catch (IOException e) {
uploadError("上传文件出错!");
ExceptionUtil.getExceptionMessage(e);
return;
}
if (inputStream == null) {
uploadError("您没有上传任何文件!");
return;
}
if (fileSize > ResourceUtil.getUploadFileMaxSize()) {
uploadError("上传文件超出限制大小!", fileName);
return;
}
// 检查文件扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase();
if (!Arrays.<String> asList(
ResourceUtil.getUploadFileExts().split(",")).contains(
fileExt)) {
uploadError("上传文件扩展名是不允许的扩展名。\n只允许"
+ ResourceUtil.getUploadFileExts() + "格式!");
return;
}
savePath += fileExt + "/";
saveUrl += fileExt + "/";
SimpleDateFormat yearDf = new SimpleDateFormat("yyyy");
SimpleDateFormat monthDf = new SimpleDateFormat("MM");
SimpleDateFormat dateDf = new SimpleDateFormat("dd");
Date date = new Date();
String ymd = yearDf.format(date) + "/" + monthDf.format(date) + "/"
+ dateDf.format(date) + "/";
savePath += ymd;
saveUrl += ymd;
File uploadDir = new File(savePath);// 创建要上传文件到指定的目录
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
String newFileName = UUID.randomUUID().toString()
.replaceAll("-", "")
+ "." + fileExt;// 新的文件名称
File uploadedFile = new File(savePath, newFileName);
try {
FileCopyUtils.copy(inputStream, new FileOutputStream(
uploadedFile));
} catch (FileNotFoundException e) {
uploadError("上传文件出错!");
ExceptionUtil.getExceptionMessage(e);
return;
} catch (IOException e) {
uploadError("上传文件出错!");
ExceptionUtil.getExceptionMessage(e);
return;
}
uploadSuccess(ServletActionContext.getRequest().getContextPath()
+ saveUrl + newFileName, fileName, 0);// 文件上传成功
return;
}
MultiPartRequestWrapper multiPartRequest = (MultiPartRequestWrapper) ServletActionContext
.getRequest();// 由于struts2上传文件时自动使用了request封装
File[] files = multiPartRequest.getFiles(ResourceUtil
.getUploadFieldName());// 上传的文件集合
String[] fileNames = multiPartRequest.getFileNames(ResourceUtil
.getUploadFieldName());// 上传文件名称集合
if (files == null || files.length < 1) {
uploadError("您没有上传任何文件!");
return;
}
for (int i = 0; i < files.length; i++) {// 循环所有文件
File file = files;// 上传的文件(临时文件)
String fileName = fileNames;// 上传文件名
if (file.length() > ResourceUtil.getUploadFileMaxSize()) {
uploadError("上传文件超出限制大小!", fileName);
return;
}
// 检查文件扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase();
if (!Arrays.<String> asList(
ResourceUtil.getUploadFileExts().split(",")).contains(
fileExt)) {
uploadError("上传文件扩展名是不允许的扩展名。\n只允许"
+ ResourceUtil.getUploadFileExts() + "格式!");
return;
}
savePath += fileExt + "/";
saveUrl += fileExt + "/";
SimpleDateFormat yearDf = new SimpleDateFormat("yyyy");
SimpleDateFormat monthDf = new SimpleDateFormat("MM");
SimpleDateFormat dateDf = new SimpleDateFormat("dd");
Date date = new Date();
String ymd = yearDf.format(date) + "/" + monthDf.format(date) + "/"
+ dateDf.format(date) + "/";
savePath += ymd;
saveUrl += ymd;
File uploadDir = new File(savePath);// 创建要上传文件到指定的目录
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
String newFileName = UUID.randomUUID().toString()
.replaceAll("-", "")
+ "." + fileExt;// 新的文件名称
File uploadedFile = new File(savePath, newFileName);
try {
FileCopyUtils.copy(file, uploadedFile);// 利用spring的文件工具上传
} catch (Exception e) {
uploadError("上传文件失败!", fileName);
return;
}
uploadSuccess(ServletActionContext.getRequest().getContextPath()
+ saveUrl + newFileName, fileName, i);// 文件上传成功
}
}
private void uploadError(String err, String msg) {
Map<String, Object> m = new HashMap<String, Object>();
m.put("err", err);
m.put("msg", msg);
writeJson(m);
}
private void uploadError(String err) {
uploadError(err, "");
}
private void uploadSuccess(String newFileName, String fileName, int id) {
Map<String, Object> m = new HashMap<String, Object>();
m.put("err", "");
Map<String, Object> nm = new HashMap<String, Object>();
nm.put("url", newFileName);
nm.put("localfile", fileName);
nm.put("id", id);
m.put("msg", nm);
writeJson(m);
}
那个是openoffice的库文件,不知道是否开源,也就不知道是否可以修改 xjj_zyw 发表于 2013-4-19 10:43 static/image/common/back.gif
那个是openoffice的库文件,不知道是否开源,也就不知道是否可以修改
我已经改了,能成功运行
页:
[1]