img src地址 以请求的方式来显示图片

img标签

java部分

// 获取图片信息
@RequestMapping(value = "/img",method = RequestMethod.GET)
public String img(HttpServletRequest request, HttpServletResponse response, HttpSession session){
	ServletOutputStream outputStream = null;
	FileInputStream inputStream = null;
	try {
		String id = request.getParameter("id");
		if(ToolsUtil.isEmpty(id)) return null;
		Satellite satellite = liveService.getSatelliteById(id);
		if(satellite == null) return null;
                String path = satellite.getPngfilepath();
		File file = new File(path);
		if(!file.exists()) return null;
		inputStream = new FileInputStream(file);
		response.setContentType("multipart/form-data");
		outputStream = response.getOutputStream();
		//读取文件流
		int len = 0;
		byte[] buffer = new byte[1024 * 10];
		while ((len = inputStream.read(buffer)) != -1){
			outputStream.write(buffer,0,len);
		}
		outputStream.flush();
		outputStream.close();
		inputStream.close();
	}
	catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

然后,图片就可以正常显示啦~

你可能感兴趣的:(img src地址 以请求的方式来显示图片)