如何在BlackBerry的服务器上上传图片?

时间:2011-10-17 12:38:15

标签: blackberry image-uploading

我是Blackberry开发的新手,我必须将我的Image从设备库上传到服务器。我发现很多与此相关的链接。但我找不到这个问题的确切结果。我用了这个Example。使用这个例子,我得到了Byte []的值,但是我无法使用这段代码满足我的要求。因为我无法理解我们必须在代码中传递哪个URL以及哪些是参数。

我又使用了一种格式,我在这里发布我的代码,使用这个我得到了响应代码:200。但我无法解决这个问题

HttpConnection oCon = (HttpConnection)Connector.open("http://74.208.77.106/jm/testing/iDaddyapi.php;deviceside=true;interface=wifi");
                 oCon.setRequestMethod(HttpConnection.POST);
                 oCon.setRequestProperty("Content-Length", "" + imageByte.length);

                 URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);

                 oPostData.append("api", "postidaddyimage");
                 oPostData.append("imagetype", "F");
                 oPostData.append("image", strImage);

                 OutputStream strmOut = oCon.openOutputStream();
                 strmOut.write(oPostData.getBytes());

                 strmOut.flush();
                 strmOut.close();



     int rc = oCon.getResponseCode();
     System.out.println("Response code.............."+rc);
     if (rc != HttpConnection.HTTP_OK)
                     throw new IOException("Error response code: " + rc);

任何人都可以帮助我吗?我坚持这个。

谢谢, 曼西

2 个答案:

答案 0 :(得分:0)

HttpConnection conn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);

conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
            String.valueOf(imagedata.length));

conn.setRequestProperty("x-rim-transcode-content", "none");
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream finalOut = conn.openOutputStream();

String newLine = "\r\n";
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write(newLine.getBytes());
String contDisp = "Content-Disposition:form-data;  
name=\"file\";filename=\"Image.jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type = "Content-Type:image/jpeg";
out.write(contDisp.getBytes());
out.write(newLine.getBytes());
out.write(type.getBytes());
out.write(newLine.getBytes());
out.write(contEnc.getBytes());
out.write(newLine.getBytes());
out.write(newLine.getBytes());
out.write(imagedata);
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write("--".getBytes());
out.write(newLine.getBytes());
finalOut.write(out.toByteArray());

out.flush();
out.close();

finalOut.flush();
finalOut.close();

答案 1 :(得分:0)

    public class ImageUploader extends Thread {

String filename;
Bitmap temp;
public ImageUploader(String filename)
{
    this.filename=filename;
}
public void run() {
    final String boundary = "Some_Unique_Text_Also_Alphanumeric";   

    try
    {
        //FileInputStream fis=new FileInputStream(File.FILESYSTEM_PATRIOT,filename);
        FileConnection fis=(FileConnection)Connector.open(filename);
        InputStream inputStream = fis.openInputStream();
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        int buffersize=1024;
        byte[] buffer=new byte[buffersize];
        int length=0;
        while((length=inputStream.read(buffer))!=-1)
        {
            bos.write(buffer,0,length);
        }
        byte[] imagedata=bos.toByteArray();

        HttpConnection conn = (HttpConnection) Connector.open(URL, Connector.READ_WRITE);
        conn.setRequestMethod(HttpConnection.POST);

        conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
                HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
                        + ";boundary=" + boundary);
        conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
                String.valueOf(imagedata.length));
        conn.setRequestProperty("x-rim-transcode-content", "none");

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream finalOut = conn.openOutputStream();

        String newLine = "\r\n";
        out.write(newLine.getBytes());
        out.write("--".getBytes());
        out.write(boundary.getBytes());
        out.write(newLine.getBytes());
        String contDisp = "Content-Disposition:form-data; name=\"file\";filename=\"Image.jpg\"";
        String contEnc = "Content-Transfer-Encoding: binary";
        String type = "Content-Type:image/jpeg";
        out.write(contDisp.getBytes());
        out.write(newLine.getBytes());
        out.write(type.getBytes());
        out.write(newLine.getBytes());
        out.write(contEnc.getBytes());
        out.write(newLine.getBytes());
        out.write(newLine.getBytes());
        out.write(imagedata);
        out.write(newLine.getBytes());
        out.write("--".getBytes());
        out.write(boundary.getBytes());
        out.write("--".getBytes());
        out.write(newLine.getBytes());
        finalOut.write(out.toByteArray());

        out.flush();
        out.close();

        finalOut.flush();
        finalOut.close();
        InputStream instream=conn.openInputStream();
        int ch=0;
        StringBuffer buffesr=new StringBuffer();
        while((ch=instream.read())!=-1)
        {
            buffesr.append((char)ch);
        }

        JSONObject myprofileObject = new JSONObject(buffesr.toString());
        String result = myprofileObject.optString("result");
        String url = myprofileObject.optString("url");
        String message=myprofileObject.optString("msg");
        MyProfile._model.setProfilePic(url);

    }
    catch (Exception e) {
        // TODO: handle exception
    }
};
    thread.start(); 
}
}