zoukankan      html  css  js  c++  java
  • Java文件下载代码

    public static void DownLoadFile(String filePath, String fileName,
                      HttpServletResponse response) throws Exception {
                System.out.println("filepath:" + filePath);
                File file = new File(filePath);
                if (!file.exists()) {
                      System.out.println("文件不存在");
                } else {
                      FileInputStream fis = new FileInputStream(file);
                      BufferedInputStream bis = new BufferedInputStream(fis);
     
                      OutputStream os = response.getOutputStream();
                      BufferedOutputStream bos = new BufferedOutputStream(os);
     
                      fileName = URLEncoder.encode(fileName, "UTF-8");
                      fileName = new String(fileName.getBytes("UTF-8"), "GBK");
     
                      response.reset();
                      response.setContentType("UTF-8");
                      response.setContentType("Application/x-msdownload");
                      response.setHeader("Content-Disposition", "attachment;filename="
                                  + fileName);
                      response.setHeader("Content-Length", String
                                  .valueOf(bis.available()));
     
                      int bytesRead = 0;
                      byte[] buffer = new byte[1024];
                      while ((bytesRead = bis.read(buffer)) != -1) {
                            bos.write(buffer, 0, bytesRead);
                      }
                      bos.flush();
                      bos.close();
                      bis.close();
     
                      os.close();
                      fis.close();
                }
          }
  • 相关阅读:
    百度云推送
    web请求报出 “超过了最大请求长度” 【注意:重启IIS】
    页面多个Jquery版本共存的冲突问题,解决方法!
    Web Api 中使用 PCM TO WAV 的语音操作
    Web Api 如何做上传文件的单元测试
    那些年收集的前端学习资源
    原创: 做一款属于自己风格的音乐播放器 (HTML5的Audio新特性)
    Web Api 接口文档制作
    如何在Asp.Net WebApi接口中,验证请求参数中是否携带token标识!
    JavaScript 面试题,给大家补补基础,加加油,埋埋坑!
  • 原文地址:https://www.cnblogs.com/quanyj/p/3414227.html
Copyright © 2011-2022 走看看