upload.jsp的页面:
<s:form action="uploadAction" method="post" enctype="multipart/form-data">
<s:textfield name="title" label="上传的文件名"/>
<s:file name="upload" label="选择文件"/>
<s:submit value="上传"/>
</s:form>
uploadAction
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 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 {
/*
* E:\project\workspace\apache-tomcat-7.0.29\work\Catalina\localhost\struts2upload\
* 注意这个位置getUpload()打印出来的东西
*/
System.out.println(getUpload());
FileInputStream fis = new FileInputStream(getUpload());
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+ getUploadFileName());
int read;
byte[] b = new byte[1024];
while((read=fis.read(b))>0){
fos.write(b,0,read);
}
return SUCCESS ;
}
}
<package name="test" extends="struts-default">
<action name="uploadAction" class="com.huxin.struts2upload.action.UpLoadAction">
<result>success.jsp</result>
<param name="savePath">upload</param>
</action>
</package>
下载:
<a href="downloadAction">下载2图片</a>
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoadAction extends ActionSupport{
private String inputPath;
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
/*
* InputStream入口
*/
public InputStream getTargetFile() throws Exception{
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
/*
public String execute() throws Exception {
return LOGIN;
}
*/
}
<action name="downloadAction" class="com.huxin.struts2upload.action.DownLoadAction">
<param name="inputPath">/uploadFiles/2.jpg</param>
<result type="stream">
<!-- 指定文件的类型 application/pdf ,application/zip ,images/jpg, -->
<param name="contentType">images/jpg</param>
<param name="inputName">targetFile</param>
<!-- attachment不能掉,是告诉客户端要下载这个2.jpg的文件 -->
<param name="contentDisposition">attachment;filename="2.jpg"</param>
<param name="buffersize">5000</param>
</result>
<result name="login">
/WEB-INF/view/login.jsp
</result>
</action>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>