GWT客户端:使用multipart/form-data POST请求发送文件

4

我已经尝试了很多方法,使用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
}

如果你知道如何在JS中实现这个想法,那么将其移植到GWT应该很简单。但是GWT不能创造奇迹。 - Thomas Broyer
问题点赞了。没有遇到过这种用例。同时添加了phonegap标签。 - appbootup
3个回答

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>

1

如果您正在寻找纯GWT解决方案,则需要使用FileUpload进行工作。

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

对于区域设置问题,请确保您使用UTF-8和GWT区域设置cookie和区域设置。


我以前已经使用过gwtupload,但现在我正在使用phonegap为手机开发,所以你不能只是上传图片(文件)。无论如何还是谢谢! - jcdmb
你能否更新一下你的问题,提供有关PhoneGap、GWTUpload以及你已经尝试过的其他方法的详细信息。 - appbootup

0
对于所有想在客户端管理文件和图片并在发送到服务器前进行处理的人,正如Manolo所推荐的,我建议使用 lib-gwt-file
对于使用PhoneGap的人,我建议使用Base64对图片进行编码,并在服务器端进行解码处理(请参见Base64Util类)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接