2011-10-17 4 views
-1

Je suis nouveau dans le développement Blackberry et je dois télécharger mon image de la galerie de l'appareil au serveur. J'ai trouvé beaucoup de liens liés à ce point. Mais je ne peux pas trouver le résultat exact de ce problème. J'ai utilisé ce Example. En utilisant cet exemple, j'ai trouvé la valeur dans Byte [] mais je ne suis pas capable de répondre à mes besoins en utilisant ce code. En cela je ne suis pas capable de comprendre quelle URL nous devons passer dans le code et quels sont les paramètres.Comment télécharger une image sur le serveur dans BlackBerry?

J'ai utilisé un format de plus, je poste mon code ici, en utilisant ceci j'ai obtenu le code de réponse: 200. Mais je ne suis pas en mesure de résoudre cela

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

Quelqu'un peut-il m'aider? Je suis coincé là-dessus.

Merci, Mansi

+0

Avez-vous vérifié ce - http://www.developer.nokia.com/Community/Wiki/HTTP_Post_multipart_file_upload_in_Java_ME? –

Répondre

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

Bonjour, Vijay merci pour votre réponse Mais j'ai une question qui dans cette ligne "HttpConnection conn = (HttpConnection) Connector.open (Url, Connector.READ_WRITE);" Quelle URL devons-nous passer et avec quels paramètres? Vous pouvez voir mon URL est ok écrire? Et quelle est la frontière comment pouvons-nous le générer? Merci – anddev

+0

Url = "http://74.208.77.106/jm/testing/iDaddyapi.php?;deviceside=true;interface=wifi"; vous n'ajoutez pas "?" après le nom du Webservice pour passer les paramètres. Maintenant, ajoutez "?" et essayez à nouveau. –

+0

ok merci mais qu'en est-il de la limite? D'où nous pouvons le générer? – 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

Je récupère l'image de la mémoire de l'appareil, puis télécharge cette image. –

+0

ok merci laissez-moi essayer. – anddev

Questions connexes