【问题标题】:Equivalent method to ajax post call in javajava中ajax post调用的等效方法
【发布时间】:2019-01-16 22:34:25
【问题描述】:

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

我尝试过使用spring rest 模板,但没有成功,发送文件导致出现异常问题。请建议我如何使用 http 客户端通过 post call 发送文件,就像我在 javascript 代码中所做的一样。

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

【问题讨论】:

  • 尝试使用 HTTP Post 请求,这里有一个例子:stackoverflow.com/questions/10604001/…
  • 为什么不使用 Apache Common HttpClient(包含在 liferay 中)?请参阅 stackoverflow.com/a/6917303/379902
  • +1 对@DanieleBaggio 的评论,我很想在重复投票中建议这个答案。最重要的是,请比“不起作用”更具体:您到底做了什么,发生了什么,您期望什么?
  • @AS_Tomar 请编辑您的问题以指定与上述 cmets 中链接答案之一的差异:您需要什么但那里没有回答?否则我希望这是一个重复的问题。
  • 嗨 olaf kock...请找到我的工作代码...我按照 Manuel 和 Daniele 的建议做了。

标签: javascript java file http liferay


【解决方案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);

【讨论】:

  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2023-03-25
  • 2019-10-15
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多