標籤:
通常我們會涉及到上傳檔案和下載檔案,在沒接struts2架構之前,我們都是使用apache下面的commons子項目的FileUpload組件來進行檔案的上傳,但是那樣做的話,代碼看起來比較繁瑣,而且不靈活,在學習了struts2後,struts2為檔案上傳下載提供了更好的實現機制,在這裡我分別就檔案下載和多檔案上傳的原始碼進行一下講
檔案上傳
首先先建立jsp頁面(用於多檔案上傳)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>登入頁面</title></head><body> <s:form action="upload.action" enctype="multipart/form-data" method="post"> <s:file name="upload" label="選擇檔案" /> <br /> <s:file name="upload" label="選擇檔案" /> <br /> <s:file name="upload" label="選擇檔案" /> <br /> <s:submit name="submit" value="上傳檔案"></s:submit> </s:form></body></html>
success.jsp頁面(用來顯示上傳成功後的檔案和檔案類型)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>成功頁面</title></head><body> 您所上傳的檔案是:<s:property value="uploadFileName"/><br/> 檔案類型:<s:property value="uploadCOntentType"/></body></html>
接下來建立UploadAction類
package cn.happy.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport { // 上傳檔案的屬性 private File[] upload; // 上傳檔案的類型 private String[] uploadContentType; // 上傳檔案的名稱 private String[] uploadFileName; // 上傳檔案的地址 private String savePath; @Override public String execute() throws Exception { byte[] buffer=new byte[1024]; for (int i = 0; i < upload.length; i++) { //建立上傳檔案的輸入資料流 FileInputStream fis=new FileInputStream(getUpload()[i]); //建立上傳檔案的輸出資料流, getImageFileName()[i] FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName()[i]); int length=fis.read(buffer); while(length>0){ fos.write(buffer,0,length); length = fis.read(buffer); } fis.close(); fos.flush(); fos.close(); } return SUCCESS; } public String getSavePath() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } }
最後編寫設定檔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.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <action name="upload" class="cn.happy.action.UploadAction" method="execute"> <param name="savePath">/image</param> <result name="success">/upload/success.jsp</result> </action> </package></struts>
實現效果展示
選擇檔案後
檔案下載
編寫jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> <a href="download.action?fileName=1.jpg">點擊此處下載文檔</a> </body></html>
建立FileDownAction類
package action;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileDownAction extends ActionSupport { //讀取下載檔案的目錄 private String inputPath; //下載的檔案名稱 private String FileName; //讀取下載檔案的輸入資料流 private InputStream inputStream; //下載檔案的類型 private String conetntType; //建立InputStream輸入資料流 public InputStream getInputStream() throws IOException { System.out.println("123"); String path=ServletActionContext.getServletContext().getRealPath(inputPath); return new BufferedInputStream(new FileInputStream(path+"\\"+FileName)); } @Override public String execute() throws Exception{ return SUCCESS; } public String getInputPath() { return inputPath; } public String getFileName() { return FileName; } public String getConetntType() { return conetntType; } public void setInputPath(String inputPath) { this.inputPath = inputPath; } public void setFileName(String fileName) { FileName = fileName; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public void setConetntType(String conetntType) { this.conetntType = conetntType; }}
編寫設定檔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.devMode" value="true"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="download" class="action.FileDownAction"> <param name="inputPath">/image</param> <result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;Filename="${FileName}"</param> <param name="bufferSize">4096</param> </result> </action> </package> </struts>
最後在webRoot目錄下建立image檔案裡面儲存下載用到的檔案
實現效果
點擊檔案下載
附加
匯入jar包
web.xml配置
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
檔案上傳(多檔案上傳)/下載