java.nio.file.nosuchfileexception:c:\ users \ pc \ desktop \ desktop \

发布于 2025-02-10 16:55:50 字数 725 浏览 1 评论 0原文

我正在尝试将PNG文件分配给数据库中的所有产品,Spring Boot不识别我分配了我分配的值“ unknown.png”的文件或

每个产品的photOname属性,即文件名与此值相同,确实是一个PNG文件,我尝试更改路径的格式,然后尝试将toString()添加到路径上,但是当我打开Localhost:8000/时,我会得到相同的例外。光产物/1应该显示图像而不是

RestController如下:

@RestController
public class CatalogueRestController {

    @Autowired
    private ProductRepository productRepository;
    
     @GetMapping(path="/photoProduct/{id}",produces = MediaType.IMAGE_PNG_VALUE)
        public byte[] getPhoto(@PathVariable("id") Long id) throws Exception{
            Product p=productRepository.findById(id).get();
            return Files.readAllBytes(Paths.get("C:/Users/PC/Desktop/info1/ecom/products/unknown.png"));
        }
    
}

谢谢

I'm trying to assign a png file to all the products in my database, Spring boot doesn't recognize either the path or the file

I've assigned the value "unknown.png" to the photoName attribute of every Product, the filename is the same as this value and it is indeed a PNG file, I tried to change the format of the path, and I tried to add a ToString() to the path, but I get the same exception when I open localhost:8000/photoProduct/1 when it should display the image instead

The RestController is as follows:

@RestController
public class CatalogueRestController {

    @Autowired
    private ProductRepository productRepository;
    
     @GetMapping(path="/photoProduct/{id}",produces = MediaType.IMAGE_PNG_VALUE)
        public byte[] getPhoto(@PathVariable("id") Long id) throws Exception{
            Product p=productRepository.findById(id).get();
            return Files.readAllBytes(Paths.get("C:/Users/PC/Desktop/info1/ecom/products/unknown.png"));
        }
    
}

Thank you in advance

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

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

发布评论

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

评论(1

我恋#小黄人 2025-02-17 16:55:50

我已经用\\\而不是/进行了测试。

@GetMapping(path="/photoProduct/{id}", produces = MediaType.IMAGE_PNG_VALUE)
public byte[] getPhoto(@PathVariable("id") Long id) throws IOException {
    Product p = productRepository.findById(id).get();
    return Files.readAllBytes(Paths.get("C:\\Users\\User\\ecom\\products\\"+p.getPhotoName()));
}

I have tested with \\\ instead of / and it works.

@GetMapping(path="/photoProduct/{id}", produces = MediaType.IMAGE_PNG_VALUE)
public byte[] getPhoto(@PathVariable("id") Long id) throws IOException {
    Product p = productRepository.findById(id).get();
    return Files.readAllBytes(Paths.get("C:\\Users\\User\\ecom\\products\\"+p.getPhotoName()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文