GWT客户端:发送包含多部分/表单数据post请求的文件

GWT客户端:发送包含多部分/表单数据post请求的文件,gwt,cordova,post,http-post,multipartform-data,Gwt,Cordova,Post,Http Post,Multipartform Data,我已经尝试了很多方法,在客户端使用GWT通过POST请求以字符串+图片的形式发送xml文件。我可以成功发送字符串,但我不知道如何使用RequestBuilder发送文件(图片),我只能发送字符串 有人知道如何使用GWT客户机(RequestBuilder)通过多部分/表单数据POST请求发送文件吗 注:由于我不想上传文件,我不需要上传器或类似的东西。我正在开发一个带有Phonegap的移动应用程序,并制作图片,这些图片应该根据POST请求发送到服务器(第三方服务) 提前谢谢 下面是一些代码: p

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

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

注:由于我不想上传文件,我不需要上传器或类似的东西。我正在开发一个带有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
}
public void sendPost()引发RequestException{
字符串边界=createBoundary();
String xml=“莱比锡博物馆的Müller Jani Erinnerung Ich wohne”;
String requestData=getRequestData(边界,xml);
RequestBuilder=新的RequestBuilder(RequestBuilder.POST)http://localhost:8080/xxx/yyy");
builder.setHeader(“内容类型”,“多部分/表单数据;字符集=UTF-8;边界=”+边界);
setHeader(“Content-Length”,Long.toString(requestData.Length());
试一试{
sendRequest(requestData,newrequestcallback()){
接收到公共无效onResponseReceived(请求-请求,响应-响应){
}
公共void onError(请求,可丢弃异常){
异常。printStackTrace();
}
});
}捕获(请求异常e){
e、 printStackTrace();
}
}
私有字符串getRequestData(字符串边界,字符串xml){
字符串s=“”;
s+=“--”+边界+“\r\n”;
s+=getRequestParameter(“xml”,xml+”);
s+=“--”+边界+“--\r\n”;//结束
返回s;
}
私有字符串getRequestParameter(字符串键、字符串值){
return“内容处置:表单数据;名称=\”“+key+\”\r\n\r\n”
+值+“\r\n”;
}
私有字符串createBoundary(){
返回“----GoneVerticalBoundary”+getRandomStr()+getRandomStr();
}
私有字符串getRandomStr(){
返回Long.toString(random.nextLong(),36);//random->在类主体中定义
}

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

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


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

如果您使用的是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>

对于所有希望在客户端管理文件和图片的人,在将它们发送到服务器之前,正如Manolo所建议的,我建议


对于使用PhoneGap的人,我建议使用Base64重新编码图片,并在服务器端解码(请参见课程)

如果您知道如何在JS中实现这一点,那么将其移植到GWT应该很简单。但GWT不能创造奇迹。没有遇到用例。我以前已经使用过gwtupload,但现在我正在开发一款带有phonegap的手机,所以你不能只在那里上传图片(文件)。无论如何谢谢你!您是否可以更新您的问题,提供有关phonegap、gwtupload以及您已经尝试过的任何其他方法的详细信息。