来自MongoDB的jsp中的Spring显示图像

时间:2014-03-27 08:57:59

标签: mongodb jsp spring-mvc servlets gridfs

我需要从MongoDB GridFS系统获取一个图像,然后将其显示在JSP img标记中。 这是我的代码不起作用:

@RequestMapping(value = "/getPhoto", method = RequestMethod.GET)
public @ResponseBody
void getPhoto(HttpServletRequest request,
        HttpServletResponse response) {
    try {
    System.out.println("getting photo...");
    GridFSDBFile imageForOutput = userFacade.loadProfilePhoto((User) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal());
    BufferedImage image = ImageIO.read(imageForOutput.getInputStream());
    byte[] imageBytes = ((DataBufferByte) image.getData().getDataBuffer()).getData();
    response.setHeader("expires", "0"); 
    response.setContentType("image/jpg");
    response.setContentLength(imageBytes.length);
    OutputStream out = response.getOutputStream();
    out.write(imageBytes, 0, imageBytes.length);
    out.flush();
    out.close();
    return;
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }

首先我得到了GridFSDBFile然后我需要得到byte []。然后我把它写在响应对象中,但我不知道我是否正确地做了。

JSP中的代码如下:

<c:url var="getPhoto" value="/settingsAdmin/getPhoto" />
<div id="preview">
   <img id="imagePreview" src="${getPhoto}" alt="Profile Photo"/>
</div>

最后,正确调用控制器,但错误必须在其中。

提前谢谢

2 个答案:

答案 0 :(得分:1)

最后,我自己找到了一个解决方案,我将其发布,以便其他人可以解决这个问题:

控制器部分

@RequestMapping(value = "/getPhoto", method = RequestMethod.GET)
public @ResponseBody
void getPhoto(HttpServletRequest request,
        HttpServletResponse response) {
    try {
            GridFSDBFile imageForOutput = userFacade.loadProfilePhoto((User) SecurityContextHolder.getContext().getAuthentication()
                    .getPrincipal());
            InputStream is = imageForOutput.getInputStream();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[16384];
            while ((nRead = is.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
            byte[]imagenEnBytes = buffer.toByteArray();


            response.setHeader("Accept-ranges","bytes");
            response.setContentType( "image/jpeg" );
            response.setContentLength(imagenEnBytes.length);
            response.setHeader("Expires","0");
            response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Content-Description","File Transfer");
            response.setHeader("Content-Transfer-Encoding:","binary");

            OutputStream out = response.getOutputStream();
            out.write( imagenEnBytes );
            out.flush();
            out.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block

    }

}

JSP视图

<c:url var="getPhoto" value="/settingsAdmin/getPhoto" />
<div id="preview">
    <img id="imagePreview" src="${getPhoto}"alt="Profile Photo"/>
</div>

谢谢大家的帮助

答案 1 :(得分:0)

试试这个,

@Controller
public class GetImageController {

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

    @Autowired
    ServletContext servletContext;

    @RequestMapping(value="/getImage", method=RequestMethod.GET)
    public void getPhoto(HttpServletRequest request,
            HttpServletResponse response) {
        try {
        System.out.println("getting photo...");

        File image = new File("E:\\path\\to\\image.jpg");
        System.out.println("file exists: "+image.exists());
        // Get content type by filename.
        String contentType = servletContext.getMimeType(image.getName());

        // Init servlet response.
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType(contentType);
        response.setHeader("Content-Length", String.valueOf(image.length()));
        response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");

        // Prepare streams.
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            // Open streams.
            input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

            // Write file contents to response.
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            // Gently close streams.
            close(output);
            close(input);
        }
        // Check if file is actually an image (avoid download of other files by hackers!).
        // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
        if (contentType == null || !contentType.startsWith("image")) {
            // Do your thing if the file appears not being a real image.
            // Throw an exception, or send 404, or show default/warning image, or just ignore it.
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }

    }

    // Helpers (can be refactored to public utility class) ----------------------------------------

    private static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                // Do your thing with the exception. Print it, log it or mail it.
                e.printStackTrace();
            }
        }
    }
}

并在jsp渲染图像中:

<c:url value="/getImage" var="imgUrl"/>
<img alt="my image" src="${imgUrl}"> //to display on browser
<a href="${imgUrl}">img</a> //to display in new window

<子> 注意:这不是在Spring MVC中显示图像的更好方法,但在Servlets中


其他方式是(两行代码):

返回直接byte[]并在produces = MediaType.IMAGE_JPEG_VALUE中指定@RequestMapping 像:

@RequestMapping(value="/getImage", method=RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
    public @ResponseBody byte[] getPhoto() throws IOException {
        System.out.println("getting photo...");

        FileInputStream image = new FileInputStream(new File("E:\\path\\to\\image.jpg"));
        return IOUtils.toByteArray(image);        
    }

这两种方式都适合我。