前端代码
location.href="/downLoadUrl"
java代码
/**
* 根据路径下载文件
*
* @param request
* @param res
* @throws Exception
*/
@RequestMapping(value = "/downLoadUrl")
public void downLoadUrl(HttpServletRequest request, HttpServletResponse res) throws Exception {
//根据网络文件地址创建URL
URL url = new URL(路径);
//获取此路径的连接
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
res.setContentType("application/force-download; charset=UTF-8");
res.setHeader("Content-Disposition", "attachment;filename=" + 下载文件名(比如:test.png));
OutputStream out = res.getOutputStream();
IOUtils.copyLarge(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}