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

时间:2019-01-16 22:34:25

标签: javascript java file http liferay

我正在用表单数据在javascript中发布ajax调用,并且表单数据还包含一个文件。我想用java做同样的事情。

我尝试过使用spring rest模板,但是没有用,发送文件时遇到异常而导致创建问题。请建议我一种方法,就像我在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);
            }
        });

1 个答案:

答案 0 :(得分: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);