@RequestMapping(value = "/all", method = RequestMethod.GET)
public ResponseEntity<List<ItemVO>> listAll() {
ResponseEntity<List<ItemVO>> entity = null;
try {
List<ItemVO> list=service.listAll();
for(ItemVO i : list){
InputStream in = getClass().getClassLoader().getResourceAsStream(i.getFilepath_img());
i.setByte_img(IOUtils.toByteArray(in));
}
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
entity = new ResponseEntity<List<ItemVO>>(list, HttpStatus.OK);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
entity = new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return entity;
}
VO的getResourceAsStream返回NullpointException(spring4)
public class ItemVO{
private int item_id;
private String filepath_img;
private byte[] byte_img;
}
的图像位于的src /主/ web应用/资源/ IMG文件夹,
存储的文件路径是如“/ IMG/XXX巴纽”
我不知道该怎么办 堆栈跟踪:
的java .lang.NullPointerException
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2146)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:721)
你检查过'i.getFilepath_img()'返回路径吗? –
是的,我做得很好 – hsyou
试着用这个路径检查目的'main/webapp/resources/img/xxx.png' –