1. 程式人生 > >Struts2的攔截器與檔案上傳

Struts2的攔截器與檔案上傳

Interceptor
implements Interceptor
extends AbstractInterceptor
與filter的區別:先過filter再過interceptor

1:所有攔截器都使用介面Interceptor ,Action去實現這個介面;

Init()方法:在伺服器起動的時候載入一次,並且只加載一次;

Destroy()方法:當攔截器銷燬時執行的方法;

Interceptor()方法:其中裡邊有一個引數invocation;

		public String intercept(ActionInvocation invocation) throws xception {
		
			System.out.println("呼叫的action類是:"+invocation.getAction().getClass().getName());
			System.err.println("呼叫的action方法是:"+invocation.getProxy().getActionName());
			System.out.println("呼叫的方法是:"+invocation.getProxy().getMethod());
		return invocation.invoke();
	
	}

org.apache.struts2.interceptor.FileUploadInterceptor
檔案上傳:
三種上傳方案
虛擬路徑與真實路徑 /upload
copyFile與copydirectory

檔案上傳的三種方式

  • 1、上傳的圖片以二進位制的形式儲存到資料庫(activiti、jbpm工作流框架)oa系統

  • 2、將檔案上傳到指定伺服器的硬碟

  • 3、將檔案上傳到tomacat所在伺服器

    1. 檔案下載
      另存為
      直接開啟
public class FileAction extends BaseAction{

	private File file;
	private String fileFileName;
	private String fileContentType;
	private String serverDir="/upload";
	
	/**
	 * 檔案上傳
	 * @return
	 * @throws IOException
	 */
	public String upload() throws IOException {
		String realPath=getRealPath(serverDir + "/" + fileFileName);
		System.out.println("路徑"+realPath);
		FileUtils.copyFile(file, new File(realPath));
		return "success";
		
	}
	
	/**
	 * 獲取檔案的真實路徑
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		return this.request.getServletContext().getRealPath(path);
		
	}
	
	
	/**
	 * 直接在頁面開啟圖片
	 * @return
	 * @throws IOException
	 */
	public String openAs() throws IOException {
		String fileName="stationmaster_img.png";
		String fileType="image/png";
		//設定檔案型別
		response.setContentType(fileType);
		response.setHeader("Content-Disposition","filename=" + fileName);
		String realPath=getRealPath(serverDir + "/" + fileName);
		//從伺服器獲取圖片寫到本地
		//FileUtils.copyFile(new File(realPath), response.getOutputStream());
		BufferedInputStream in=new BufferedInputStream(new FileInputStream(realPath));
		BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
		copyBufStream(in, out);
		return null;
		
	}

	/**
	 * 如果沒有匯入FileUtils檔案的jar包就可以用自定義的方法來獲取圖片寫到本地
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	private void copyBufStream(BufferedInputStream in,BufferedOutputStream out) throws IOException {
		byte[] bbuf =new byte[1024];
		int len=0;
		while((len=in.read(bbuf))!=-1) {
			out.write(bbuf, 0, len);
		}
		in.close();
		out.close();
		
	}
	
	/**
	 * 下載圖片
	 * @return
	 * @throws IOException
	 */
	public String saveAs() throws IOException {
		String fileName="stationmaster_img.png";
		String fileType="image/png";
		//設定檔案型別
		response.setContentType(fileType);
		response.setHeader("Content-Disposition","attachment;filename=" + fileName);
		String realPath=getRealPath(serverDir + "/" + fileName);
		//從伺服器獲取圖片寫到本地
		FileUtils.copyFile(new File(realPath), response.getOutputStream());
		return null;
		
	}
	
	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;
	}
	
	
	
}
  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檔案上傳大小設定

  5. struts2檔案上傳型別設定
    根據struts2自帶的fileupload攔截器中提供的allowedTypes來進行限制

    image/png,image/gif,image/jpeg
  6. 其它
    enctype=“multipart/form-data” method=“post”

//在前臺jsp頁面嵌入form表單
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/helloworld.action" method="post">
     <input  type="file"name="uploadImage"><br/>
     <button type="submit">Submit</button>
   </form>