java下载时不弹出下载弹窗,怎么办!!急需
跟断点都能跟到,运行完就没有反映了,怎么才能弹窗选择下载路径????
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得请求文件名
String fileName = request.getParameter("fileName");
System.out.println(fileName);
//设置文件MIME类型
response.setContentType(getServletContext().getMimeType(fileName));
//设置Content-Disposition
//服务端向客户端游览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt、jpg等,会直接在浏览器中显示,
//如果需要提示用户保存,就要利用Content-Disposition进行一下处理,关键在于一定要加上attachment:
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + fileName);
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}