无法用jersey上传文件到rest api。

问题描述 投票:0回答:1

我的nexus暴露了一个REST API来上传文件.用curl我可以用这个命令上传。

curl -X POST "http:/myurl:9086serviceestv1components?repository=ebooks-store。" -H "accept: applicationjson" -H "Content-Type: multipartform-data" -F "raw.directory=test" -F "[email protected];type=applicationpdf" -F "raw.asset1.filename=billet.pdf"

文档中说只需要3个信息。https:/help.sonatype.comrepomanager3rest-and-integration-apicomponents-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();

在我的nexus的日志中,我有这个错误。

2019-08-22 21:43:32,122+0000 INFO [qtp969575574-7812] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Uploading component with parameters: repository="ebooks-store" format="raw" directory="test" 2019-08-22 21:43:32,122+0000 INFO [qtp969575574-7812] admin org. sonatype.nexus.repository.upload.internal.UploadManagerImpl - Asset with parameters: file="null" filename="article.pdf" 2019-08-22 21:00:00 [qtp969575574-7812] admin org. pdf" 2019-08-22 21:43:32,122+0000 INFO [qtp969575574-7812] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Asset with parameters: file="article.pdf" 2019-08-22 21:43:32,124+0000 WARN [qtp969575574-7812] admin org. sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 678c48c7-d7fc-438d-94ab-df54977fed23) Failed to map exception org.jboss.resteasy.spi.BadRequestException: RESTEASY003520。在 org.jboss.resteasy.core.request.QualityValue.parseAsInteger(QualityValue.java:112) ... 2019-08-22 21:43:32,125+0000 WARN [qtp969575574-7812] admin org. sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 678c48c7-d7fc-438d-94ab-df54977fed23) Response: [500] 'FaultXO{id='678c48c7-d7fc-438d-94ab-df54977fed23', message='org.jboss.resteasy.spi.BadRequestException: RESTEASY003520。 '}'; mapped from: org.sonatype.nexus.rest.ValidationErrorsException.BadRequestException: RESTEASY003520: Malformed quality value: 在'2'上缺少必要的资产字段'Filename'。

我做错了什么?当我把raw.asset1.filename改成Filename时,得到的结果是

2019-08-22 21:48:51,653+0000 INFO [qtp969575574-7831] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Uploading component with parameters: repository="ebooks-store" format="raw" Filename="article. pdf" directory="test" 2019-08-22 21:48:51,653+0000 INFO [qtp969575574-7831] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Asset with parameters. UploadManagerImpl - Asset with parameters: file="null" 2019-08-22 21:48:51,653+0000 INFO [qtp969575574-7831] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Asset with parameters: file="article. pdf" 2019-08-22 21:48:51,655+0000 WARN [qtp969575574-7831] admin org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 2e179b6c-7c6a-486d-bb17-41a86da08103) Failed to map exception org.jboss.resteasy.spi.BadRequestException: RESTEASY003520。消息='org.jboss.resteasy.spi.BadRequestException.RESTEASY003520: Malformed quality value: RESTEASY003520.BadRequestException: RESTEASY003520: Malformed quality value: '}';映射自:org.sonatype.nexus.rest.ValidationErrorsException.BadRequestException: RESTEASY003520: Malformed quality value: 未知组件字段'Filename','1'上缺少必要的资产字段'Filename','2'上缺少必要的资产字段'Filename',资产1和2有相同的坐标。

我真的不知道该怎么做。

java spring rest api jersey-client
1个回答
2
投票

如果你有一个maven repo,似乎你必须使用Maven值来上传。Raw对我不起作用,但这对我的Maven repo是有效的。


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";
}
© www.soinside.com 2019 - 2024. All rights reserved.