GWT客户端:发送带有multipart / form-data post请求的文件

时间:2012-12-11 17:24:51

标签: gwt cordova post http-post multipartform-data

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

有人知道如何使用GWT Client(RequestBuilder)发送带有multipart / form-data POST请求的文件吗?

P.S。:由于我不想上传文件,我不需要上传器或类似的东西。我正在开发一个带有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
}

3 个答案:

答案 0 :(得分:1)

如果您正在寻找纯gwt解决方案,那么您需要使用FileUpload

如果您不介意使用第三方开源jar,那么您可以尝试gwtupload

对于区域设置问题,只需确保使用UTF-8和GWT区域设置Cookie和区域设置。

答案 1 :(得分:1)

如果您使用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());
    }
  });

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

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

答案 2 :(得分:0)

对于想要在客户端管理文件和图片的所有人,在将其发送到服务器之前,正如Manolo建议的那样,我建议lib-gwt-file

对于使用PhoneGap的人,我建议使用Base64插入图片,并在服务器端解码(参见课程Base64Util