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");
参考资料:数组