在Spring Boot中,可以使用以下步骤将图片批量传输给前端:

  1. 在Spring Boot项目的resources目录下创建一个static文件夹,用于存放图片文件。
  2. 将需要传输的图片文件放入static文件夹中。
  3. 在Spring Boot的Controller中创建一个接口,用于获取图片文件的路径。
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.nio.file.Files;
    
    @RestController
    @RequestMapping("/images")
    public class ImageController {
    
        @GetMapping("/{filename}")
        public ResponseEntity<byte[]> getImage(@PathVariable("filename") String filename) throws IOException {
            Resource resource = new ClassPathResource("static/" + filename);
            byte[] imageBytes = Files.readAllBytes(resource.getFile().toPath());
            return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(imageBytes);
        }
    }
    
  4. 在前端页面中使用该接口获取图片文件的路径,并将图片显示出来。
    <img src="/images/{filename}" alt="image">
    
    其中{filename}是具体的图片文件名。

当访问/images/{filename}接口时,将返回对应图片文件的字节数组,并设置响应头的Content-Type为image/jpeg,前端页面会根据该响应内容显示图片。

请确保图片文件存在于static文件夹中,并且文件名在前端页面中正确指定

spring boot 怎么批量的把图片传给前端

原文地址: https://www.cveoy.top/t/topic/iki6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录