1. 程式人生 > >struts攔截器和檔案上傳

struts攔截器和檔案上傳

攔截器:
Interceptor
    implements Interceptor    (com.opensymphony.xwork2.interceptor.Interceptor)
      extends AbstractInterceptor  (com.opensymphony.xwork2.interceptor.AbstractInterceptor)
    與filter的區別:先過filter再過interceptor

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

    
    0. 檔案下載
   另存為
   直接開啟

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=10*1024*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 com.zking.five.interceptor;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.coyote.http11.filters.BufferedInputFilter;

import com.zking.four.web.BaseAction;

/**
 * 檔案上傳的三種方案:
 *     1.將上傳的檔案以二進位制的形式存放到資料庫        oa系統        activiti工作流程
 * @author Machenike
 * 
 *     2.將檔案上傳到檔案伺服器(硬碟足夠大)中
 * 
 *     3.將檔案傳送到tomcat所在的普通web伺服器
 * 
 * 
 * 真實路徑與虛擬路徑的概念
 *         1,所謂真實路徑指的是在自己電腦上能夠找到的路徑
 *         2,所謂虛擬,在自己的電腦上看不到,在別人電腦(tomcat所在位置伺服器)上能夠看到
 *
 *
 */

public class UploadAction extends BaseAction{
    private File file;//變數名指的是jsp中檔案選擇器的name屬性, 就是你要上傳的檔案         xxx
    private String fileContentType;//xxxContentType;    檔案型別
    private String fileFileName;//xxxFileName        檔名稱
    
    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;
    }

    private String serverDir = "/upload";
    

    /**
     * 下載檔案的方法
     * @return
     */
    public String upload() {
        String realPath = serverDir + "/" +fileFileName;
        /*
         * 引數1:本地要下載的圖片檔案
         * 
         * 引數2: 在伺服器生成的檔案
         */
        System.out.println(fileFileName);
        System.out.println(fileContentType);
//        System.out.println(application.getRealPath(realPath));
//        System.out.println(getRealPath(realPath));
        try {
            FileUtils.copyFile(file, new File(getRealPath(realPath)));
//            File file2 = new File(getRealPath(realPath));
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return SUCCESS;
    }
    
    
    /**
     * 設定緩衝流的檢視圖片  優化
     * @param bos
     * @param bis
     */
    public void CopyFile(BufferedOutputStream bos,BufferedInputStream bis) {
        byte[] b = new byte[1024];
        int len = 0;
        try {
            while((len=bis.read(b))!=-1) {
                bos.write(b,0,len);
            }
            bis.close();
            bos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 獲取Linux下的上傳檔案的所在位置
     * @param path
     * @return
     */
    private String getRealPath(String path) {
        // TODO Auto-generated method stub
        System.out.println("path:"+path);
        return application.getRealPath(path);
    }

    /**
     * 在本地檢視tomcat伺服器上的圖片
     * @return
     */
    public String openAs() {
        String name = "1.jpg";
        String type = "image/jpg";
        String realPath = serverDir + "/" +name;
        response.setContentType(type);
        response.setHeader("Content-Disposition","filename=" + name);//檔名
        
        try {
//            FileUtils.copyFile(new File(getRealPath(realPath)), response.getOutputStream());
            FileInputStream fileInputStream = new FileInputStream(new File(getRealPath(realPath)));
            CopyFile(new BufferedOutputStream(response.getOutputStream()),new BufferedInputStream(fileInputStream));
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 下載檔案
     * @return
     */
    public String download() {
        String name = "1.jpg";
        String type = "image/jpg";
        String realPath = serverDir + "/" +name;
        response.setContentType(type);
        response.setHeader("Content-Disposition","attachment;filename=" + name);//檔名
        
        try {
            FileUtils.copyFile(new File(getRealPath(realPath)), response.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    

    

}