http http:// localhost:8080/products/download/image001.png的http失败

发布于 2025-02-09 20:58:51 字数 2909 浏览 3 评论 0原文

大家好,我正在构建一个带有弹簧启动和Angular的应用程序,即IM在服务器中上传文件,并且在尝试下载此文件时会遇到此错误“错误:SyntaxError:意外令牌 - 在JSON.PARSE的位置0处的JSON in json in JSON.PARSE”。 这是我的代码的一部分

我的组件

downloadProduct(fileUrl){
    this.httpClientService.downProduct(fileUrl).subscribe(response => {
      window.open(response.url, '_blank');
    }
    )
  }

我的服务

public downProduct(fileUrl: string | undefined): Observable<any>{
    return this.httpClient.get(this.urlBack+"products/download/"+fileUrl, {
      responseType: 'json'
    });
  }

和我的弹簧控制器

@CrossOrigin(origins = "http://localhost:4200")
    @GetMapping("download/{filename}")
    public ResponseEntity<Resource> downloadFiles(@PathVariable("filename") String filename) throws IOException {
        System.out.println("weeeeeesh");
        Path filePath = get(DIRECTORY).toAbsolutePath().normalize().resolve(filename);
        if(!Files.exists(filePath)) {
            throw new FileNotFoundException(filename + " was not found on the server");
        }
        Resource resource = new UrlResource(filePath.toUri());
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("File-Name", filename);
        httpHeaders.add(CONTENT_DISPOSITION, "attachment;File-Name=" + resource.getFilename());
        System.out.println(resource.getFilename());
        return ResponseEntity.ok().contentType(MediaType.parseMediaType(Files.probeContentType(filePath)))
                .headers(httpHeaders).body(resource);

    }

这是控制台中显示的错误

SyntaxError: Unexpected token � in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9799:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:83895:33) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9798:60) at Zone.runTask (http://localhost:4200/polyfills.js:9571:47) at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:9880:34) at invokeTask (http://localhost:4200/polyfills.js:10993:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:11030:21)
message: "Unexpected token � in JSON at position 0"
stack: "SyntaxError: Unexpected token � in JSON at position 0\n    at JSON.parse (<anonymous>)\n    at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51)\n    at ZoneDelegate.invokeTask (http:/

所以请如何解决此问题

Hello guys im building an application with spring boot and angular, im uploading files in the server and when trying to download this files am getting this error "error: SyntaxError: Unexpected token � in JSON at position 0 at JSON.parse".
Here is part of my code that is concerned

My Component

downloadProduct(fileUrl){
    this.httpClientService.downProduct(fileUrl).subscribe(response => {
      window.open(response.url, '_blank');
    }
    )
  }

My Service

public downProduct(fileUrl: string | undefined): Observable<any>{
    return this.httpClient.get(this.urlBack+"products/download/"+fileUrl, {
      responseType: 'json'
    });
  }

And My Spring Controller

@CrossOrigin(origins = "http://localhost:4200")
    @GetMapping("download/{filename}")
    public ResponseEntity<Resource> downloadFiles(@PathVariable("filename") String filename) throws IOException {
        System.out.println("weeeeeesh");
        Path filePath = get(DIRECTORY).toAbsolutePath().normalize().resolve(filename);
        if(!Files.exists(filePath)) {
            throw new FileNotFoundException(filename + " was not found on the server");
        }
        Resource resource = new UrlResource(filePath.toUri());
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("File-Name", filename);
        httpHeaders.add(CONTENT_DISPOSITION, "attachment;File-Name=" + resource.getFilename());
        System.out.println(resource.getFilename());
        return ResponseEntity.ok().contentType(MediaType.parseMediaType(Files.probeContentType(filePath)))
                .headers(httpHeaders).body(resource);

    }

and here is the error shown in the console

SyntaxError: Unexpected token � in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9799:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:83895:33) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9798:60) at Zone.runTask (http://localhost:4200/polyfills.js:9571:47) at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:9880:34) at invokeTask (http://localhost:4200/polyfills.js:10993:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:11030:21)
message: "Unexpected token � in JSON at position 0"
stack: "SyntaxError: Unexpected token � in JSON at position 0\n    at JSON.parse (<anonymous>)\n    at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51)\n    at ZoneDelegate.invokeTask (http:/

so please how can i fix this problem

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文