springboot中根据url地址获取图片

application.yml中,配置路径:
file:
  dir: /mnt/mfs-cli/gf/          //服务器地址
  #dir: E:\\Area\\anhui           //本地地址
 
在Controller文件中,定义地址:
	@Value("${file.dir}")
    private String fileDir;
 
下面是实际应用方法:
@SuppressWarnings({ "rawtypes", "unchecked" })
@ApiOperation(value = "根据图片url获取图片", notes = "根据图片url获取图片")
@GetMapping(value = "getImage")
public void getImage(@ApiParam(value = "图片地址") @RequestParam String url,HttpServletResponse response) {
	FileInputStream inputStream = null;
	OutputStream out = null;
	try {
		log.info("根据图片url获取图片base64");
        //读取本地图片输入流  
        inputStream = new FileInputStream(fileDir + File.separator + url);  
        int i = inputStream.available();  
        byte[] buff = new byte[i];  //byte数组用于存放图片字节数据  
        inputStream.read(buff);  
        response.setContentType("image/*"); //设置发送到客户端的响应内容类型   
        out = response.getOutputStream();  
        out.write(buff);  
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		if (out != null) {  
            try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
        }
		if (inputStream != null) {  
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
        }
	}
}

你可能感兴趣的:(java)