gang_qi_xi_2 2019-09-10 09:15 采纳率: 52.6%
浏览 219
已采纳

用如下方法JSP中为什么不能下载图片?

各位大佬帮忙看一下,这么写不能下载吗?

jsp:http://www.xxxx.com/xxx/DownloadServlet?\\iZ7u58anz6bwr6Z\visitimagenew\2019090713313015656819640920769762952299.jpg
servlet:protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String path=req.getParameter("filepath");
String filename="image";
File file=new File(path+filename);
if(file.exists()){
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition","attachment;filename=\""+filename+"\"");
InputStream inputStream=new FileInputStream(file);
ServletOutputStream outputStream=resp.getOutputStream();
byte b[]=new byte[1024];
int n;
while((n=inputStream.read(b))!=-1){
outputStream.write(b,0,n);
}
outputStream.close();
inputStream.close();
}else{
req.setAttribute("erroeResult","下载失败");
}

}
  • 写回答

2条回答 默认 最新

  • threenewbee 2019-09-10 09:22
    关注

    反斜杠写在参数里,要用url编码转义

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部