Struts2文件上传和下载
2017-09-19 00:03
483 查看
struts.xml配置文件
UploadAction类
upload.jsp上传页面
struts.xml配置
down.jsp页面
DownloadAction类
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="csl" namespace="/" extends="struts-default"> <action name="up" class="com.test.UploadAction"> <result name="success">/down.jsp</result> <result name="input">/index.jsp</result> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/x-pn</param> <param name="maximumSize">1024*1024</param> </interceptor-ref> <interceptor-ref name="defaultStack" /> </action> </package> </struts>
UploadAction类
package com.test; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class UploadAction extends ActionSupport{ private File image; private String imageFileName; private String imageContentType; private String name ; private String pass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public File getImage() { return image; } public void setImage(File image) { this.image = image; } public String getImageFileName() { return imageFileName; } public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } public String getImageContentType() { return imageContentType; } public void setImageContentType(String imageContentType) { this.imageContentType = imageContentType; } @Override public String execute() throws Exception { String path = ServletActionContext.getServletContext().getRealPath("/uploads"); System.out.println(name+"--->"+pass); File file = new File(path); if(!file.exists()){ file.mkdir(); } System.out.println(file); FileUtils.copyFile(image,new File(path,imageFileName)); return SUCCESS; } }
upload.jsp上传页面
<body> <form action="up" method="post" enctype="multipart/form-data"> <input type="file" name="image"/> <input type="submit" value="上传"/> </form> </body>
struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="csl" namespace="/" extends="struts-default"> <action name="down" class="com.test.DownloadAction"> <result name="success" type="stream"> <!-- 文件流 --> <param name="contentType"> text/plain</param> <!-- 内容类型 --> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <!-- 文件名类型 --> <param name="inputName">inputStream</param> <!-- 文件流 --> <param name="bufferSize">1024& b12f lt;/param> <!-- 缓存字节大小 --> </result> </action> </package> </struts>
down.jsp页面
hljs xml"> <a href="down?fileName=<s:property value="imageFileName" />">下载<s:property value="imageFileName" /></a> <img alt="" src="<s:property value="'uploads/'+imageFileName"/>"> </body>
DownloadAction类
package com.test; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport{ private String fileName; private InputStream inputStream; public InputStream getInputStream() throws FileNotFoundException { String path = ServletActionContext.getServletContext().getRealPath("/uploads"); String file = path +"\\"+ fileName ; System.out.println(file); InputStream is = new BufferedInputStream(new FileInputStream(file)); System.out.println(is); return is; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } @Override public String execute() throws Exception { return SUCCESS; } }
相关文章推荐
- Struts2中实现多文件上传于下载
- Struts2使用注解实现文件的上传与下载(二)
- Struts2中文件的上传与下载
- Struts2实现文件上传和下载
- Struts2文件上传与下载
- struts2文件上传和下载
- JAVA Web 之 struts2文件上传下载演示(一)(转)
- Struts2自学入门(十)——多文件上传及文件下载
- struts2_大纲08_文件上传下载
- Struts2实现文件的上传与下载
- Struts2文件的上传和下载
- struts2文件上传和下载
- Struts2之实现文件上传与下载
- Struts2实现文件上传和下载
- Jimoshi_Struts2 框架学习(三)--valueStack和ognl、struts2验证、struts2文件上传和下载
- WEB文件上传下载过程简介(基于Struts2)
- struts2中的文件上传和下载
- struts2实现文件上传下载
- struts2文件上传下载(含中文编码问题)
- Struts2实现文件上传下载笔记