java - 使用 Angular 参数上传文件

标签 java angular spring-boot

我想发送文件上传,我在前端使用 Angular,在后端使用 Spring Boot。我有要在请求中发送的参数和正文中的字节数组。但是当我发送此消息时,我收到错误400。

这是我的后端

@PostMapping(path=Urls.UPLOAD_FILE_IN_LIBELLE_GDA, produces = { MediaType.APPLICATION_JSON_VALUE})
    public void uploadFileInLibelleGda(
            @RequestParam String processus,
            @RequestParam String level0Name,
            @RequestParam String nomFichier,
            @RequestParam String nomLibelle,
            @RequestParam String anneeFolderName,
            @RequestParam String semaineFolderName,
            @RequestBody ByteArrayResource fichier) throws Exception {

        uploadService.uploadFileInLibelleGda(racine, processus,level0Name,nomLibelle, anneeFolderName, semaineFolderName, nomFichier,  fichier.getByteArray());
    }

这是我的前端

  public uploadFiles(
        nomFichier: string,
        nomLibelle: string,
        processus: string,
        level0Name: string,
        semaineFolderName: string,
        anneeFolderName: string,
        byte: Blob
    ): Observable<any> {

        let params = new HttpParams();

        params.append('level0Name', level0Name);
        params.append('processus', processus);
        params.append('nomLibelle', nomLibelle);
        params.append('anneeFolderName', anneeFolderName);
        params.append('semaineFolderName', semaineFolderName);
        params.append('nomFichier', nomFichier);


        return this.httpClient.post(this.urlUploadFile, byte, {

            'params': params
        });
    }

最佳答案

对于 MultiPart FileUpload,spring 已经提供了 RequestPart 使用它。

@RequestParam 依赖于通过注册的 Converter 或 PropertyEditor 进行类型转换,而 RequestPart 依赖于 HttpMessageConverters,同时考虑请求部分的“Content-Type” header 。 RequestParam 可能与名称-值表单字段一起使用。

https://docs.spring.io/spring/docs/current/javadoc- api/org/springframework/web/bind/annotation/RequestPart.html

在 Spring Rest 服务中使用 @RequestPart (value="uploadFile") MultipartFile 文件

 let formData:FormData = new FormData();
    formData.append('uploadFile', file, file.name);
    let headers = new HttpHeaders();
    /** In Angular 5, including the header Content-Type can invalidate your request */
    headers.append('Content-Type', 'multipart/form-data');
    headers.append('Accept', 'application/json');

    let params = new HttpParams();

params.append('level0Name', '1');
params.append('processus', '1');
params.append('nomLibelle', '1');
params.append('anneeFolderName', '1');
params.append('semaineFolderName', '1');
params.append('nomFichier', '1');

    this.httpClient.post(this.resourcePrefix+'/user/upload', formData,  { headers: headers, params: params})

        .subscribe(
            data => console.log('success'),
            error => console.log(error)
        )
}

关于java - 使用 Angular 参数上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58640822/

相关文章:

java - 在 Java 中使用 DateFormat 格式化 Date 对象

java - 如何删除对话框引用的对象?

angular - 如何将 MatSort 与 Object[] 类型的表一起使用

spring - URL参数如何映射到Spring Boot Controller 上的对象?

java - 添加失败 : stat/var/lib/docker/tmp/docker-builderXYZ/myapp. jar: 没有这样的文件或目录

java - 使用 Java RegEx 如何仅返回完全匹配的内容?

java - 使用 Spring WebFlux 的 webclient 在 Mono 上有条件地重复或重试

node.js - 使用 Node js 将 Angular 应用程序作为 docker 镜像运行

javascript - 检查 Web 应用程序是否安装了 React、Angular 或 Vue

java - 将 querydsl 与 Spring Boot、Gradle 和 Idea 一起使用时,QClasses 中未解析的符号