后台:https://www.cnblogs.com/lucas1024/p/9533220.html
前台:注意不能用ajax获取
<script>
function downloadImage(){
var urlString = "http://pic32.nipic.com/20130829/12906030_124355855000_2.png";
//跳转到后端控制器
location.href="${ctx}/guest/download.do?urlString="+urlString;
}
自己写的
前台
image.png
后台
@GetMapping("/DownloadImg")
@ResponseBody
// File file = new File("C:\\Users\\User\\Desktopimg\\img.png");
public void tempDownLoad(HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException {
// 下载本地文件
String fileName = "Operator.doc".toString(); // 文件的默认保存名
// 读到流中
InputStream inStream = new FileInputStream("C:\\Users\\User\\Desktop\\img.png");// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}