package com.action;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.aspectj.util.FileUtil;
import com.opensymphony.xwork2.ActionContext;
public class UploadAction {
private File file;
private String fileFileName; // 文件名
private String fileContentType;// 文件类型
private String maximumSize;// 文件大小
private String allowedTypes;// 上传文件容许的类型
public String execute() throws Exception {
String realpath = ServletActionContext.getServletContext().getRealPath("/upload");
HttpSession session=ServletActionContext.getRequest().getSession();
HttpServletRequest request=ServletActionContext.getRequest();
boolean falg = false;
String fileType[] = allowedTypes.split(",");
for (int j = 0; j < fileType.length; j++) {
if (fileContentType.equals(fileType[j])) {
falg = true;
}
}
if (falg == false) {
ActionContext.getContext().put("errormsg", "你上传的格式不是images格式文件!");
return "error_msg";
}
if (file.length() > Long.parseLong(maximumSize)) {
ActionContext.getContext().put("errormsg", "上传文件超过限制5MB");
return "error_msg";
}
fileFileName = fileFileName.toLowerCase();// 获取这个文件转为为小写
/**
*/
boolean falg1 = false;
if (falg1) {
ActionContext.getContext().put("errormsg", "该文件已经导入,请选择其他文件!");
return "error_msg";
}
/** 正在上传1 */
File uploadFile = new File(realpath);
if (!uploadFile.exists()) {
uploadFile.mkdir();
}
File savefile = new File(new File(realpath), fileFileName);
// 创建文件目录
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
try {
FileUtil.copyFile(file, savefile);
//session.setAttribute("msgs", "上传成功");
// IOUtils.renameFile(uploadFile, "goods_");
//System.out.println("fileFileName:"+fileFileName);
request.setAttribute("msgs",URLEncoder.encode(fileFileName,"UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
String[] fileFiles = fileFileName.split("\\.");
return UUID.randomUUID().toString()+"."+fileFiles[1];
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getMaximumSize() {
return maximumSize;
}
public void setMaximumSize(String maximumSize) {
this.maximumSize = maximumSize;
}
public String getAllowedTypes() {
return allowedTypes;
}
public void setAllowedTypes(String allowedTypes) {
this.allowedTypes = allowedTypes;
}
}
<action name="upload" class="com.UploadAction"> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg, image/pjpeg ,image/bmp, image/gif, image/x-png, </param> <param name="maximumSize">5242880</param> <result name="success">/message.jsp</result> <result name="error_msg">/error.jsp</result> </action>