简体   繁体   English

Java中Ajax发布调用的等效方法

[英]Equivalent method to ajax post call in java

I am posting a ajax call in javascript with form data and form data also contains one file. 我在javascript中用表单数据发布了一个ajax调用,表单数据还包含一个文件。 i want to do same with java. 我想用java做同样的事情。

i have tried usingspring rest template but didn't work, sending file creating problem getting exceptions. 我已经尝试过使用spring rest模板,但无法正常工作,发送文件会遇到异常的问题。 Please suggest me a way how ill use http client to send file over post call as same as i did in javascript code. 请给我建议一种方法,就像我在javascript代码中一样,如何使用http客户端通过邮寄发送文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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