下載檔案,java下載檔案

來源:互聯網
上載者:User

下載檔案,java下載檔案

二進位下載

public void download(HttpServletRequest request, HttpServletResponse response) throws IOException{                //String filePath = "src/main/webapp/apk/android/NCB.apk";        String filePath=request.getSession().getServletContext().getRealPath("/apk/android/NCB.apk");        File file = new File(filePath);                 /* 如果檔案存在 */        if (file.exists()) {            String fileName = URLEncoder.encode(file.getName(), enc);            response.reset();            ServletContext servletContext=request.getServletContext();            response.setContentType(servletContext.getMimeType(fileName));                      response.addHeader("Content-Disposition", "attachment; filename=" + fileName);            int fileLength = (int) file.length();            response.setContentLength(fileLength);            /* 如果檔案長度大於0 */            if (fileLength > 0) {                /* 建立輸入資料流 */                InputStream inStream = null;                ServletOutputStream outStream = null;                try {                    inStream = new FileInputStream(file);                    byte[] buf = new byte[4096];                    /* 建立輸出資料流 */                    outStream = response.getOutputStream();                    int readLength;                    while (((readLength = inStream.read(buf)) != -1)) {                        outStream.write(buf, 0, readLength);                    }                } finally {                    inStream.close();                    outStream.flush();                    outStream.close();                }            }        }    }

springMVC下載

 public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) 6             throws UnsupportedEncodingException { 7         String path = request.getSession().getServletContext().getRealPath(""); 8         String filename = "模板檔案.xls"; 9         File file = new File(path +  "\\file\\templagte\\" + filename);10         String userAgent = request.getHeader("User-Agent");11         byte[] bytes = userAgent.contains("MSIE") ? filename.getBytes() : filename.getBytes("UTF-8"); // fileName.getBytes("UTF-8")處理safari的亂碼問題12         String fileName = new String(bytes, "ISO-8859-1"); 13         // 設定輸出的格式14         response.setContentType("multipart/form-data");15         response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));16         17         InputStream inStream = null;18         try {19             inStream = new FileInputStream(file);20             IOUtils.copy(inStream, response.getOutputStream());//使用commons-io組件進行檔案流的處理21         } catch (IOException e) {22             e.printStackTrace();23         }finally{24             IOUtils.closeQuietly(inStream);25         }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.