【问题标题】:Uploading Image from Angular 7 to Spring-boot将图像从 Angular 7 上传到 Spring-boot
【发布时间】:2019-09-23 04:02:07
【问题描述】:

我正在尝试将图像从我的 Angular 应用程序传输到 Spring boot,但我似乎无法使其工作。当我从 Angular 发送带有文件的 POST 请求时,spring boot 似乎根本没有反应,所以我用 PostMan 对其进行了测试,我收到以下错误:

 "timestamp": "2019-05-05T06:45:26.907+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "No primary or default constructor found for class java.io.File",
    "path": "/upload"

在 Spring Boot 中:

ERROR 18100 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for class java.io.File] with root cause

以及字符解码失败。

在 Spring 输出中,它似乎猜对了文件类型(图像 jpeg) 它的名字。

这是 Spring Boot 接收器:

@RequestMapping(method = RequestMethod.POST,value ="/upload")
     public void postImage(File file) throws IOException {
     System.out.println("received");
}

稍后我打算在文件夹中写下图像,但现在我只想得到它。

下面是 Angular 部分

<form>
    <div>Title:  <input type="text" id="input" [(ngModel)]="img.name" 
[ngModelOptions]="{standalone: true}"> 
      <br>
      <input type="file" (change)='handleImages($event)' >
    </div> 
    <br>
   <button type="submit" class="confirm"(click) = 
'add()'>Confirm</button>
 </form>

component.ts:

handleImages(Event){  
      this.selectedFile = Event.target.files[0];
      console.log(this.selectedFile);
      this.http.post('http://localhost:8080/upload',this.selectedFile)
}

【问题讨论】:

    标签: java angular image file spring-boot


    【解决方案1】:

    您需要将参数 File 更改为 MultipartFile

    //@RequestMapping(method = RequestMethod.POST,value ="/upload")
    @PostMapping(value = "/upload")
    public void postImage(@RequestParam("file") MultipartFile file) throws IOException {
         System.out.println("received");
    }
    

    并将文件内容包装在 FormData 中

    handleImages(Event){  
          this.selectedFile = Event.target.files[0];
          let formData = new FormData();
          formData.append("file", this.selectedFile);
          console.log(this.selectedFile);
          this.http.post('http://localhost:8080/upload',formData)
    }
    

    【讨论】:

    • 感谢您的宝贵时间和建议,但 Spring 似乎没有收到 post 请求。端口和 URL 似乎是正确的,因为 Angular 能够从 Spring 获取内容。 @RequestMapping(method = RequestMethod.POST,value ="/upload") public void postImage(@RequestParam("file") MultipartFile file) throws IOException { System.out.println("received");这就是 Spring 正在听的。你有什么建议吗?
    • 你可以尝试从@RequestMapping(method = RequestMethod.POST,value ="/upload") 更改为@PostMapping(value = "/upload") 吗?
    • 嗨,在使用 PostMan 进行测试时进行建议的更改(图像被接收并按原样保存),但从角度来看,仍然是完全静音,例如当 Angular 向 FD 发布请求时,spring 甚至没有反应。我确保 console.log 了 formdata.get('file') 并且控制台中的一切似乎都很好。有什么建议吗?
    • console.log(this.selectedFile) 中的console.log 中是否有任何内容?在网络选项卡中,您看到发布请求运行了吗?
    • 您好,事实证明,除非我 .subscribe ,否则 Post 请求不会发送。您在浏览器控制台中查找它的建议让我意识到这一点。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2019-08-26
    • 1970-01-01
    • 2020-04-19
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多