Struts3.0的上传改进:
1、HttpServletRequest增加了文件上传的支持
Part getPart(String name)通过名称获取文件上传域
Collection<Part> getParts()获取所有文件上传域
2、表单属性enctype数据表单编码方式
application/x-www-formOurlencoded 默认编码格式
multipart/form-data 二进流方式
text/plain 表单发送邮件
index.jsp的form表单
UploadServlet.jsp
配置web.xml
结果:
Struts2的上传与下载:
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:UploadAction.java
package action; 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; //直接在struts.xml文件中配置的属性 private String savePath; public String upload() throws Exception { //以服务器的文件保存地址和原文件名建立上传文件输出流 FileInputStream fis = new FileInputStream(getUpload()); FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer , 0 , len); } fos.close(); fis.close(); return SUCCESS; } 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(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } //接受struts.xml文件配置值的方法 } |
第三步:upload.jsp页面
第四步:struts.xml配置
结果:
Struts2下载:
实现文件下载的FileDownloadAction:提供一个返回InputStream流的方法。
在WebRoot下建一个uploadFiles
DownLoadAction.java
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="mess" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="hello" namespace="/hello" extends="struts-default"> <action name="login" class="action.DownLoadAction" method="upload"> <param name="savePath"> /uploadFiles </param> <param name="allowType">image/png,image/gif,image/jpeg</param> <result name="success"> /succ.jsp </result> <result name="input"> /index.jsp </result> <!-- 配置拦截器 --> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/png,image/gif,image/jpeg</param> <param name="maximumSize">4000</param> </interceptor-ref> <!-- 默认的拦截器 --> <interceptor-ref name="defaultStack" /> </action> <action name="down" class="action.DownLoadAction"> <param name="inputPath">/uploadFiles/1.gif</param> <result name="success" type="stream"> <param name="contentType">image/jpg</param> <param name="inputName">targetFile</param> <param name="bufferSize">4096</param> <param name="contenrDisposition">filename="clock.jpg"</param> </result> </action> </package> </struts> |
down.jsp
结果: