文件下载java实现代码

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

 基本信息

× 1    浏览数: 29269 分享时间: 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
    厉害了  
您的评论: