各位大佬帮忙看一下,这么写不能下载吗?
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","下载失败");
}
}