標籤:設定 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 實現對指定目錄的檔案進行下載