java如何实现下载弹出的对话框

如题,我想实现一个功能,就是在一个jsp页面上,有一个下载按钮,我点击后,总是下载到默认位置。但是,我想实现一点下载,会弹出一个窗口可以选下载路径,就像我在任何一个界面上右键点击“另存为...”之后会弹出一个对话框让我选择下载路径一样。我用这样的不对。请问我应该加什么东西,才能实现这个效果?求大神们指点我,谢谢?-0-#我是用超链接

Java实现点击下载文件的时候,弹出“另存为”对话框,选择保存位置,然后下载,代码如下:

public void downLoad(String filePath, HttpServletResponse response) 
throws Exception { 
System.out.println("filePath"+filePath); 
File f = new File(filePath); 
if (!f.exists()) { 
response.sendError(404, "File not found!"); 
return; 

BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); 
byte[] buf = new byte[1024]; 
int len = 0; 
response.reset(); 
response.setContentType("application/x-msdownload"); 
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName()); 
OutputStream out = response.getOutputStream(); 
while ((len = br.read(buf)) > 0) out.write(buf, 0, len); 
br.close(); 
out.close(); 
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-05
public void downLoad(String filePath, HttpServletResponse response) throws Exception { System.out.println("filePath"+filePath); File f = new File(filePath); if (!f.exists()) { response.sendError(404, "File not found!"); return; } BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); byte[] buf = new byte[1024]; int len = 0; response.reset(); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + f.getName()); OutputStream out = response.getOutputStream(); while ((len = br.read(buf)) > 0) out.write(buf, 0, len); br.close(); out.close(); }

希望采纳本回答被提问者采纳
第2个回答  2019-02-18
javascript中有个window对象,它的open方法,window.open("这里面是你需要下载的文件的地址")
记得要在响应头里添加
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename=" + printName);