java 實現對指定目錄的檔案進行下載

來源:互聯網
上載者:User

標籤:設定   type   建立   線路圖   ica   span   http   div   tty   

@RequestMapping("/exportDocument")    @ResponseBody    public void exportDocument(HttpServletRequest request,HttpServletResponse response) throws IOException {            XWPFDocument xdoc = null;        FileInputStream is = null;        OutputStream out=null;         try {            String wordName="數聚空港2.0使用手冊.docx";            wordName = new String(wordName.getBytes(), "iso8859-1");           // File file = new File("/root/usersGuide.docx");             response.setContentType("APPLICATION/OCTET-STREAM");            response.setHeader("Content-Disposition", "attachment; filename="                    + wordName);            is = new FileInputStream("/root/usersGuide.docx");            out=response.getOutputStream();//獲得一個output對象            xdoc = new XWPFDocument(is);            } catch (IOException e) {                e.printStackTrace();            }finally{                try {                    xdoc.write(out);//Write out this document to an Outputstream.                    is.close();                    out.close();                } catch (IOException e) {                    e.printStackTrace();                }            }                 /*上述是都word文檔的下載*/

/*下面是對各種類型的檔案下載*/ /*//2.擷取要下載的檔案名稱 String fileName = "test.png"; //3.設定content-disposition回應標頭控制瀏覽器以下載的形式開啟檔案 response.setHeader("content-disposition", "attachment;filename="+fileName); //4.擷取要下載的檔案輸入資料流 InputStream in=null; OutputStream out=null; try {//擷取要下載的檔案的絕對路徑 in=new FileInputStream("/root/運營線路圖0.3.5版.png"); int len = 0; //5.建立資料緩衝區 byte[] buffer = new byte[1024]; //6.通過response對象擷取OutputStream流 out = response.getOutputStream(); //7.將FileInputStream流寫入到buffer緩衝區 while ((len = in.read(buffer)) > 0) {//in.read(byte[] b)最多讀入b.length個位元組 在碰到流的結尾時 返回-1 //8.使用OutputStream將緩衝區的資料輸出到用戶端瀏覽器 out.write(buffer,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ in.close(); out.close(); }*/ }

 

java 實現對指定目錄的檔案進行下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.