一、建立項目
項目名稱:demodowload
二、添加struts jar包
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.jar
xwork-core-2.3.4.jar
三、在web.xml檔案中配置核心過濾器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Struts2_13fileUpload</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
四、在WebRoot下建立存儲檔案的目錄
/download
在該目錄下存放圖檔.jpeg
五、在項目中添加struts配置檔案中添加核心配置檔案
1.在項目中建立conf目錄
/conf
2.在conf目錄下不回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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple"/>
</struts>
六、建立Action
1.在src下建立包
包名:action
2.在包下建立Action
Action名稱:DownloadAction.java
Action内容:
public class DownloadAction extends ActionSupport {
private String fileName;
/*
* 把讀取檔案的流對象傳回,Web伺服器會讀取流對象中的資料并封裝到response中,然後發送到用戶端
*/
public InputStream getInputStream() throws FileNotFoundException, UnsupportedEncodingException{
InputStream is = null;
this.fileName="圖檔.jpeg";
//把URL路徑轉化為伺服器的本地路徑
String realPath = ServletActionContext.getServletContext().getRealPath("/download/"
+this.fileName);
is = new FileInputStream(realPath);
return is;
}
public String doDownloadFile(){
//此處可加入業務,如判斷使用者是否有下載下傳權限
return SUCCESS;
public String getFileName() throws UnsupportedEncodingException {
//編碼的目的是為了解決中文檔案名出現亂碼的問題
return java.net.URLEncoder.encode(fileName, "UTF-8");//該編碼方式在IE6下可能有該問題:檔案名長度不能超過16個字元
//return new String(this.fileName.getBytes("GBK"),"ISO-8859-1");//該編碼方式可相容IE6,但GBK不利于實作國際化
public void setFileName(String fileName) {
this.fileName = fileName;
}
3.在struts.xml檔案中進行action配置
<package name="default" namespace="/" extends="struts-default">
<action name="doDownloadFile" class="action.DownloadAction" method="doDownloadFile">
<result name="success" type="stream">
<param name="inputName">inputStream</param><!--檔案對應的流對象 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param><!--發送給用戶端的檔案名,需要注意中文亂碼問題 -->
<param name="contentType">application/octet-stream</param><!--檔案類型,application/octet-stream是不限制類型 -->
<param name="bufferSize">1024</param><!--緩沖區大小 -->
</result>
</action>
</package>
七、頁面準備
頁面名稱:downloadFile.jsp
頁岩内容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Insert title here</title>
</head>
<body>
<s:a action="doDownloadFile">下載下傳檔案</s:a>
</body>
</html>
本文轉自 素顔豬 51CTO部落格,原文連結:http://blog.51cto.com/suyanzhu/1559526