一、不在同一台主机,通过给对方文件的方式
Controller
@RequestMapping(value = "/down/{uuid}", method = RequestMethod.GET)
@ResponseBody
public void downloadImage(@PathVariable("uuid") final String uuid, HttpServletResponse response) {
flService.downImage(uuid, response);
}
service
void downImage(String uuid, HttpServletResponse response);
serviceImpl
@Override
public void downImage(String uuid, HttpServletResponse response) {
//response.setHeader("Content-Disposition", "attachment;fileName=" + "asdf.jpg"); //下载
response.setContentType("image/png");//显示
String realPath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\asdf.jpg";
InputStream fileInputStream;
try {
fileInputStream = new FileInputStream(realPath );
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(fileInputStream, outputStream);
outputStream.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
实现文件下载
response.setHeader("Content-Disposition", "attachment;fileName=" + "asdf.jpg");
实现文件在浏览器显示
response.setContentType("image/png");
二、通过nginx代理的方式访问。
nginx.conf配置
location ~ ^/(img/|images/) {
root C:/Users/Public/Pictures;
access_log off;
expires max;
}
效果如下:
转载于:https://blog.51cto.com/1385903/2067365