springmvc實現圖片下載和在瀏覽器顯示以及nginx代理訪問圖片

1、不在同一臺主機,經過給對方文件的方式java

Controllernginx

@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);

serviceImplapp

@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();
		}
	}

實現文件下載ide

response.setHeader("Content-Disposition", "attachment;fileName=" + "asdf.jpg");

實現文件在瀏覽器顯示
ui

response.setContentType("image/png");

2、經過nginx代理的方式訪問。
代理

nginx.conf配置get

$A2OW@`DJCNT%2Y)XS3[5FC.png

location ~ ^/(img/|images/) {
          root C:/Users/Public/Pictures;
          access_log off;
          expires max;
        }

效果以下:
it

image.png

訪問路徑爲:http://localhost:80/images/aaa.pngio

相關文章
相關標籤/搜索