Servlet 如何实现文件下载:
PrintWriter out = response.getWriter();
//不管实际类型,待下载文件 ContentType 统一指定为 application/octet-stream
response.setContentType("application/octet-stream");
//中文文件名必须转码为 ISO8859-1,否则为乱码
String fileName = new String("文本文件.txt".getBytes(), "ISO8859-1");
//作为附件下载,相应的 "inline;filename = "+fileName 是在线(浏览器中显示内容)打开
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//因为文件编码也为 ISO8859-1,所以内容须转码成 ISO8859-1,尚不知如何控制下载文本文件的编码
//或有谁知道的,还请告诉我一下。 文件内容可以从物理文件中来,或者数据库中读取填入等等
out.write(new String("Servlet 文件下载测试".getBytes(), "ISO8859-1"));
out.close();
PrintWriter out = response.getWriter();
//不管实际类型,待下载文件 ContentType 统一指定为 application/octet-stream
response.setContentType("application/octet-stream");
//中文文件名必须转码为 ISO8859-1,否则为乱码
String fileName = new String("文本文件.txt".getBytes(), "ISO8859-1");
//作为附件下载,相应的 "inline;filename = "+fileName 是在线(浏览器中显示内容)打开
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//因为文件编码也为 ISO8859-1,所以内容须转码成 ISO8859-1,尚不知如何控制下载文本文件的编码
//或有谁知道的,还请告诉我一下。 文件内容可以从物理文件中来,或者数据库中读取填入等等
out.write(new String("Servlet 文件下载测试".getBytes(), "ISO8859-1"));
out.close();