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

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

问题描述

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



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

显示它们的最简单方法是什么?什么样的servlet /控制器,我需要在JSP上显示图像字节?



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



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

解决方案

最简单的解决方案输入OutputStream或ResponseEntity。



我更喜欢:返回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;
}


b $ b

但还有更多:



直接使用HttpServletResponse

  @RequestMapping(value =/ document / {id} / fileContent,method = RequestMethod.GET)
public void getDocumentFileContent(final HttpServletResponse response,
@PathVariable文档文档)
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());
.setContentType(mimeType);
response.setContentLength(fileInfo.getSize());
response.addHeader(Content-Disposition,attachment; filename = \+ fileName +' );

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


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.

解决方案

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

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

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