1. 程式人生 > >java檔案流方式下載檔案

java檔案流方式下載檔案

該方式Java中基本上都通用,主要4個步驟,下面的ServletContext物件可以不用,可以根據自己的方式來。
這裡我使用的是SpringMVC,不過它在這裡的唯一用途就是用來獲取ServletContext物件,這個物件的用途,下面例項中有說明

需要用到兩個jar包: commons-fileupload.jar 和 commons-io.jar

程式碼如下:

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import
org.springframework.web.context.ServletContextAware; import javax.servlet.ServletContext; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; @Controller public class FileController implements ServletContextAware{ //Spring這裡是通過實現ServletContextAware介面來注入ServletContext物件
private ServletContext servletContext; @Override public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } @RequestMapping("file/download") public void fileDownload(HttpServletResponse response){ //獲取網站部署路徑(通過ServletContext物件),用於確定下載檔案位置,從而實現下載
String path = servletContext.getRealPath("/"); //1.設定檔案ContentType型別,這樣設定,會自動判斷下載檔案型別 response.setContentType("multipart/form-data"); //2.設定檔案頭:最後一個引數是設定下載檔名(假如我們叫zms.jpg,這裡是設定名稱) response.setHeader("Content-Disposition", "attachment;fileName="+"zms.jpg"); ServletOutputStream out=null; FileInputStream inputStream=null; //通過檔案路徑獲得File物件(假如此路徑中有一個 zms.jpg 檔案) File file = new File(path + "download/" + "zms.jpg"); try { inputStream = new FileInputStream(file); //3.通過response獲取ServletOutputStream物件(out) out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[512]; while (b != -1){ b = inputStream.read(buffer); if(b != -1){ out.write(buffer,0,b);//4.寫到輸出流(out)中 } } } catch (IOException e) { e.printStackTrace(); }finally{ try{ if(inputStream!=null){ inputStream.close(); } if(out!=null){ out.close(); out.flush(); } }catch (IOException e) { e.printStackTrace(); } } } }

第二種方式,直接通過response得到輸出流

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @RequestMapping(value="tempDownLoad")
    public void tempDownLoad(HttpServletRequest request,HttpServletResponse response){

        InputStream in = null;
        try{
            //1.設定檔案ContentType型別,這樣設定,會自動判斷下載檔案型別  
            response.setContentType("multipart/form-data");  
            //2.設定檔案頭:最後一個引數是設定下載檔名
            response.setHeader("Content-Disposition", "attachment;fileName=zms.jpg"); 

            //通過檔案路徑獲得File物件(假如此路徑中有一個 zms.jpg 檔案)  
            File file = new File("E:/template/zms.jpg");  

            in = new FileInputStream(file);  

            //3.通過response獲取ServletOutputStream物件(out)  
            int b = 0;  
            byte[] buffer = new byte[512];  
            while (b != -1){  
               b = in.read(buffer);
               if(b != -1){
                    response.getOutputStream().write(buffer,0,b);//4.寫到輸出流(out)中   
               }

            }  
        } catch (Exception e) {  
        }finally{
            try {
                if (in != null) {
                    in.close();
                }
                response.getOutputStream().flush();
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("關閉檔案IOException!");
            }
        }

    }