2011-10-17 25 views
-1

私はBlackberry開発で新しく、デバイスギャラリーからサーバーにイメージをアップロードする必要があります。私はこの点に関連する多くのリンクを見つけました。しかし、私はこの問題の正確な結果を見つけることができません。私はこれをExampleと使いました。この例では、Byte []に​​値を渡しましたが、このコードを使用して要件を満たすことができません。その中で私はコードで渡さなければならないURLとパラメータを理解できません。BlackBerryでサーバーに画像をアップロードするには?

もう1つのフォーマットを使用しましたが、ここでコードを投稿します。これを使用してレスポンスコード: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); 

私を助けることができる人は誰ですか?私はこれに固執しています。 http://www.developer.nokia.com/Community/Wiki/HTTP_Post_multipart_file_upload_in_Java_ME -

おかげで、 マンシ

+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(); 
+0

こんにちは、Vijayはあなたの返事に感謝します。しかし、私はこの行で "HttpConnection conn =(HttpConnection)Connector.open(Url、Connector.READ_WRITE);"どんなURLを渡す必要がありますか?私のURLは大丈夫ですか?どうやってそれを作り出すことができるのでしょうか?ありがとう – anddev

+0

URL = "http://74.208.77.106/jm/testing/iDaddyapi.php?;deviceside=true;interface=wifi"; "?"を追加していません。パラメータを渡すためのWebサービス名の後に "?"そしてさらに試みる。 –

+0

ありがとうございますが、境界についてはどうですか?そこから私たちはそれを生み出すことができますか? – anddev

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

デバイスのメモリからイメージを取り出し、そのイメージをアップロードします。 –

+0

よろしくお願いいたします。 – anddev

関連する問題