Struts2 annotation convention upload file 注释上传文件
@InterceptorRefs({@InterceptorRef(value="fileUpload", params=
{"allowedTypes","image/bmp,image/PNG,image/gif,image/JPEG,image/JPG,image/jpg,application/x-
zip-compressed","maximumSize","20971520"}
),@InterceptorRef(value="defaultStack")})
@SuppressWarnings("serial")
public class UploadFileAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContentType;
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public void setFile(File file) {
this.file = file;
}
public File getFile(){
return file;
}
public String execute(){
System.out.println("begin upload file.... ");
LinkPath link = LinkPath.newInstance();
String dataPath = link.getdataPath();
DateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
Date date=new Date();
String dateDir = format.format(date);
File f = new File(dataPath+"\\"+dateDir);
if(!f.exists()){
f.mkdirs();
}
File dataFile = new File(dataPath+"\\"+dateDir+"\\"+this.getFileFileName());
try {
//将第一个参数对应的 文件 copy 到 第二个参数对应的文件中
FileUtil.copyFile(this.file,dataFile);
if(dataFile.exists()){
String fileType = dataFile.getPath().substring
(dataFile.getPath().lastIndexOf("."),dataFile.getPath().length());
if(".zip".equals(fileType)||".ZIP".equals(fileType)){
UpZIP zip = new UpZIP();
zip.unzip(dataFile.getPath(), dataPath+"\\"+dateDir);
dataFile.delete();
}
}
Struts2Utils.renderText("{success:true,message:'上传成功'}");
} catch (IOException e) {
Struts2Utils.renderText("{success:flase,message:'失败'}");
e.printStackTrace();
}
return null;
}
}
@InterceptorRefs({@InterceptorRef(value="fileUpload", params=
{"allowedTypes","image/bmp,image/PNG,image/gif,image/JPEG,image/JPG,image/jpg,application/x-
zip-compressed","maximumSize","20971520"}
),@InterceptorRef(value="defaultStack")})
@SuppressWarnings("serial")
public class UploadFileAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContentType;
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public void setFile(File file) {
this.file = file;
}
public File getFile(){
return file;
}
public String execute(){
System.out.println("begin upload file.... ");
LinkPath link = LinkPath.newInstance();
String dataPath = link.getdataPath();
DateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
Date date=new Date();
String dateDir = format.format(date);
File f = new File(dataPath+"\\"+dateDir);
if(!f.exists()){
f.mkdirs();
}
File dataFile = new File(dataPath+"\\"+dateDir+"\\"+this.getFileFileName());
try {
//将第一个参数对应的 文件 copy 到 第二个参数对应的文件中
FileUtil.copyFile(this.file,dataFile);
if(dataFile.exists()){
String fileType = dataFile.getPath().substring
(dataFile.getPath().lastIndexOf("."),dataFile.getPath().length());
if(".zip".equals(fileType)||".ZIP".equals(fileType)){
UpZIP zip = new UpZIP();
zip.unzip(dataFile.getPath(), dataPath+"\\"+dateDir);
dataFile.delete();
}
}
Struts2Utils.renderText("{success:true,message:'上传成功'}");
} catch (IOException e) {
Struts2Utils.renderText("{success:flase,message:'失败'}");
e.printStackTrace();
}
return null;
}
}