【问题标题】:Impossible to Upload file to rest api with jersey无法使用球衣上传文件以休息 api
【发布时间】:2019-12-28 06:09:58
【问题描述】:

我的 nexus 公开了一个 REST API 来上传文件。 使用 curl 我可以使用此命令上传:

curl -X POST "http://myurl: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"

文档说只需要 3 个信息: https://help.sonatype.com/repomanager3/rest-and-integration-api/components-api

raw.directory (String = Destination for upload files (e.g. /path/to/files)) 
raw.assetN  (File   = at least one  Binary asset) 
raw.assetN.filename (String = Filename to be used for the corresponding assetN asset)

所以在我的 java 代码中,我尝试使用 Jersey 做同样的事情:

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

        String url = nexusBaseUrl+"v1/components?repository="+repositoryName;
        WebTarget target = client.target(url);
        //Very important to do, we have to register this
        target.register(MultiPartFeature.class);
        final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType()));

        //Use response object to verify upload success

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

在我的关系日志中,我有这个错误:

2019-08-22 21:43:32,122+0000 信息 [qtp969575574-7812] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 上传带有参数的组件:repository="ebooks-store" 格式="原始" 目录="测试" 2019-08-22 21:43:32,122+0000 信息 [qtp969575574-7812] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带参数的资产:file="null" filename="article.pdf" 2019-08-22 21:43:32,122+0000 信息 [qtp969575574-7812] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带参数的资产:file="article.pdf" 2019-08-22 21:43:32,124+0000 警告 [qtp969575574-7812] 管理员 org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 678c48c7-d7fc-438d-94ab-df54977fed23) 无法映射异常 org.jboss.resteasy.spi.BadRequestException:RESTEASY003520:格式错误 质量价值。 在 org.jboss.resteasy.core.request.QualityValue.parseAsInteger(QualityValue.java:112) ... 2019-08-22 21:43:32,125+0000 警告 [qtp969575574-7812] 管理员 org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 678c48c7-d7fc-438d-94ab-df54977fed23)响应:[500] 'FaultXO{id='678c48c7-d7fc-438d-94ab-df54977fed23', 消息='org.jboss.resteasy.spi.BadRequestException:RESTEASY003520: 错误的质量值。'}';映射自: org.sonatype.nexus.rest.ValidationErrorsException:缺少必需的 “2”上的资产字段“文件名”

我做错了什么?当我只用 Filename 更改 raw.asset1.filename 时,我得到了

2019-08-22 21:48:51,653+0000 信息 [qtp969575574-7831] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 上传带有参数的组件:repository="ebooks-store" 格式="原始" 文件名="article.pdf" 目录="测试" 2019-08-22 21:48:51,653+0000 信息 [qtp969575574-7831] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带参数的资产:file="null" 2019-08-22 21:48:51,653+0000 INFO [qtp969575574-7831] 管理员 org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带参数的资产:file="article.pdf" 2019-08-22 21:48:51,655+0000 警告 [qtp969575574-7831] 管理员 org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 2e179b6c-7c6a-486d-bb17-41a86da08103) 映射异常失败 org.jboss.resteasy.spi.BadRequestException:RESTEASY003520:格式错误 质量价值。消息='org.jboss.resteasy.spi.BadRequestException: RESTEASY003520:质量值格式错误。'}';映射自: org.sonatype.nexus.rest.ValidationErrorsException:未知组件 字段“文件名”,“1”上缺少必需的资产字段“文件名”, “2”上缺少必需的资产字段“文件名”,资产 1 和 2 坐标相同

我真的不知道该怎么办。

【问题讨论】:

  • 向上。没人知道吗?

标签: java spring rest api jersey-client


【解决方案1】:

如果您有一个 Maven 存储库,您似乎必须使用 Maven 值来上传。 Raw对我不起作用。 但这适用于我的 Maven 存储库。


import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

class Authenticator implements ClientRequestFilter
{
    private final String user;
    private final String password;

    Authenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    public void filter(ClientRequestContext requestContext) throws IOException {
        MultivaluedMap<String, Object> headers = requestContext.getHeaders();
        final String basicAuthentication = getBasicAuthentication();
        headers.add("Authorization", basicAuthentication);
    }

    private String getBasicAuthentication() {
        String token = this.user + ":" + this.password;
        try {
            return "BASIC " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalStateException("Cannot encode with UTF-8", ex);
        }
    }
}
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.glassfish.jersey.media.multipart.internal.MultiPartWriter;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;

public class NexusFacade
{
    public Response uploadFile(File file, String repository, String groupId, String artifactId, String extension, String version)
    {
        String user = "XXXXX";
        String password = "XXXXX";
        ClientConfig configuration = new ClientConfig();
        configuration.register(MultiPartWriter.class);
        Client client = ClientBuilder.newClient(configuration).register(new Authenticator(user, password));

        FormDataMultiPart multipartEntity = new FormDataMultiPart();
        multipartEntity.bodyPart(new FileDataBodyPart("maven2.asset1", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
        multipartEntity.bodyPart(new FormDataBodyPart("maven2.asset1.extension", extension, MediaType.TEXT_PLAIN_TYPE));
        multipartEntity.bodyPart(new FormDataBodyPart("maven2.groupId", groupId, MediaType.TEXT_PLAIN_TYPE));
        multipartEntity.bodyPart(new FormDataBodyPart("maven2.artifactId", artifactId, MediaType.TEXT_PLAIN_TYPE));
        multipartEntity.bodyPart(new FormDataBodyPart("maven2.version", version, MediaType.TEXT_PLAIN_TYPE));
        multipartEntity.bodyPart(new FormDataBodyPart("maven2.generate-pom", "false", MediaType.TEXT_PLAIN_TYPE));

        return client.target(baseURL)
                .path("/service/rest/v1/components")
                .queryParam("repository", repository)
                .request()
                .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA));
    }

    private String baseURL = "http://my-nexus.localhost";
}

【讨论】:

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