<code id="ymukc"><xmp id="ymukc">

文件下載java實現代碼

usib8630的頭像 usib8630 0 2016-03-05 20:46 0

 基本信息

× 1   

瀏覽數: 27938

分享時間: 2 年 前

11

1. 通過FileInputStream讀取文件輸入流

    public void export(String excelPath, String fileName) throws Exception{
        
        //將文件存到指定位置
        //讀取目標文件流,轉換調用下載
        File resultFile = new File(excelPath);
        FileInputStream resultFileFi = new FileInputStream(resultFile);
        long l = resultFile.length();
        int k = 0;
        byte abyte0[] = new byte[65000];
        
        // 調用下載
        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();
        
        //轉換成功后,刪除臨時文件
        resultFile.delete();
    }

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

    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 ( "出現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();
                }
         }        
    }


666666666666666666
2 年 前 6565

12 [下一頁]

  • 1821466008的頭像 1821466008 2016-12-22 19:38 代碼數:0

    hao

  • cixexey的頭像 cixexey 2018-06-22 23:11 代碼數:0

    Please continue this great work and I look forward to more of your awesome blog posts.

    ????? ?????

  • cixexey的頭像 cixexey 2018-06-24 18:42 代碼數:0

    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.

    Lebensversicherung kündigen

  • cixexey的頭像 cixexey 2018-06-27 18:58 代碼數:0

    Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..appellate attorneys Georgia

  • cixexey的頭像 cixexey 2018-06-30 16:39 代碼數:0

    I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates.virtual dogs game

  • cixexey的頭像 cixexey 2018-06-30 19:18 代碼數:0

    Your blog provided us with valuable information to work with. Each &  every tips of your post are awesome. Thanks a lot for sharing. Keep blogging,facebook

  • cixexey的頭像 cixexey 2018-07-01 22:09 代碼數:0

    I high appreciate this post. It’s hard to find the good from the bad sometimes, but I think you’ve nailed it! would you mind updating your blog with more information? acim

  • cixexey的頭像 cixexey 2018-07-02 16:03 代碼數:0

    This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog..conference

  • cixexey的頭像 cixexey 2018-07-06 05:54 代碼數:0

    I read a article under the same title some time ago, but this articles quality is much, much better. How you do this..new property for sale singapore

  • bebmoo的頭像 bebmoo 2017-04-18 08:59 代碼數:0

    厲害了

     

您的評論:

  
六合特码资料
<code id="ymukc"><xmp id="ymukc">
<code id="ymukc"><xmp id="ymukc">