我使用上传图片向数据库添加一行。当我查阅行列表时,会出现图像,但是当我尝试更新一行时,我遇到错误“无法加载资源:服务器响应状态为400:spring mvc”。图像没有出现!!
//add row
@RequestMapping(value = "/add")
public String ajouter(@ModelAttribute("serv") Service service ,MultipartFile file) throws Exception {
Long idser;
// add
if (service.getIdService() ==0) {
service.setImgService(file.getOriginalFilename());
idser = metier.addservice(service);
// add new image file
if (!file.isEmpty()) {
String path = System.getProperty("java.io.tmpdir") + "/"
+ idser + "_" + service.getImgService();
file.transferTo(new File(path));
}
}
return "redirect:/page/pageus";
}
// update
@RequestMapping("/edit/{id}")
public ModelAndView editService(@PathVariable("id") long id,Model model,@ModelAttribute Service service){
service=metier.getService(id);
model.addAttribute("editedserv",service);
return new ModelAndView("Admin/page/pageedit","serviceObject",service);
}
// get image of the products
@RequestMapping(value = "Photoser", produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] photoCat(Long idser) throws Exception {
Service serv = metier.getService(idser);
String path = System.getProperty("java.io.tmpdir") + "/" + idser+"_"+serv.getImgService();
File serImage = new File(path);
return IOUtils.toByteArray(new FileInputStream(serImage));
}
//show img in jsp
<img src="Photoser?idser=${serviceObject.idService}"/>
有人可以帮帮我!
答案 0 :(得分:0)
您应该使用js或jquery通过以下内容填充src
, [您的图像字节数组数据] 是一串字节数据,因此您可能需要更改返回类型到控制器中的String
,无论如何都要尝试;)
<img src="data:image/png;base64,[your image byte array data]"/>