簡體   English   中英

無法將文件上傳到帶有球衣的休息 api

[英]Impossible to Upload file to rest api with jersey

我的 nexus 公開了一個 REST API 來上傳文件。 使用 curl 我可以使用以下命令上傳:

curl -X POST " http://myurl:9086/service/rest/v1/components?repository=ebooks-store " -H "accept: application/json" -H "Content-Type: 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 INFO [qtp969575574-7812] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 上傳組件參數: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 - 帶參數的資產:file="null" filename=" article.pdf" 2019-08-22 21:43:32,122+0000 INFO [qtp969575574-7812] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 帶有參數的資產:file="article.pdf" 201 08-22 21:43:32,124+0000 WARN [qtp969575574-7812] admin org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 678c48c7-d7fc-437-d7fc-437-feasyd5234org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper) .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 .internal.ValidationErrorsExceptionMapper - (ID 678c48c7-d7fc-438d-94ab-df54977fed23) 響應:[500]'FaultXO{id='678c48c7-d7fc-438d-94ab-df54977fs. BadRequestException: RESTEASY003520: 質量值格式錯誤。'}'; 映射自:org.sonatype.nexus.rest.ValidationErrorsException:“2”上缺少必需的資產字段“文件名”

我做錯了什么 ? 當我僅通過文件名更改 raw.asset1.filename 時,我得到了

2019-08-22 21:48:51,653+0000 INFO [qtp969575574-7831] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 上傳組件參數: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 - 帶參數的資產:文件="null" 2019-08-22 21:48:51,653+0000 INFO [qtp969575574-7831] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 帶參數的資產:file="article.pdf" 201 08-22 21:48:51,655+0000 WARN [qtp969575574-7831] 管理員 org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID 2e179b6c-7c6a-486deasys1bb017.org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper) .spi.BadRequestException:RESTEASY003520:質量值格式錯誤。 message='org.jboss.resteasy.spi.BadRequestException: RESTEASY003520: 格式錯誤的質量值。'}'; 映射自:org.sonatype.nexus.rest.ValidationErrorsException:未知組件字段“文件名”,“1”上缺少必需的資產字段“文件名”,“2”上缺少必需的資產字段“文件名”,資產 1 和 2 有相同的坐標

我真的不知道該怎么辦。

如果您有一個 Maven 存儲庫,似乎您必須使用 Maven 值來上傳。 原始對我不起作用。 但這適用於我的 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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM