Springboot 注解实现读取目录下的图片并返回到前端

produces = MediaType.IMAGE_JPEG_VALUE

 

@ResponseBody
@GetMapping(value="getimage/{fileId}",produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] getImg(HttpServletResponse response, @PathVariable("fileId") Long fileId){
    //fileId 是自己的业务ID,用来查询相关文件的
   
   File image = new File(applyAttachment.getPath()+applyAttachment.getServerName());
   FileInputStream inputStream = null;
   try {
      inputStream = new FileInputStream(image);
      int length = inputStream.available();
      byte bytes[] = new byte[length];
      response.setContentLength(length);
      inputStream.read(bytes);
      return bytes;
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }
   return null;
}

 

你可能感兴趣的:(Spring,java,spring)