0

I am posting a ajax call in javascript with form data and form data also contains one file. i want to do same with java.

i have tried usingspring rest template but didn't work, sending file creating problem getting exceptions. Please suggest me a way how ill use http client to send file over post call as same as i did in javascript code.

const uploadFormData = new FormData();
    uploadFormData.append('file', this.state.uploadFile);
    uploadFormData.set('folderId' , this.state.uploadFoldarId);
    uploadFormData.set('repositoryId' , this.state.uploadRepositoryId);
    uploadFormData.set('orgName' , this.state.uploadOrgName);

    uploadFormData.set('sourceFileName','document_forTPAPI.txt');
    uploadFormData.set('title','from_React');
    uploadFormData.set('description','test');
    uploadFormData.set('changeLog','no');
    uploadFormData.set('mimeType','application\\txt');
    uploadFormData.set('serviceContext ','{}');
    $.ajax({
            url: 'https://tst.com/api/jsonws/dlapp/add-file-entry',
            type: 'POST',
            data: uploadFormData,
            async: false,
            cache: false,
            contentType: false,
            enctype: 'multipart/form-data',
            processData: false,

            success: function (response) {
                //alert(response);
                console.log(response);
            }
        });
AS_Tomar
  • 11
  • 3
  • 1
    Try using HTTP Post requests, an example here : https://stackoverflow.com/questions/10604001/how-to-send-simple-http-post-request-with-post-parameters-in-java – Manuel Espinosa Jan 16 '19 at 22:49
  • 1
    Why not the Apache Common HttpClient (included in liferay)?, see https://stackoverflow.com/a/6917303/379902 – Daniele Baggio Jan 17 '19 at 10:56
  • +1 to @DanieleBaggio's comment, I'm tempted to suggest that answer in a vote for duplicate. On top of that, please be more specific than "doesn't work": What precisely have you done, what's happening, what did you expect? – Olaf Kock Jan 17 '19 at 17:41
  • @AS_Tomar please edit your question to specify differences to one of the linked answers in the comments above: What do you need that's not answered there? Otherwise I'd expect this to be a duplicate question. – Olaf Kock Jan 17 '19 at 21:07
  • Hi olaf kock...please find my working code...i did as suggested by Manuel and Daniele. – AS_Tomar Jan 17 '19 at 22:41

1 Answers1

1
 uploadHttpClient = HttpClientBuilder.create().build();
    String userCredentialsBasicAuth =
        "Basic " + new String(Base64.encodeBase64(userCredentials.getBytes()));
    HttpPost post = new HttpPost(url + "/add-file-entry");
    post.setHeader(AUTHORIZATION, userCredentialsBasicAuth);
    BasicHttpContext ctx = new BasicHttpContext();

    FileBody filebody = new FileBody(getFileFromMultipartFile(file));
    StringBody repositoryIdBody = new StringBody(repositoryId, ContentType.TEXT_PLAIN);
    StringBody folderIdBody = new StringBody(folderId, ContentType.TEXT_PLAIN);
    StringBody sourceFileName =
        new StringBody(file.getOriginalFilename(), ContentType.TEXT_PLAIN);
    StringBody mimeType = new StringBody(file.getContentType(), ContentType.TEXT_PLAIN);
    StringBody title = new StringBody(file.getOriginalFilename(), ContentType.TEXT_PLAIN);
    StringBody description = new StringBody(StringPool.BLANK, ContentType.TEXT_PLAIN);
    StringBody changeLog = new StringBody(StringPool.BLANK, ContentType.TEXT_PLAIN);
    StringBody serviceContext = new StringBody("{}", ContentType.TEXT_PLAIN);

    org.apache.http.HttpEntity entity =
        MultipartEntityBuilder.create()
            .addPart("file", filebody)
            .addPart("repositoryId", repositoryIdBody)
            .addPart("folderId", folderIdBody)
            .addPart("sourceFileName", sourceFileName)
            .addPart("mimeType", mimeType)
            .addPart("title", title)
            .addPart("description", description)
            .addPart("changeLog", changeLog)
            .addPart("serviceContext", serviceContext)
            .build();

    post.setEntity(entity);
    HttpResponse resp = uploadHttpClient.execute(post, ctx);
AS_Tomar
  • 11
  • 3