下載

    // 設置爲下載application/x-download
    resp.setContentType("application/x-download");
    // 下載後的文件名稱
    String downloadFileName = "abc.txt";
    // 編碼轉換
    downloadFileName = URLEncoder.encode(downloadFileName, "UTF-8");
    resp.addHeader("Content-Disposition", "attachment;filename="
      + downloadFileName);
    try {
     OutputStream os = resp.getOutputStream();
     FileInputStream fis = new FileInputStream(filePath);
     byte[] b = new byte[1024]
     int i = 0;
     while ((i = fis.read(b)) > 0) {
      os.write(b, 0, i);
     }
     fis.close();
     os.flush();
     os.close();
    }

發佈了10 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章