2011-10-17 176 views
-1

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

我使用了一种格式,我在这里发布我的代码,使用这个我得到了响应代码: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); 

任何人都可以帮我吗?我困在此。

感谢, 曼西

+0

你检查这一点 - http://www.developer.nokia.com/Community/Wiki/HTTP_Post_multipart_file_upload_in_Java_ME? – 2011-10-17 19:02:19

回答

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可以写吗?我们怎样才能生成它?谢谢 – anddev 2011-10-18 04:32:30

+0

Url =“http://74.208.77.106/jm/testing/iDaddyapi.php?;deviceside=true;interface=wifi”; 你不添加“?”用于传递参数的Webservice名称之后。 现在添加“?”然后再试一次。 – 2011-10-18 05:00:14

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