【问题标题】:Unable to upload file with jersey 2. Error 400无法使用球衣 2 上传文件。错误 400
【发布时间】:2019-08-27 17:45:57
【问题描述】:

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

curl -u "admin:admin" -X POST “http://myurlu:9086/service/rest/v1/components?repository=ebooks-store” -H“接受:应用程序/json”-H“内容类型:multipart/form-data”-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 信息 [qtp969575574-10357] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 上传带有参数的组件:repository="ebooks-store" 格式=“原始”目录=“测试”2019-08-27 17:27:40,734+0000 信息 [qtp969575574-10357] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带参数的资产:file="billet.pdf" filename="billet.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();

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

2019-08-27 17:28:29,617+0000 信息 [qtp969575574-10306] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 上传带有参数的组件:repository="ebooks-store" 格式="原始"asset1.filename.="article.pdf" 目录="测试" 2019-08-27 17:28:29,618+0000 信息 [qtp969575574-10306] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带参数的资产:file="article.pdf" 2019-08-27 17:28:29,619+0000 警告 [qtp969575574-10306] 管理员 org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID c6a95618-aed1-45e8-90ee-2af315eb209b)响应:[400] '[ValidationErrorXO{id='asset1.filename.', message='未知组件 字段 'asset1.filename.''}, ValidationErrorXO{id='filename', message='在'1'上缺少必需的资产字段'文件名'}]';映射 来自:org.sonatype.nexus.rest.ValidationErrorsException:未知 组件字段“asset1.filename.”,缺少必需的资产字段 '1'上的'文件名'

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

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

【问题讨论】:

  • 您发送的文件部分名称为"file"。在您的 curl 代码中,正在使用部件名称 raw.asset1 发送文件。 FileDataBodyPart 构造函数的第一个参数是部件的名称。
  • @PaulSamsotha 非常感谢它的工作

标签: java maven jersey


【解决方案1】:

正确答案是

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2021-12-18
    • 2019-12-28
    • 2012-05-22
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多