简体   繁体   English

如何在 Spring 引导中发送图像作为响应?

[英]How to send image as response in Spring boot?

I am a beginner with spring boot (restController) and angular I want to send an image to my client(angular) with this method:我是 spring 引导(restController)和 angular 的初学者我想用这种方法向我的客户(角度)发送图像:

@RestController public class ProductRestController { 
    @Autowired private ProductRepository pr;
    @GetMapping (path = "/ photoProduit / {id}", produces = org.springframework.http.MediaType.IMAGE_PNG_VALUE)
     public byte [] getPhoto (@PathVariable Long id) throws IOException { 
        Product p = pr.findById (id) .get (); return Files.readAllBytes (Paths.get (System.getProperty ("user.home") + "/ ecom / produits /" + p.getPhotoName ()));
        
    }
 }

But the URL returns me a code 500 with this error但是 URL 返回一个代码500并出现此错误

There was an unexpected error 
(type = Internal Server Error, status = 500). C: \ Users \ Gonan \ ecom \ products \ unknow.png java.nio.file.NoSuchFileException: C: \ Users \ Gonan \ ecom \ produits \ unknow.png

Can you help me, please??你能帮我吗??

As per the exception you have posted, it looks like the image path you trying to read is not found.根据您发布的异常,看起来您尝试读取的图像路径未找到。

Also there are few corrections to the method, here is how i have done this:该方法也有一些更正,这是我的做法:

import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

@GetMapping(value = "/image", produces = MediaType.IMAGE_JPEG_VALUE)
    public ResponseEntity<Resource> image() throws IOException {
        final ByteArrayResource inputStream = new ByteArrayResource(Files.readAllBytes(Paths.get(
                "/home/silentsudo/Videos/dum111111b.jpg"
        )));
        return ResponseEntity
                .status(HttpStatus.OK)
                .contentLength(inputStream.contentLength())
                .body(inputStream);

    }

Please make sure that the URI to Paths.get(...) is a valid otherwise you will still get java.nio.file.NoSuchFileException请确保Paths.get(...)的 URI 有效,否则您仍然会收到java.nio.file.NoSuchFileException

暂无
暂无

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

相关问题 在Spring启动中异步发送响应 - Send response asynchronously in Spring boot Java Spring Boot:如何响应将重定向html发送到UI - Java Spring Boot: How to response send redirect html to UI 如何在 Spring 引导中发送请求 scope bean 作为响应 - How to send request scope bean as response in Spring Boot Spring Boot:根据结果发送响应头 - Spring Boot: send response header based on result 如何在同一个 POST 请求中发送 JSON 和图像并将其级联? Spring 开机 - How to send JSON and an Image in same POST request and cascade it? Spring Boot 对于 Spring Boot 中的 GET 请求,如何在单个 JSON 响应中发送 Header 中的一些特定信息和 Body 中的一些信息? - How to send some specific info in Header and some info in Body in a single JSON response, for a GET request in Spring boot? 如何临时创建一个没有任何文件位置的文本文件,并在运行时在 spring 启动时作为响应发送? - How to temporarily create a text file without any file location and send as a response in spring boot at run time? 如何在 spring 引导中发生任何约束违反错误时发送自定义响应消息 - How to send customise response message when any constraint violation error occured in spring boot Spring Boot - 如何通过带有查询参数的 url 发送 POST 请求并将响应存储在方法中? - Spring Boot - How to send a POST request via a url with query parameters and store the response inside a method? 如何在 spring 引导中发送 400、401、405、403 和 500 错误的自定义响应? - How to send custom response for 400, 401, 405, 403 and 500 errors in spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM