1. 程式人生 > >Struts2的檔案上傳和下載(1)單檔案上傳

Struts2的檔案上傳和下載(1)單檔案上傳

在struts2中整合fileuoload功能,因為在匯入的jar包中包含了common-fileipload.jar檔案
在struts2中的interceptor 中有一個fileupload攔截器,他的主要功能就是完成檔案上傳。

注意事項

  1. method=post
  2. 所有元件必須有name
  3. encType=multipart/form-data

Struts2上傳步驟

  1. 在頁面上建立表單
  2. 建立Action,在action中定義三個變數用來接收上傳資訊
  3. 使用FileUtils的copyFile方法完成檔案複製操作

FileUploadAction.java

package com.qwl.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{
	private static final long serialVersionUID=1L;
	//提交過來的檔案
	private File file;
	//提交過來的file的名字
private String fileFileName; //提交過來的file的型別 private String fileContentType; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName =
fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String execute()throws Exception{ /*System.out.println(file.getCanonicalPath()); System.out.println(this.fileContentType); System.out.println(this.fileFileName);*/ //流操作 /*//檔案輸入流 InputStream is=new FileInputStream(file); //設定檔案的儲存目錄 String uploadPath=ServletActionContext.getServletContext().getRealPath("/upload"); //設定目標檔案 File toFile=new File(uploadPath,this.getFileFileName()); //檔案輸出流 OutputStream os=new FileOutputStream(toFile); byte[] buffer=new byte[1024]; int length=0; //讀取file檔案輸出到toFile檔案中 while(-1!=(length=is.read(buffer,0,buffer.length))) { os.write(buffer); } //關閉輸入流和輸出流 is.close(); os.close();*/ writeFile(); return SUCCESS; } public void writeFile() throws IOException{ FileUtils.copyFile(file,new File("/upload",fileFileName)); } } }

fileUpload.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>檔案上傳</title>
</head>
<body>
	<s:form action="fileUpload" method="post" enctype="multipart/form-data">
		<s:file name="file" label="檔案上傳"></s:file>
		<s:submit value="上傳"/>
		<s:reset value="重置"/>
	</s:form>
	
</body>
</html>

result.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>檔案上傳</title>
</head>
<body>
	<s:form action="fileUpload" method="post" enctype="multipart/form-data">
		<s:file name="file" label="檔案上傳"></s:file>
		<s:submit value="上傳"/>
		<s:reset value="重置"/>
	</s:form>
	
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC  
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"  
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="struts2" namespace="/" extends="struts-default">
		<action name="fileUpload" class="com.qwl.action.FileUploadAction">
			<result name="success">/result.jsp</result>
		</action>
	</package>
</struts>