简体   繁体   English

下载时如何设置文件类型?

[英]How to set file type when it's downloaded?

I have this Spring endpoint which returns file: 我有这个Spring端点返回文件:

@RequestMapping(value = "/files/{merchant_id}", method = RequestMethod.GET)
    public void getFile(@PathVariable("merchant_id") Integer merchant_id, HttpServletResponse response) {

        try {
          File initialFile = new File("/opt/1/Why_Brookfield_Callout_3x.png");
          InputStream is = FileUtils.openInputStream(initialFile);
          org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
          response.flushBuffer();
        } catch (IOException ex) {
          throw new RuntimeException("IOError writing file to output stream");
        }
    }

But when I download the file I get type text. 但是当我下载文件时,我得到的是文本。 How I can set the type of there file to .png file? 如何将那里的文件类型设置为.png文件?

add produces = MediaType.IMAGE_PNG in your @RequestMapping @RequestMapping添加produces = MediaType.IMAGE_PNG

see https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/MediaType.html for more mediatypes 请参阅https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/MediaType.html了解更多媒体类型

So the method definition becomes: 因此,方法定义变为:

@RequestMapping(value = "/files/{merchant_id}", method = RequestMethod.GET, produces = org.springframework.http.MediaType.IMAGE_PNG)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM