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

struts2 檔案上傳與下載

檔案上傳:
三種上傳方案
虛擬路徑與真實路徑 /upload
copyFile與copydirectory
檔案上傳的三種方案

  • 1.將上傳的檔案以二進位制的形式存放到資料庫 oa系統 activiti工作流框架
  • 2.將檔案上傳到檔案伺服器(硬碟足夠大)中
  • 3.上傳到tomcat所在的普通Web伺服器中
  • 真實路徑與虛擬路徑的概念
  • 1.所謂真實路徑指的是自己電腦上能夠找到的路徑
  • 2.所謂虛擬,在自己電腦上看不到的,路徑在別人的電腦(tomcat所在地址)上能夠看到的
  1. 內容型別

    response.setContentType(d.getMime());

  2. 設定響應頭

    response.setHeader(“Content-Disposition”,“attachment;filename=” + fileName);//檔名

  3. 處理檔名的中文亂碼

    String fileName = d.getFileName();
    fileName = new String(fileName.getBytes(“utf-8”), “iso8859-1”);

  4. struts2檔案上傳大小設定

    指定允許上傳的檔案最大位元組數。預設值是2097152(2M) 10M=101024

    1024

    <constant name="struts.multipart.maxSize" value="10485760"/>

  5. struts2檔案上傳型別設定

    根據struts2自帶的fileupload攔截器中提供的allowedTypes來進行限制

    <interceptor-ref name="fileUpload">
         <param name="allowedTypes">image/png,image/gif,image/jpeg
         </param></interceptor-ref>
    
  6. 其它

    enctype=“multipart/form-data” method=“post”
    private File file;
    private String fileContentType;
    private String fileFileName;

package src.com.zking.five.interceptor;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.apache.commons.io.FileUtils;

import src.com.zking.fore.web.BaseAction;
import src.com.zking.fore.web.dao.StudentDAO;
import src.com.zking.fore.web.entity.Student;


/**
 * 檔案上傳的三種方案
 * 	1.將上傳的檔案以二進位制的形式存放到資料庫     oa系統   activiti工作流框架
 * 	2.將檔案上傳到檔案伺服器(硬碟足夠大)中
 *  3.上傳到tomcat所在的普通Web伺服器中
 * @author YGUUU
 *
 *真實路徑與虛擬路徑的概念
 *   1.所謂真實路徑指的是自己電腦上能夠找到的路徑
 *   2.所謂虛擬,在自己電腦上看不到的,路徑在別人的電腦(tomcat所在地址)上能夠看到的
 */
public class UploadAction extends BaseAction{
	private StudentDAO sd=new StudentDAO();
	private Student student=new Student();
	
	public File file;//變數名指的是jsp的name屬性,就是你要上傳的檔案   xxx
	public String fileContentType;//xxxContentType
	public String fileFileName;//xxxFileName
	
	private String serverDir="/upload";
	
	
//	public String upload() {
//		System.out.println(fileContentType);
//		System.out.println(fileFileName);
//		String realPath= getRealPath(serverDir+"/"+fileFileName);
//		System.out.println(realPath);
//		try {
//			/**
//			 * 引數1:本地圖片檔案
//			 * 引數2:在伺服器生成的檔案
//			 */
//			FileUtils.copyFile(file, new File(realPath));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return SUCCESS;
//	}

	
	/**
	 * 獲取Linux下的上傳檔案的所在位置
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		return application.getRealPath(path);
	}

//	public String open() {
//		String type="image/jpeg";
//		String name="22-16042915064O11.jpg";
//		response.setContentType(type);
//		response.setHeader("Content-Disposition","attachment;filename=" + name);//檔名
//		/**
//		 * 將遠端的圖片輸出到本地
//		 *    資料來源inputstream:遠端  new File(realPath)
//		 *    目的:輸出到本地的jsp    response.getOutputStream
//		 */
//		String realPath= getRealPath(serverDir+"/"+name);
//		try {
//			FileUtils.copyFile(new File(realPath), response.getOutputStream());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		return null;
//	}
	
	
//	public String download() {
//		String type="image/jpeg";
//		String name="22-16042915064O11.jpg";
//		response.setContentType(type);
//		response.setHeader("Content-Disposition","filename=" + name);//檔名
//		/**
//		 * 將遠端的圖片輸出到本地
//		 *    資料來源inputstream:遠端  new File(realPath)
//		 *    目的:輸出到本地的jsp    response.getOutputStream
//		 */
//		String realPath= getRealPath(serverDir+"/"+name);
//		try {
//			//FileUtils.copyFile(new File(realPath), response.getOutputStream());
//			BufferedInputStream in=new BufferedInputStream(new FileInputStream(new File(realPath)));
//			BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
//			copyStream(in, out);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//	}

	/**
	 * 加快檔案讀取速度
	 * @param in
	 * @param out
	 */
	private void copyStream(BufferedInputStream in,BufferedOutputStream out) {
		byte[] bbuf=new byte[1024];
		int len=0;
		try {
			while((len=in.read(bbuf))!=-1) {
				out.write(bbuf,0,len);
			}
			in.close();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public File getFile() {
		return file;
	}

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

	public String getFileContentType() {
		return fileContentType;
	}

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

	public String getFileFileName() {
		return fileFileName;
	}

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

}

圖片直接顯示,上傳與下載jsp程式碼

<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
 跳轉成功

 <h2>檔案選擇器</h2>
 <form action="${pageContext.request.contextPath }/sy/uploadAction_upload.action" enctype="multipart/form-data" method="post">
		<input type="file" name="file">
		<input type="submit" value="上傳">
 </form>
 <h2>直接開啟圖片</h2>
 <s:url var="openAsUrl" namespace="/sy" action="uploadAction_open.action"></s:url>
 <s:property value="openAsUrl"/>
 <img alt="" src='<s:property value="openAsUrl"/>'>
 
 <h2>另存為(下載)</h2>
 <s:url var="downloadAsUrl" namespace="/sy" action="uploadAction_download.action"></s:url>
 <s:a href="%{#downloadAsUrl}">下載</s:a>
 
</body>
</html>