标签:
文件的上传和下载需要两个jar包 commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar
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=UTF-8"> <title>EYang</title> <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> <script type="text/javascript"> $(function() { }); </script> </head> <body> <form action="file/fileUp.action" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form> <form action="file/fileLoad.action" method="post"> <input type="submit" value="下载"> </form> </body> </html>
struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- value为false时、关闭动态方法调用 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 开发模式 <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> 可以添加外部的xml文件 <include file="example.xml"/> --> <!-- Add packages here --> <constant name="struts.multipart.saveDir" value="/tmp" /> <!-- 指定国际化资源文件的baseName为uploadFile --> <constant name="struts.custom.i18n.resources" value="uploadFile"></constant> <!-- 设置该应用使用的编码集为utf-8 --> <constant name="struts.i18n.encoding" value="utf-8"></constant> <package name="eyang" namespace="/file" extends="struts-default"> <!-- 全局拦截器配置 <interceptors> <interceptor name="loginInterceptor" class="com.eyang.interceptor.LoginInterceptor"></interceptor> <interceptor-stack name="myInterceptor"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="loginInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myInterceptor"></default-interceptor-ref> --> <!-- <interceptors> <interceptor name="loginInterceptor" class="com.eyang.interceptor.LoginInterceptor"></interceptor> <interceptor name="SessionInterceptor" class="com.eyang.interceptor.SessionInterceptor"></interceptor> </interceptors> <action name="login" class="com.eyang.action.LoginAction" method="login"> <result >/success.jsp</result> 单个action需要执行的拦截器 <interceptor-ref name="SessionInterceptor"></interceptor-ref> <interceptor-ref name="loginInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> --> <action name="fileUp" class="com.eyang.action.FileUpAction" method="fileUp"> <result>/fileList.jsp</result> </action> <action name="fileLoad" class="com.eyang.action.FileLoadAction" method="fileLoad"> <!-- 配置结果类型为stream的结果 --> <result name="success" type="stream"> <!-- 指定下载文件的类型 --> <param name="contentType">image/jpg</param> <!-- 指定下载文件的方法名 --> <param name="inputName">downloadFile</param> <param name="contentDisposition">attachement;filename="struts.jpg"</param> <!-- 指定下载文件的缓冲大小 --> <param name="bufferSize">50000000</param> </result> </action> </package> </struts>
文件上传Action
package com.eyang.action; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUpAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private File file; /** * @return String * @throws IOException */ public String fileUp() throws IOException { String realpath = ServletActionContext.getServletContext().getRealPath("/file"); // D:\Office\eclipse-jee-kepler-SR2-win32\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\EYang_Template\file System.out.println("realpath: " + realpath); if (file != null) { File savefile = new File(new File(realpath), "001.jpg"); if (!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs(); FileUtils.copyFile(file, savefile); } return SUCCESS; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } }
文件下载Action
package com.eyang.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileLoadAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; public String fileLoad() { return SUCCESS; } public InputStream getDownloadFile() throws Exception { InputStream in = ServletActionContext.getServletContext().getResourceAsStream("/img/qianyesong.jpg"); return in; } }
标签:
原文地址:http://www.cnblogs.com/keryang/p/4281550.html