利用response对象使客户端实现下载

来源:互联网 发布:水晶相册制作淘宝 编辑:程序博客网 时间:2024/04/28 10:27
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);

原创粉丝点击