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();
                }
          }
  • 相关阅读:
    Windows Phone 7 日期选择控件DatePicker和时间选择控件TimePicker
    Windows Phone 7 开发小技巧
    Windows Phone 7 MVVM模式的学习笔记
    Windows Phone 7 网络编程之留言板应用
    C# 中的INotifyPropertyChanged和ObservableCollection<T>
    《深入浅出:Windows Phone 7应用开发》
    Windows Phone 7 MVVM模式通讯方式之实现Command
    Windows Phone 7 chart图表编程
    Windows Phone 7 网络编程之调用web service
    Perst嵌入式数据库介绍
  • 原文地址:https://www.cnblogs.com/quanyj/p/3414227.html
Copyright © 2011-2022 走看看