response.setContentType("text/html");
OutputStream out=response.getOutputStream();
//得到文件的在服务器中的实际地址
String filepath=this.getServletContext().getRealPath("/pic/a上海留學生莎莉自拍第一季.jpg");
String filename=filepath.substring(filepath.lastIndexOf("\\")+1);
//如果url路径含有中文,则需要使用URLEncoder对中文进行重新编码
response.setHeader("content-disposition",
"attachment:filename="+URLEncoder.encode(filename,"UTF-8"));
FileInputStream in=new FileInputStream(filepath);
byte data[]=new byte[1024*1024];
in.read(data);
out.write(data);
利用response对象使客户端实现下载
最新推荐文章于 2023-09-07 14:00:49 发布