標籤:
檔案的上傳和下載需要兩個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\fileSystem.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;}}
struts2實現檔案的上傳和下載