手机
当前位置:查字典教程网 >编程开发 >Java >java中struts2实现文件上传下载功能
java中struts2实现文件上传下载功能
摘要:先谈一谈struts2实现文件的上传和下载实例实现的原理:Struts2是通过CommonsFileUpload文件上传。CommonsFi...

先谈一谈struts2实现文件的上传和下载实例实现的原理:

Struts 2是通过Commons FileUpload文件上传。

Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。

具体实现:

一、创建index.jsp页面

<body> <s:form action="upload" method="post" theme="simple" enctype="multipart/form-data"> <table align="center" width="50%" border="1"> <tr> <td>选择上传文件</td> <td id="more"> <s:file name="file"></s:file> <input type="button" value="Add More.."> </td> </tr> <tr> <td><s:submit type="button" value="submit"/></td> <td><s:reset value="reset "></s:reset></td> </tr> </table> <table align="center" width="50%" border="1"> <tr> <td>测试.txt</td> <td> <a href="<s:url value='download.action'><s:param name='fileName'>测试.txt</s:param> </s:url>">下载</a> </td> </tr> </table> </s:form> </body>

创建result.jsp页面

<body> <s:form> <div>成功上传的文件:<br> <ul> <s:iterator value="#request.fileName" id="file" status="status"> <li><s:property/> </li> </s:iterator> </ul> </div> </s:form> </body>

当然别忘了在每个页面上都添加上struts2 标签引用<%@taglib prefix="s" uri="/struts-tags" %>

二、创建updown.js文件,在index.jsp中引用

function checkf(){ var files = document.getElementsByName("file"); if(files[0].value.length!=0){ return true; }else{ alert("请选择文件"); return false; } } function addMore() { var td = document.getElementById("more"); var br = document.createElement("br"); var input = document.createElement("input"); var button = document.createElement("input"); input.type = "file"; input.name = "file"; button.type = "button"; button.value = "Remove"; button.onclick = function() { td.removeChild(br); td.removeChild(input); td.removeChild(button); } td.appendChild(br); td.appendChild(input); td.appendChild(button); }

三、创建upDownloadAction.java

package com.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.http.HttpServletRequest; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; public class UpDownloadAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<File> file;// 对应jsp中file标签 private List<String> fileFileName;// private List<String> fileContentType;// private String fileName;//获得jsp中pram参数 @SuppressWarnings("deprecation") // 文件上传 public String uploadFiile() throws Exception { String root = ServletActionContext.getServletContext().getRealPath( "/upload");// 上传路径 System.out.println(root); InputStream inputStream; File destFile; OutputStream os; for (int i = 0; i < file.size(); i++) { inputStream = new FileInputStream(file.get(i)); destFile = new File(root, this.getFileFileName().get(i)); os = new FileOutputStream(destFile); byte[] buffer = new byte[400]; int length = 0; while ((length = inputStream.read(buffer)) > 0) { os.write(buffer, 0, length); } inputStream.close(); os.close(); } HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("fileName", fileFileName); return SUCCESS; } // 文件下载 public InputStream getDownloadFile() throws FileNotFoundException, UnsupportedEncodingException { System.out.println(getFileName()); // 如果下载文件名为中文,进行字符编码转换 ServletActionContext.getResponse().setHeader("Content-Disposition","attachment;fileName=" + java.net.URLEncoder.encode(fileName, "UTF-8")); InputStream inputStream = new FileInputStream("F:/" //使用绝对路径 ,从该路径下载“测试.txt"文件 + this.getFileName()); System.out.println(inputStream); return inputStream; } // 下载 public String downloadFile() throws Exception { return SUCCESS; } public String getFileName() throws UnsupportedEncodingException { return fileName; } public void setFileName(String fileName) throws UnsupportedEncodingException { this.fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8"); } }

注:属性的 get和set方法已省略。

四、最后是配置文件

1、web.xml配置

<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

2、struts.xml配置

<struts> <constant name="struts.i18n.encoding" value="utf-8"></constant> <constant name="struts.multipart.saveDir" value="f:"></constant> <package name="struts2" extends="struts-default"> <action name="upload" method="uploadFiile"> <result name="success">/jsp/result.jsp</result> <interceptor-ref name="fileUpload"> <!--maximumSize (可选) - 这个拦截器允许的上传到action中的文件最大长度(以byte为单位). 注意这个参数和在webwork.properties中定义的属性没有关系,默认2MB--> <param name="maximumSize">409600</param> <!--allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html), 这些列表是这个拦截器允许的可以传到action中的contentType.如果没有指定就是允许任何上传类型.--> <param name="allowedTypes"> text/plain </param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> <action name="download" method="downloadFile" > <result name="success" type="stream"> <> <param name="contentType"> application/txt; </param> <> <!--contentDisposition是文件下载的处理方式,包括内联(inline)和附件(attachment), 默认是inline, 使用附件时这样配置:attachment;filename="文件名" 。--> <param name="contentDisposition"> attachment;filename="${fileName}" </param> <> <param name="inputName">downloadFile</param> <> <param name="bufferSize">2048</param> </result> </action> </package> </struts>

一个简单的Struts2多文件上传和单文件下载就实现了。

以上就是本文的全部内容,希望对大家的学习有所帮助。

【java中struts2实现文件上传下载功能】相关文章:

深入Java Robot实现控制鼠标和键盘的方法详解

java 重定义数组的实现方法(与VB的ReDim相像)

java中url汉字编码互相转换实例

java实现合并两个已经排序的列表实例代码

Java如何实现HTTP断点续传功能

struts2单个文件上传的两种实现方式

java导出csv方法实现讲解

java实现大文件分割与合并的实例代码

java代码实现截图功能(屏幕截图)

java中 spring 定时任务 实现代码

精品推荐
分类导航