使用img标记在JSP中显示Blob

时间:2015-11-25 18:32:25

标签: javascript jquery spring jsp blob

我想用用户图库(上传和显示照片)构建一个webapp。在上传的情况下,我正在使用DropZone.js插件,我猜它工作正常。问题是,我无法在网站上显示我的照片。我不知道问题出在哪里。

控制器方法代码:

@RequestMapping(value = "upload/", method = RequestMethod.POST)
public GenericResponse uploadPhotos(MultipartHttpServletRequest request){
    try {
        Iterator<String> iterator = request.getFileNames();

    while(iterator.hasNext()){
        String uploadedFile = iterator.next();

        MultipartFile file = request.getFile(uploadedFile);

        String fileName = file.getOriginalFilename();
        String mimeType = file.getContentType();
        byte[] bytes = file.getBytes();

        Photo photo = new Photo(fileName, mimeType, bytes);

        userService.addNewPhoto(new Long(1), photo);
        }
    } catch (IOException e) {
            e.printStackTrace();
    }

    return new GenericResponse("success");
}

照片实体类:

public class Photo extends BaseEntity{

    @Lob
    private byte[] file;

    private String mimeType;

    private String fileName;

    public Photo() {

    }

    public Photo(String fileName, String mimeType, byte[] bytes) {
        this.fileName = fileName;
        this.mimeType = mimeType;
        this.file = bytes;
    }

// getters, setters, etc.
}

在* .jsp里面划分:

<div class="ui three column grid" id="galleryGrid">

  <jstl:forEach items="${photos}" var="photo">
    <div class="column">
      <div class="ui image segment">
          <div class="image">
            <img  alt="${photo.getFileName()}" src= "data:${photo.getMimeType()};bytes,${photo.getFile()}"/>
          </div>
      </div>
    </div>
  </jstl:forEach>

</div>

编辑:

@RequestMapping(value = "Gallery/{id}", method = RequestMethod.GET)
public String showGallery(Model model, @PathVariable Long id){
    Collection<Photo> photos = userService.getGalleryPhotos(id);

    model.addAttribute("photos", photos);

    return "Gallery";
}

EDIT2:

enter image description here 谢谢你的帮助。

0 个答案:

没有答案