📄 uploadaction.java
字号:
package com.ata.upload;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
// private String allowTypes;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
//注意savePath的get和set方法
public String getSavePath() throws Exception {
return ServletActionContext.getRequest().getRealPath(savePath);
//获取web应用的绝对路径
}
public void setSavePath(String value) {
this.savePath = value;
}
// //手动实现文件过滤
// public String getAllowTypes(){
// return allowTypes;
// }
// public void setAllowTypes(String allowTypes)
// {
// this.allowTypes=allowTypes;
// }
// public String filterType(String[] types){
// String fileType=getUploadContentType();
// for(String type:types){
// if(type.equals(fileType)){
// return null;
// }
// }
// return INPUT;
// }
//
//
//
//
public String execute() throws Exception{
// String filterResult =filterType(getAllowTypes.split(","));
// if (filterResult!=null){
// ActionContext.getContext().put("typeError","您要上传的文件类型不正确!");
// return filterResult;
// }
//
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
FileInputStream fis= new FileInputStream(getUpload());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
return SUCCESS;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -