Struts2文件上传与下载

本文主要写的是struts2 的文件上传与下载(中文文件名问题的解决)。 

使用的时候直接在地址栏中输入:http://localhost:8080/fud/ 回车即可 

详细配置见下面代码: 
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> <!-- 设置默认编码格式 --> <constant name="struts.i18n.encoding" value="GBK" /> <!-- 设置最大上传文件是300M --> <constant name="struts.multipart.maxSize" value="314572800" /> <!-- 设置默认的临时文件存储位置 --> <constant name="struts.multipart.saveDir" value="C:/fileUpload" /> <!-- 设置调试模式 --> <constant name="struts.devMode" value="true"></constant> <package name="fileupload" extends="struts-default" namespace="/file"> <!-- 文件上传 --> <action name="upload" class="com.file.action.FileUploadAction"> <result type="redirectAction"> <param name="actionName">list</param> <param name="namespace">/file</param> </result> </action> <!-- 下载文件 --> <action name="download" class="com.file.action.FileDownloadAction"> <result type="stream" name="success"> <param name="contentType">application/octet-stream</param> <!-- 要有相对应的getDownloadFile()方法返回值是 InputStream --> <param name="inputName">downloadFile</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">4096</param> </result> </action> <!-- 文件下载列表 --> <action name="list" class="com.file.action.FileListAction"> <result>/file.jsp</result> </action> </package> </struts>

上传文件Action处理: 
上传的时候到不用注意中文编码问题,因为struts已经帮我们做了 

 package com.file.action; import java.io.File; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport { private File file; private String contentType; private String fileName; @Override public String execute() throws Exception { File saveFile = new File("c:/download/" + fileName); if (!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs(); FileUtils.copyFile(file, saveFile);// 复制文件 this.addFieldError("isSuccess", "文件上传成功!"); return SUCCESS; } public void setUploadContentType(String contentType) { this.contentType = contentType; } public void setUpload(File file) { this.file = file; } public void setUploadFileName(String fileName) { this.fileName = fileName; } }

 

下载时的Action,这个需要特别的注意:特别是在下载带有中文名字的文件的时候要对中文进行编码与解码。代码如下 

 

 

 package com.file.action; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import com.opensymphony.xwork2.ActionSupport; public class FileDownloadAction extends ActionSupport { private String fileName;// 要下载的文件名 public String execute() throws Exception { return SUCCESS; } // 下载文件 public InputStream getDownloadFile() { InputStream is = null; try { is = new FileInputStream("c:/download/" + fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } return is; } public void setFileName(String fileName) { try {// 解决中文文件名问题 this.fileName = new String(fileName.getBytes("ISO-8859-1"), "GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } public String getFileName() { String name = ""; try {// 解决下载文件中文文件名问题 name = new String(fileName.getBytes("GBK"), "ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return name; } }

 

下面这个与文件下载就没有什么关系了,主要是为了显示主页面用的:代码如下

 package com.file.action; import java.io.File; import java.util.LinkedList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class FileListAction extends ActionSupport { private List<File> files = new LinkedList<File>(); public String execute() throws Exception { return SUCCESS; } public List<File> getFiles() { File file = new File("c:/download/"); File filelist[] = file.listFiles(); for (File f : filelist) { files.add(f); } return files; } }

 

接下来就是jsp页面了; 
导航页面index.jsp写法 

<mce:script language="javascript"><!-- window.location = "/fud/file/list"; // --></mce:script>

在这就是显示页面了 list.jsp 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <h3> 下载文件 </h3> <table align="center" border="1"> <tr> <td width="10%"> 序号 </td> <td width="80%"> 文件名 </td> <td width="10%"> 下载 </td> </tr> <tr> <td> <s:iterator status="status" value="files" var="f"> <tr> <td> <s:property value="#status.count" /> </td> <td> <s:property value="#f.name" /> </td> <td> <s:a action="download" namespace="/file" encode="false"> <s:param name="fileName" value="%{#f.name}" /> 下载 </s:a> </td> </tr> </s:iterator> </table> <hr> <!-- 文件上传模块 --> <s:form action="upload" method="post" enctype="multipart/form-data" namespace="/file"> <s:file label="请选择文件" name="upload" ></s:file> <s:submit name="submit" value="提交"></s:submit> <s:fielderror name="isSuccess" /> </s:form> <s:debug></s:debug> </body> </html>

使用说明: 
在浏览器中输入:http://localhost:8080/fud 
结果如图(当然前提是"c://download/"文件夹下要有东西) 

你可能感兴趣的:(exception,struts,String,File,upload,Class)