首页 > 解决方案 > 无法使用球衣 2 上传文件。错误 400

问题描述

一开始我可以用这个 curl 请求上传我的文件:

curl -u "admin:admin" -X POST " http://myurlu:9086/service/rest/v1/components?repository=ebooks-store " -H "accept: application/json" -H "Content-Type:多部分/表单数据”-F“raw.directory=test”-F“raw.asset1=@billet.pdf;type=application/pdf”-F“raw.asset1.filename=billet.pdf”

并使用成功的日志:

2019-08-27 17:27:40,734+0000 INFO [qtp969575574-10357] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 使用参数上传组件:repository="ebooks-store" format="raw" directory="test" 2019-08-27 17:27:40,734+0000 INFO [qtp969575574-10357] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带有参数的资产:file="billet.pdf" 文件名="方坯.pdf"

我有在我的代码中上传的用例,所以我使用 jersey 创建了这个服务:

FileDataBodyPart filePart = new FileDataBodyPart("file", new File("C:\\Users\\tkossoko\\Documents\\article.pdf"), 
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
        FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
                .field("raw.directory", "test")
                .field("raw.asset1.filename.", "article.pdf")
                .bodyPart(filePart);

        String url = nexusBaseUrl+"v1/components?repository="+repositoryName;
        webTarget = client.target(url);
        //Very important to do, we have to register this
        webTarget.register(MultiPartFeature.class);
        final Response response = webTarget.request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).header("Expect", "100-continue")
                .post(Entity.entity(multipart, multipart.getMediaType()));

        //Use response object to verify upload success

        formDataMultiPart.close();
        multipart.close();

但我每次都得到这个错误:

message='在'1'上缺少必需的资产字段'文件名'}]'; 映射自:org.sonatype.nexus.rest.ValidationErrorsException:未知组件字段“asset1.filename。”,缺少必需的资产字段“1”上的“文件名”

我的代码有什么问题?我使用的网络服务正在等待 3 个参数:

raw.directory 字符串 raw.assetN 文件 raw.assetN.filename 字符串

标签: javamavenjersey

解决方案


正确答案是

FileDataBodyPart filePart = new FileDataBodyPart("raw.asset1", new File("C:\\Users\\tkos\\Documents\\article.pdf"), 
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
        FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
                .field("raw.directory", "test")
                .field("raw.asset1.filename.", "article.pdf")
                .bodyPart(filePart);

        String url = nexusBaseUrl+"v1/components?repository="+repositoryName;
        webTarget = client.target(url);
        //Very important to do, we have to register this
        webTarget.register(MultiPartFeature.class);
        final Response response = webTarget.request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).header("Expect", "100-continue")
                .post(Entity.entity(multipart, multipart.getMediaType()));

        //Use response object to verify upload success

        formDataMultiPart.close();
        multipart.close();

推荐阅读