JAVA读取本地文件并显示到页面中

javaweb图片的显示

@RequestMapping(value="showImg")
@ResponseBody
public void ShowImg(HttpServletRequest request,HttpServletResponse response) throws IOException{
//String imgFile = request.getParameter("imgFile"); //文件名
// String path= UrlUtil.getValue("goodsImg");//这里是存放图片的文件夹地址
String path = ConfigUtil.getValue("file_upload_url");
String imgFile = "无标题.png";
FileInputStream fileIs=null;
try {
fileIs = new FileInputStream(path+"/"+imgFile);
} catch (Exception e) {
Logger.error(this.getClass(), e);
return;
}
int i=fileIs.available(); //得到文件大小
byte data[]=new byte[i];
fileIs.read(data); //读数据
response.setContentType("image.png"); //设置返回的文件类型
OutputStream outStream=response.getOutputStream(); //得到向客户端输出二进制数据的对象
outStream.write(data); //输出数据
outStream.flush();
outStream.close();
fileIs.close();
}


var src = address + "/basevalue/showImg";
$("#logo").attr("src",src); 

你可能感兴趣的:(JAVA)