使用黑莓上传图像

发布于 2024-09-13 07:14:51 字数 1594 浏览 6 评论 0原文

我想使用 MultipartPostData 在黑莓模拟器中上传图像,以下是我的代码,但它似乎不起作用。我还签署了我的 .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());
    }
   }
 }

I want to upload an image in blackberry simulator using MultipartPostData, the following is my code but it does not seem to work. I have also signed my .cod file. Can anyone help me please?

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());
    }
   }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-09-20 07:14:51

//你必须有一个bundary格式的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());

//you must have a bundary format post data, the .cod file must be work on the simulator

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());
北城半夏 2024-09-20 07:14:51

一旦您调用 MultipartPostData.setData(),它就会覆盖您使用 MultipartPostData.append() 设置的任何 Content-Disposition 数据。

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

As soon as you call MultipartPostData.setData(), it overwrites any Content-Disposition data you have set with MultipartPostData.append().

leonel's answer works or you can use Vlad Patryshev's ClientHttpRequest class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文