既然是struts2框架中的文件上传功能,当然就需要struts2的jar包,以及支持文件上传所要用到的辅助包,在我的项目下有以下jar包:
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>个人觉得文件下载比文件上传难理解,但是自己做过一遍具体流程之后,就能从中理解怎么实现。
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>