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

Struts 2 檔案上傳與下載

單個檔案上傳

表單設定enctype="multipart/form-data“

表單設定method="POST“ <s:file name="f1" label="logo"/>

多檔案上傳:

把所有的屬性改為陣列即可。

private File []f1;

private String [] contentType;

private String [] fileName;

Action中設定:

get,set方法必須新增file的名字;

f1, contentType, fileName三個屬性 (f1為File型別, contentType, fileName為String型別)

f1的getter/setter方法

f1ContentType的getter/setter方法

f1FileName的getter/setter方法

//上傳Action
public class UpfileAction extends ActionSupport {

	private File file;
	private String contentType;
	private String fileName;
	private String txt;

	public String getTxt() {
		return txt;
	}

	public void setTxt(String txt) {
		this.txt = txt;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return contentType;
	}

	public void setFileContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFileFileName() {
		return fileName;
	}

	public void setFileFileName(String fileName) {
		this.fileName = fileName;
	}

	@Override
	public String execute() throws Exception {
		
		System.out.println(txt);
		
		// 獲取伺服器根目錄
		String path = ServletActionContext.getServletContext().getRealPath(
				"/upfile");
		// 建立服務根目錄下的資料夾
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		// 目標檔案
		File dest = new File(dir, fileName);

		// 位元組輸入輸出流
		FileInputStream in = new FileInputStream(file);
		FileOutputStream out = new FileOutputStream(dest);
		// 臨時快取
		byte[] b = new byte[1024];
		int len = -1;
		// 檔案複製
		while ((len = in.read(b)) != -1) {
			out.write(b, 0, len);
		}
		// 關流
		in.close();
		out.close();
		// 返回提示資訊
		this.addActionMessage("檔案上傳成功!");
		return SUCCESS;
	}

}
//下載Action
public class DownloadAction extends ActionSupport {

	private int id;
	private String fileName;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public InputStream getInputStream(){
		try {
			//設定下載檔名稱
			fileName=URLEncoder.encode("高清無碼.avi","UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//設定下載檔案路徑
		String filepath = "upfile/aaaaa.xlsx";
		return ServletActionContext.getServletContext().getResourceAsStream(filepath);
	}
	

	@Override
	public String execute() throws Exception {
		System.out.println(id);
		return SUCCESS;
	}

}

struts.xml中的配置:

<struts>
- <!--  配置檔案上傳上限 
  --> 
  <constant name="struts.multipart.maxSize" value="10000000" /> 
- <!--  使用國際化配置 
  --> 
  <constant name="struts.custom.i18n.resources" value="res" /> 
- <package name="testpack" extends="struts-default" namespace="/">
- <action name="upfile" class="com.web.action.UpfileAction">
  <result>/index.jsp</result> 
  <result name="input">/index.jsp</result> 
- <!--  新增攔截器實現檔案上傳限制 
  --> 
- <interceptor-ref name="defaultStack">
- <!-- 檔案大小,單位位元組
  --> 
  <param name="fileUpload.maximumSize">1000000</param> 
- <!-- 檔案型別
  --> 
- <!--   <param name="fileUpload.allowedTypes">text/plain,application/vnd.ms-excel</param> 
  --> 
- <!-- 檔案字尾
  --> 
  <param name="fileUpload.allowedExtensions">.txt,.xls</param> 
  </interceptor-ref>
  </action>
- <action name="download" class="com.web.action.DownloadAction">
- <!--  檔案下載時result標籤的type屬性設定成stream 
  --> 
- <result type="stream">
- <!--  配置下載檔案型別 
  --> 
  <param name="contentType">application/octet-stream</param> 
- <!--  下載檔名稱 
  --> 
  <param name="contentDisposition">attachment;filename="${fileName}"</param> 
- <!--  *下載檔案流物件 
  --> 
  <param name="inputName">inputStream</param> 
- <!--  檔案大小 
  --> 
  <param name="bufferSize">2048</param> 
- <!--  設定是否快取 
  --> 
  <param name="allowCaching">false</param> 
- <!--  字元編碼 
  --> 
  <param name="contentCharSet">UTF-8</param> 
  </result>
  </action>
  </package>
  </struts>

JSP頁面中的配置:

引入struts2標籤
<%@ taglib uri="/struts-tags" prefix="s" %>

<body>
  	<s:actionerror/>    //錯誤提示
  	<s:actionmessage/>
    <s:form action="upfile" method="post" namespace="/" enctype="multipart/form-data">
    	<s:textfield name="txt" label="標題"></s:textfield>
    	<s:file name="file" label="檔案上傳"></s:file>
    	<s:submit value="提交"></s:submit>
    </s:form>
  </body>


//下載JSP頁面
 
  <body>
    <a href="download?id=100001">下載</a>
  </body>