GWT客户端: sending a file with multipart/form-data post request

标签 gwt cordova post http-post multipartform-data

我已经尝试了很多方法,在客户端使用 GWT 通过 POST 请求以字符串+图片的形式发送 xml 文件。我可以成功发送字符串,但我不知道如何使用RequestBuilder发送文件(图片),我只能发送字符串。

有人知道如何使用 GWT 客户端 (RequestBuilder) 通过多部分/表单数据 POST 请求发送文件吗?

P.S.:由于我不想上传文件,因此我不需要 uploader 或类似的东西。我正在使用 Phonegap 开发一个移动应用程序,并制作图片,这些图片应该根据 POST 请求发送到服务器(第三方服务)。

提前致谢!

这里有一些代码:

public void sendPost() throws RequestException {
        String boundary = createBoundary();
        String xml = "<note> <to>Müller</to> <from>Jani</from> <heading>Erinnerung</heading> <body>Ich wohne in der Leipzigerstraße</body> </note>";
        String requestData = getRequestData(boundary, xml);



    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, "http://localhost:8080/xxx/yyy");
    builder.setHeader("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + boundary);
    builder.setHeader("Content-Length", Long.toString(requestData.length()));
    try {
      builder.sendRequest(requestData, new RequestCallback() {
        public void onResponseReceived(Request request, Response response) {

        }
        public void onError(Request request, Throwable exception) {
          exception.printStackTrace();
        }
      });
    } catch (RequestException e) {
      e.printStackTrace();
    }

}

private String getRequestData(String boundary, String xml) {
    String s = "";

    s += "--" + boundary + "\r\n";
    s += getRequestParameter("xml", xml + "");
    s += "--" + boundary + "--\r\n"; // end
    return s;
}

private String getRequestParameter(String key, String value) {
    return "Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n"
            + value + "\r\n";
}
private String createBoundary() {
    return "----GoneVerticalBoundary" + getRandomStr() + getRandomStr();
}

private String getRandomStr() {
    return Long.toString(random.nextLong(), 36); //random -> DEFINED IN THE CLASS BODY
}

最佳答案

如果您使用 gwt +phonegap,您应该使用 gwt-phonegap,对吧?

我在我的应用程序中所做的就是在桌面版本中使用 gwtupload,在移动设备中使用phonegap 文件传输。对于这两种情况,我在服务器端都使用 gwtupload servlet。

这是我使用 gwt-phonegap 的代码:

  FileUploadOptions options = new FileUploadOptions();
  options.setFileKey("file");
  options.setFileName("my_file.txt");
  options.setMimeType("text/plain");

  phonegap.getFile().createFileTransfer().upload(
   "file:///my_path/my_file.txt", 
   "http://my-gwtupload-servlet/servlet.gupld", 
   options, 
   new FileUploadCallback() {
    @Override
    public void onSuccess(FileUploadResult result) {
      if (result.getResponseCode() == 200) {
      } else {
      }
    }
    @Override
    public void onFailure(FileTransferError error) {
      Window.alert("Error sending the file, error-code: " + error.getCode());
    }
  });

我使用延迟绑定(bind)来使用 phonegap.env 选择适当的实现:

<replace-with class="...UploadFilePhoneGap">
  <when-type-is class="....UploadFile" />
  <when-property-is name="phonegap.env" value="yes" />
</replace-with>

关于GWT客户端: sending a file with multipart/form-data post request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13825320/

相关文章:

GWT CellTable,单击时禁用 ButtonCell

Gwt 架构 : why use MVP, 编辑器、RequestFactory、Gin 等?

javascript - 更改可折叠菜单标题中的文本会导致其消失

javascript - 在 Ionic 应用程序上使用 $.getScript 加载 Google Map Api

ios - 导出 ios 应用程序时出错

post - 多行curl命令

gwt - 企业应用框架

php - 如何通过POST类型以html形式发送PHP数组

c# - 如何从 Pacer.gov API 发出案例请求?

java - GWT DatePicker 在 2.5.1 中的变化