Struts2知识点回顾(六)文件上传与下载
来源:互联网 发布:小鲜肉演技知乎 编辑:程序博客网 时间:2024/05/16 17:18
文件上传
准备工作
既然是struts2框架中的文件上传功能,当然就需要struts2的jar包,以及支持文件上传所要用到的辅助包,在我的项目下有以下jar包:
编码流程
- 准备好文件保存目录。
- 编写上传页面(表单)。
- 创建Action继承ActionSupport,在我这里是FileUploadAction.java。
- 声明接收文件各项参数的变量并且提供getter、setter,在我这里是文件类型:xlsContentType、文件名:xlsName,文件xls。其中xls为表单组件中name属性对应的值。
- 编写action处理方法,在我这里是upload()方法,方法中执行了解析Excel文件和保存文件。我在这里顺便学习了如何使用jxl工具来解析Excel文件。所以jar包中多了个jxl.jar。
- 在配置文件struts.xml中配置action
开始写代码
1.上传文件的页面fileupload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body> <fieldset style="width:300px;height:100px;text-align:center;"> <legend>文件上传</legend> <!-- method要使用POST提交,eenctype要设置成multipart/form-data --> <form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data"> <!-- 这个name需要在action中声明,用来接收这个参数域--> <input type="file" name="xls"><br><br> <input type="submit" value="上传文件"> </form> </fieldset></body></html>
2.FileUploadAction.java
package action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport{ private String xlsFileName; private String xlsContentType; private File xls; public String upload() throws Exception{ FileInputStream in = new FileInputStream(xls); //从输入流中构建Workbook对象 Workbook workbook = Workbook.getWorkbook(in); Sheet s = workbook.getSheet(0); int count = s.getRows(); System.out.println("文件名:" + xlsFileName); System.out.println("文件类型:" + xlsContentType); //遍历cell,cell为单元格 for(int i = 0; i < count; i++){ Cell[] cells = s.getRow(i); for(Cell cell : cells){ System.out.print(cell.getContents() + "\t"); } System.out.println(); } //保存上传的文件 File save = new File(ServletActionContext.getServletContext().getRealPath(xlsFileName)); OutputStream out = new FileOutputStream(save); int b = 0; while((b = in.read()) != -1){ out.write(b); } in.close(); out.close(); return SUCCESS; } public String getXlsFileName() { return xlsFileName; } public void setXlsFileName(String xlsFileName) { this.xlsFileName = xlsFileName; } public File getXls() { return xls; } public void setXls(File xls) { this.xls = xls; } public String getXlsContentType() { return xlsContentType; } public void setXlsContentType(String xlsContentType) { this.xlsContentType = xlsContentType; }}
3.struts.xml
<package name="upload" extends="struts-default" namespace="/"> <action name="upload" class="action.FileUploadAction" method="upload"> <result name="success">/success.jsp</result> </action></package>
运行结果
文件下载
个人觉得文件下载比文件上传难理解,但是自己做过一遍具体流程之后,就能从中理解怎么实现。
编码流程
- 提供下载页面。
- 准备好提供下载的文件。
- 创建Action继承ActionSupport,声明下载时显示的文件名,并提供getter、setter(在action中声明能解决中文乱码问题)。在我这里Action是FileDownload.action,文件名是fileName。
- 编写一个方法签名为 public InputStream getXXX()的方法,其中XXX要与配置文件中的配置对应。并且返回值返回的是包含要下载文件的输入流。在我这里是getFile()。
- 编写一个action方法来对外提供下载。
- 配置文件中配置action。
开始写代码
1.下载页面filedownload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>文件下载</title></head><body> <fieldset> <legend>文件下载</legend> <a href="${pageContext.servletContext.contextPath }/download">点击下载</a> </fieldset></body></html>
2.FileDownloadAction.java
package action;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import javax.servlet.Servlet;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileDownloadAction extends ActionSupport { private String fileName; //这个方法名需要与struts.xml中指定的一致 public InputStream getFile() throws UnsupportedEncodingException{ //文件中文名用URLEncoder编译一下。 this.fileName = URLEncoder.encode("示例.xls", "UTF-8"); //返回包含了文件的输入流 return ServletActionContext.getServletContext().getResourceAsStream("file/示例.xls"); } //对外部访问的action,在配置文件中对名为“success”的结果进行配置。 public String download(){ return SUCCESS; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; }}
3.配置文件struts.xml(最重要的一步)
<package name="download" extends="struts-default" namespace="/"> <action name="download" class="action.FileDownloadAction" method="download"> <result name="success" type="stream"> <param name="contentType">text/plain</param> <!-- 指定文件名称 --> <param name="contentDisposition">attachment;filename="${fileName}"</param> <!-- 这个就是指定要取哪个方法的输入流的配置 --> <param name="inputName">file</param> </result> </action></package>
阅读全文
0 0
- Struts2知识点回顾(六)文件上传与下载
- struts2学习笔记(六)文件上传与下载(下)基于Struts2的文件上传与下载
- struts2学习笔记(六)文件上传与下载(上)jspServlet的文件上传与下载
- 走进Struts2(六)— 文件上传和下载
- 文件上传与下载(一)struts2
- Struts2文件上传与下载
- Struts2文件上传与下载
- Struts2文件上传与下载
- Struts2文件上传与下载
- Struts2 文件上传与下载
- struts2文件上传与下载
- Struts2文件上传与下载
- Struts2文件上传与下载
- Struts2文件上传与下载
- Struts2-文件上传与下载
- struts2文件上传与下载
- Struts2文件上传与下载
- struts2文件上传与下载
- HDU 4565 -- So Easy! (矩阵幂模板)
- 其他对象
- Unity 中 启动页 滚动 代码实现
- 水波纹特效—Ripple
- bzoj 3749: [POI2015]Łasuchy
- Struts2知识点回顾(六)文件上传与下载
- null和“”,==和equals()的混用和区别
- 树莓派raspberry之samba
- HDOJ 1869 六度分离(floyd最短路~)
- 如何学习代码
- 在ubuntu14.04下openni+opencv+kinectV1的学习一:学习准备
- 树形结构的3种搜索方式示例分享
- BMP图像数据格式详解
- java中类的静态成员和实例成员