Spring MVC 图像控制器,用于在 JSP 中显示图像字节

发布于 2024-11-19 05:07:55 字数 315 浏览 1 评论 0原文

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

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

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

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

任何帮助或例子将非常感激。多谢。

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

The images are stored in the DB as Blobs.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-11-26 05:07:55

最简单的解决方案(尽可能少写)是带有返回类型 OutputStream 或 ResponseEntity 的 Spring MVC 控制器方法。

我更喜欢:返回一个 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

@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();
    }

The simplest solution (in terms so writing as less as possible) is a Spring MVC Controller Method with return Type OutputStream or 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;
}

But there are many more:

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();
    }
诠释孤独 2024-11-26 05:07:55

我在这里编译了一些代码

请查看并告诉我是否适合您的情况。我认为这应该有效。

I have compiled some code here!

Please have a look and let me know if that fits your case. I think this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文