简体   繁体   中英

Failed to load resource: the server responded with a status of 400 :spring mvc

I add a row to databse with upload image . when i consult the list of rows , the image is appears, but when i try for update a row i encounter the error "Failed to load resource: the server responded with a status of 400 :spring mvc". image not appears!!

//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}"/>

can someone help me !

You should use js or jquery to fill src by following content, [your image byte array data] is a string of byte data, so you may need to change your return type to String in controller, anyway try it;)

<img src="data:image/png;base64,[your image byte array data]"/>

Take a look of embedding base64 images .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM