标签:context 配置 system dac pack work 页面 icc eal
//UploadAction类
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class UploadAction {
// 普通表单元素参数
private String uname;
// 文件域表单参数
private File pic;
// 上传类型
private String picContentType;
// 上传文件名
private String picFileName;
// action配置请求参数
private String savePath;
// 配置/upload
public String execute() {
// 对文件对象进行读写、
if (uname != null) {
try {
FileInputStream fis = new FileInputStream(pic);
// 获取相对路径的绝对路径
String absPath = ServletActionContext.getServletContext()
.getRealPath(savePath);
FileOutputStream fos = new FileOutputStream(absPath
+ File.separator + picFileName);
// 读写
byte[] b = new byte[fis.available()];
fis.read(b);// 读取
fos.write(b);// 写入
fis.close();// 关闭输入流
fos.flush();// 清空输出流
fos.close();// 关闭输出流
System.out.println("上传者:" + uname + "上传文件名" + picFileName
+ "上传文件类型:" + picContentType);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Action.SUCCESS;
}
public UploadAction() {
}
public UploadAction(String uname, File pic, String picContentType,
String picFileName, String savePath) {
this.uname = uname;
this.pic = pic;
this.picContentType = picContentType;
this.picFileName = picFileName;
this.savePath = savePath;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
//struts.xml
//上传
<action name="upload" class="action.UploadAction">
<param name="savePath">/upload</param>
<result>download.jsp</result>
</action>
//前台页面小伙伴们随便写写就行了~·~~测试一下就可以了
标签:context 配置 system dac pack work 页面 icc eal
原文地址:https://www.cnblogs.com/zzy666/p/10030175.html