//jsp
<a href="download?filename=test.jpg">download</a>
//servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.addHeader("content-type", "application/octet-stream");
String filename = request.getParameter("filename");
response.addHeader("content-Disposition", "attachment;filename=" + filename);
System.out.println("filename=" + filename);
InputStream in = getServletContext().getResourceAsStream("download/" + filename);
ServletOutputStream out = response.getOutputStream();
byte[] bs = new byte[10];
int len = -1;
while((len = in.read(bs)) != -1)
{
out.write(bs,0,len);
}
out.close();
in.close();
}
文件名中文乱码解决
URLEncoder.encode(filename, "utf-8")
网友评论