Spring MVC 图像控制器,在 JSP 中显示图像字节 [英] Spring MVC Image Controller, to display Image Bytes in JSP

查看:36
本文介绍了Spring MVC 图像控制器,在 JSP 中显示图像字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring MVC 应用程序,其中一个 JSP 必须显示来自数据库的图像.

I have a Spring MVC application, and one of the JSPs must show images coming from a database.

图像作为 Blob 存储在数据库中.

The images are stored in the DB as Blobs.

显示它们的最简单方法是什么?我需要什么样的 servlet/控制器在 JSP 上显示图像字节?应该是一个简单的问题,但我一直无法在任何地方找到完整的解决方案.

What is the easiest way to display them? What kind of servlet/controller do I need to show on the JSP the image bytes? Should be an easy question, but I haven't been able to find a full solution anywhere.

我的理解是我需要一个单独的控制器,比如 ImgController/id=,它将根据请求参数显示图像字节,然后在我的 JSP 中我可以有 img src="ImgController/id...".但是我如何实现和连接这个控制器?

My understanding is that I need a separate controller, say ImgController/id=, which will display the image bytes based on a request parameter, and then in my JSP I can have img src="ImgController/id...". But how do I implement and wire this controller?

任何帮助或示例将不胜感激.非常感谢.

Any help or example would be really appreciated. Thanks a lot.

推荐答案

最简单的解决方案(在尽可能少写的方面)是使用返回类型为 OutputStream 或 ResponseEntity 的 Spring MVC 控制器方法.

The simplest solution (in terms so writing as less as possible) is a Spring MVC Controller Method with return Type OutputStream or ResponseEntity.

我更喜欢:返回一个ResponseEntity:

I prefer: return a ResponseEntity:

@RequestMapping(value = "/reportTemplate/{id}/content", method = RequestMethod.GET)
public ResponseEntity<byte[]> downloadReportTemplateContent(
        @PathVariable("id") final ReportTemplate reportTemplate)
        throws IOException {
    ReportDatei file = reportTemplate.getFile();

    String fileName = reportTemplate.getName() + EXCEL_FILE_NAME_ENDING;
    HttpHeaders responseHeaders = httpHeaderExcelFileAttachment(fileName,
                                    datei.getDaten().length);
    return new ResponseEntity<byte[]>(file.getDataArray(),
                                      responseHeaders, HttpStatus.OK);
}


public static HttpHeaders httpHeaderExcelFileAttachment(final String fileName,
        final int fileSize) {
    String encodedFileName = fileName.replace('"', ' ').replace(' ', '_');

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.parseMediaType("application/vnd.ms-excel"));
    responseHeaders.setContentLength(fileSize);
    responseHeaders.set("Content-Disposition", "attachment");
    responseHeaders.add("Content-Disposition", "filename="" + encodedFileName + '"');
    return responseHeaders;
}

但还有更多:

直接使用HttpServletResponse

Use HttpServletResponse directly

@RequestMapping(value = "/document/{id}/fileContent", method = RequestMethod.GET)
    public void getDocumentFileContent(final HttpServletResponse response,
            @PathVariable("id") final Document document)
            throws IOException {
        FileContentUtil.writeFileContentToHttpResponse(document.getFile(),
                        response, this.fileService);
    }

    public static void writeFileContentToHttpResponse(final CommonFileInfo fileInfo,
              final HttpServletResponse response,
              final FileService fileService) throws IOException {
        String mimeType = fileInfo.getMimeType() != null ? fileInfo.getMimeType() : CommonFileInfo.DEFAULT_MIME_TYPE;
        String fileName = fileInfo.getOriginalName().replace('"', ' ');

        FileContent fileContent = fileService.loadFileContent(fileInfo.getFileContentBusinessId());
        response.setContentType(mimeType);
        response.setContentLength(fileInfo.getSize());
        response.addHeader("Content-Disposition", "attachment; filename="" + fileName + '"');

        response.getOutputStream().write(fileContent.getContent());
        response.getOutputStream().flush();
    }

这篇关于Spring MVC 图像控制器,在 JSP 中显示图像字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆