response.getOutputStream()
Content-Type
: 傳遞給客戶端的文件的 MIME 類型;Content-Disposition:attachment;filename=xxx
:// 下載 public class DownloadServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException,IOException{ String filename = "F:浮誇.mp3"; //根據文件名獲取 MIME 類型 String contentType = this.getServletContext().getMimeType(filename); String contentDisposition = "attachment;filename=a.mp3"; // 輸入流 FileInputStream input = new FileInputStream(filename); // 設置頭 resp.setHeader("Content-Type",contentType); resp.setHeader("Content-Disposition",contentDisposition); // 獲取綁定了客戶端的流 ServletOutputStream output = resp.getOutputStream(); // 把輸入流中的數據寫入到輸出流中 IOUtils.copy(input,output); input.close(); } }
filename = new String(filename.getBytes("GBK"),"ISO-88859-1");
參考資料:數組