Image 使用blackberry上传图像

Image 使用blackberry上传图像,image,blackberry,file-upload,blackberry-simulator,Image,Blackberry,File Upload,Blackberry Simulator,我想使用MultipartPostData在blackberry模拟器中上传一个图像,下面是我的代码,但它似乎不起作用。我还签署了我的.cod文件。有人能帮我吗 public void postData(String Url, bytes[] data) { if (DeviceInfo.isSimulator()){ Url=Url+";deviceSide=true"; } HttpConnection httpConn=null; OutputStream os=null; Input

我想使用MultipartPostData在blackberry模拟器中上传一个图像,下面是我的代码,但它似乎不起作用。我还签署了我的.cod文件。有人能帮我吗

public void postData(String Url, bytes[] data)
{
 if (DeviceInfo.isSimulator()){
 Url=Url+";deviceSide=true";
}
HttpConnection httpConn=null;
OutputStream os=null;
InputStream is=null;
String url=Url;
try {
   PostData form = new MultipartPostData(MultipartPostData.DEFAULT_CHARSET, false) ;
   byte [] postData = data;
form.setData(postData);

      httpConn = (HttpConnection) Connector.open(url);
      httpConn.setRequestMethod(HttpConnection.POST);
   httpConn.setRequestProperty("User-Agent", "BlackBerry");
   httpConn.setRequestProperty("Content-Type", "multipart/form-data");
   httpConn.setRequestProperty("MIME-Type", "Image/Jpeg");
      httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postData.length));
      httpConn.setRequestProperty("Content-Language", "en-US");

      os =httpConn.openOutputStream();
      os.write(form.getBytes());

    //read response
    StringBuffer sb = new StringBuffer();
    is = httpConn.openDataInputStream();
    int chr;
    while ((chr = is.read()) != -1)
      sb.append((char) chr);

    System.out.println("Result................................ " + sb.toString());
    String result=sb.toString();
}
catch(Exception e)
{
    System.out.println(e.toString());
}
finally {
    try{
        if(is!= null)
          is.close();
        if(os != null)
          os.close();
if(httpConn != null)
 httpConn.close();
 } catch(Exception e1){
        System.out.println(e1.toString());
    }
   }
 }

//您必须具有二进制格式的post数据,.cod文件必须在模拟器上工作

httpConn = (HttpConnection)connDesc.getConnection();
                httpConn.setRequestMethod(HttpConnection.POST);          
    httpConn.setRequestProperty("user-agent", "BlackBerry");    
    httpConn.setRequestProperty("content-type", "multipart/form-data; boundary=----------V2ymHFg03ehbqgZCaKO6jy");

            os = httpConn.openOutputStream();
            //os.write(form.getBytes());

            byte[] fileBytes = {1,2,3,4}; //retrieve file bytes with your own code                

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            bos.write(("\r\n--" + "----------V2ymHFg03ehbqgZCaKO6jy" + "\r\n").getBytes());
       bos.write(("Content-Disposition: form-data; name=\"mifoto\"; filename=\"leo.gif\"\r\n").getBytes());
       bos.write(("Content-Type: image/gif\r\n\r\n").getBytes());
            bos.write(fileBytes);
       bos.write(("\r\n--" + "----------V2ymHFg03ehbqgZCaKO6jy" + "--\r\n").getBytes());

            os.write(bos.toByteArray());

只要您调用
MultipartPostData.setData()
,它就会覆盖您使用
MultipartPostData.append()
设置的任何内容处置数据


leonel的答案有效,或者您可以使用Vlad Patryshev的类。

实际上,MultipartPostData应该使用boudary和“内容配置”行来构造post数据。你有没有找到任何方法来执行你的请求?我正在尝试做同样的事情,无法将任何数据传递给服务器(服务器在帖子中只看到一个空的数据数组)。