本文共 1593 字,大约阅读时间需要 5 分钟。
struts.xmlaction/uploadFiles /JSP/success.jsp
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private String title; private File upload; private String uploadContentType; private String uploadFileName; private String savePath; 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; } public String getSavePath() { return ServletActionContext.getServletContext().getRealPath("/WEB-INF/" + savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } public String execute() throws Exception { 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); } fos.close(); return SUCCESS; } }
转载地址:http://wkgmi.baihongyu.com/