简体   繁体   中英

Unable to upload file with jersey 2. Error 400

At the beginning I can upload my file with this curl request :

curl -u "admin:admin" -X POST " http://myurlu: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"

And with the successful log :

2019-08-27 17:27:40,734+0000 INFO [qtp969575574-10357] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Uploading component with parameters: 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 - Asset with parameters: file="billet.pdf" filename="billet.pdf"

I have the use case to upload in my code, so I created this service using 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();

But I got each time this error :

2019-08-27 17:28:29,617+0000 INFO [qtp969575574-10306] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Uploading component with parameters: repository="ebooks-store" format="raw" asset1.filename.="article.pdf" directory="test" 2019-08-27 17:28:29,618+0000 INFO [qtp969575574-10306] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - Asset with parameters: file="article.pdf" 2019-08-27 17:28:29,619+0000 WARN [qtp969575574-10306] admin org.sonatype.nexus.siesta.internal.ValidationErrorsExceptionMapper - (ID c6a95618-aed1-45e8-90ee-2af315eb209b) Response: [400] '[ValidationErrorXO{id='asset1.filename.', message='Unknown component field 'asset1.filename.''}, ValidationErrorXO{id='filename', message='Missing required asset field 'Filename' on '1''}]'; mapped from: org.sonatype.nexus.rest.ValidationErrorsException: Unknown component field 'asset1.filename.', Missing required asset field 'Filename' on '1'

What is wrong in my code ? The webservice I use is waiting for 3 paramaters :

raw.directory String raw.assetN File raw.assetN.filename String

the correct answer is

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM