public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String id = request.getParameter("id");
String destUrl = "http://www.***.com/Download?id=" +id;
// 建立链接
URL url = new URL(destUrl);
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
// 连接指定的资源
httpUrl.connect();
// 获取网络输入流
BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + id);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
if (id != null) {
BufferedInputStream br = bis;
int len = 0;
while ((len = br.read(buf)) > 0){
out.write(buf, 0, len);
}
br.close();
}
out.flush();
out.close();
}
下载跨域文件,设置IE 或者 火狐 直接下载,而不调用软件直接打开