中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

文件下載java實(shí)現(xiàn)代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

1. 通過(guò)FileInputStream讀取文件輸入流

    public void export(String excelPath, String fileName) throws Exception{
        
        //將文件存到指定位置
        //讀取目標(biāo)文件流,轉(zhuǎn)換調(diào)用下載
        File resultFile = new File(excelPath);
        FileInputStream resultFileFi = new FileInputStream(resultFile);
        long l = resultFile.length();
        int k = 0;
        byte abyte0[] = new byte[65000];
        
        // 調(diào)用下載
        response.setContentType("application/x-msdownload");
        response.setContentLength((int) l);
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        while ((long) k < l) {
            int j;
            j = resultFileFi.read(abyte0, 0, 65000);
            k += j;
            response.getOutputStream().write(abyte0, 0, j);
        }
        resultFileFi.close();
        
        //轉(zhuǎn)換成功后,刪除臨時(shí)文件
        resultFile.delete();
    }

2. 注意getServletContext().getMimeType(fileName),讀取文件類(lèi)型

    public void export1(String excelPath, String fileName){
        if (request.getParameter("file") != null) {
            fileName = request.getParameter("file");
        }
        System.out.println(ServletActionContext.getServletContext().getMimeType(fileName));
        response.setContentType(ServletActionContext.getServletContext().getMimeType(fileName));
        response.setHeader("Content-disposition","attachment; filename="+fileName);
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream( new FileInputStream(excelPath) );
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff,0,bytesRead);
            }
         } catch(final IOException e) {
            System.out.println ( "出現(xiàn)IOException." + e );
         } finally {
            if (bis != null)
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (bos != null)
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
         }        
    }


標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:python簡(jiǎn)單爬蟲(chóng)

下一篇:JAVA 比較兩張圖片的相似度的代碼