1. 程式人生 > >Struts 檔案的上傳與下載

Struts 檔案的上傳與下載

一、struts框架提供了檔案的上傳和下載功能,簡化了很多的程式碼

二、檔案的上傳

1.前端用struts框架中的ognl標籤

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@  taglib uri="/struts-tags" prefix="s" %>

<h5>檔案的上傳</h5>
<s:form namespace="/" action="upload" method="post" enctype="multipart/form-data" theme="xhtml" >
	<s:textfield name="username" label="使用者名稱" />
	<s:file  name="image"  label="檔案" ></s:file>
	<s:submit  value="提交"  ></s:submit>
</s:form>

2.後端action

public class UploadAction  extends ActionSupport{
	
	@Setter
	private String username;
	@Setter
	private File image; // 上傳的檔案物件 image和前端name一致
	@Setter
	private String imageFileName; // 上傳的檔名字
	@Setter
	private String imageContentType; // 上傳檔案的MIME型別
	
	@Override
	public String execute() throws Exception {
		// 構建檔案上傳的目錄
		String dir = ServletActionContext.getServletContext().getRealPath("/image") ;
		// 構建檔案上傳的目錄和名稱
		File destFile = new File(dir  ,imageFileName);
		// 拷貝檔案
		FileUtils.copyFile(image, destFile);
		
		System.out.println("----------------");
		System.out.println(image );
		System.out.println(imageFileName);
		System.out.println(imageContentType);
		 
		return NONE;
		
	}
}

3.配置檔案xml

<!-- 國際化 -->
	<constant name="struts.custom.i18n.resources" value="uploadmessages"></constant>
	<package name="p5" extends="struts-default" namespace="/upload"   >
		<!-- 註冊攔截器 -->
		<interceptors>
			<!-- 1 定義攔截器棧 -->
			<interceptor-stack name="myStack">
			<!-- 1.1 註冊多個攔截器 -->
				<interceptor-ref name="fileUpload">
					<!-- 設定上傳檔案的允許檔案拓展名 -->
					<param name="allowedExtensions">jpeg,png,jpg,excel</param>
					<!-- 設定單個檔案的大小 -->
					<param name="maximumSize">1048076</param>
				</interceptor-ref>
				<!-- 1.2 引用預設攔截器棧,放在最後  --> 
				<interceptor-ref name="defaultStack"/>
			</interceptor-stack>
		</interceptors>
		<!-- 2 引用攔截器棧 -->
		<default-interceptor-ref name="myStack"></default-interceptor-ref>
		
		<action name="upload" class="com.struts.web.upload.UploadAction" > 
			<result name="input" >/upload/upload.jsp </result> 
		 </action>
	
	</package> 

4.利用框架的上傳檔案大小,錯誤型別的提示Properties檔案

#struts-messages.properties 檔案中
struts.messages.error.uploading=檔案上傳錯誤: {0}
struts.messages.error.file.too.large=檔案 {0} 太大了. 限制最大為 {4} bytes!
struts.messages.error.content.type.not.allowed=檔案MIME型別限定為: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=檔案拓展名不被允許: {0} "{1}" "{2}" {3}

三、檔案的下載

1.前端程式碼 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@  taglib uri="/struts-tags" prefix="s" %>

<h5>檔案下載</h5>
<a href="/down/download?fileName=a.rar" >三國演義</a><br/>
<a href="/down/download?fileName=西遊記.rar" >西遊記</a><br/>

2.後端action

public class DownLoadAction extends ActionSupport {

	private String fileName;
	//防止檔案亂碼
	public void setFileName(String fileName) throws Exception {
		this.fileName = new String((fileName.getBytes("ISO-8859-1")),"UTF-8");
	}
	// xml檔案中獲得檔案的名字
	public String getFileName() {
		return fileName;
	}
	// 返回下載檔案的輸入流,把下載檔案讀取到程式中來
	public InputStream getInputStream() throws Exception{
		String dir = ServletActionContext.getServletContext().getRealPath("/image");
		System.out.println(dir);
		File file = new File(dir,fileName);
		return new FileInputStream(file);
	}
	
	@Override
	public String execute() throws Exception {
		
		
		System.out.println("下載檔案==" + fileName);
	
		return SUCCESS;
	}
}

3.配置檔案xml

<package name="p6" extends="struts-default" namespace="/down"   >
		
		<action name="download" class="com.struts.web.download.DownLoadAction" > 
			<result name="success" type="stream"  >
				<!-- 設定下載檔案的建議儲存名稱,獲取action傳遞的的filename屬性 -->
				<param name="contentDisposition">attachment;fileName="${fileName}"</param>
				<!-- 設定返回下載檔案的輸入流方法  -->
				<param name="inputName">inputStream</param>
			</result>  
		 </action>
	
	</package>