Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
pankajpareek
Posts: 68
Registered: ‎09-29-2009
My Device: Not Specified
Accepted Solution

upload image on server using post parameter.

Hello experts,

          i want to upload one image on server using post method with alongsome parameter like file name,status,tag,deviceid,device name.

 

server  taking some windows credentials also. I am connecting one .html page  which in my url it takes all the paramter and in action it goes to .php file and it upload image on server side.

 

I am using this code, i ma trying from last 3 days but i am not getting success.

Please Help me...

 

My code is:-

 

 

private String httpConn(String file){ HttpConnection conn = null; OutputStream os = null; InputStream is = null; String url = ""; int respcode=0; //web service path where image will be uploaded. url="http://usertest:smileytongue:asstest@demo.possage.com/admin/image_upload_iphone_app.html"; try{ conn = (HttpConnection)Connector.open(url); conn.setRequestMethod(HttpConnection.POST); String name="file:///" + file; FileConnection fc= (FileConnection)Connector.open(name); is=fc.openInputStream(); /* byte[] imgData = IOUtilities.streamToBytes(is); is.read(imgData);*/ byte[] ReimgData = IOUtilities.streamToBytes(is); //Resize Image according to setting. byte[] imgData= reszieImage(ReimgData); is.read(imgData); String boundary="---------------------------14737809831466499882746641449"; String body = ""; //filename.. body="\r\n--" +boundary+"\r\n"; body+= "Content-Disposition: form-data; name=\"filename\"\r\n\r\n"+"test.jpg"; body+="\r\n--" +boundary+"\r\n"; //tag.. body="\r\n--" +boundary+"\r\n"; body+= "Content-Disposition: form-data; name=\"tag\"\r\n\r\n"+"tag"; body+="\r\n--" +boundary+"\r\n"; //status.. body="\r\n--" +boundary+"\r\n"; body+= "Content-Disposition: form-data; name=\"status\"\r\n\r\n"+"status"; body+="\r\n--" +boundary+"\r\n"; //deviceid.. body="\r\n--" +boundary+"\r\n"; body+= "Content-Disposition: form-data; name=\"deviceid\"\r\n\r\n"+"deviceid"; body+="\r\n--" +boundary+"\r\n"; //devicemodel.. body="\r\n--" +boundary+"\r\n"; body+= "Content-Disposition: form-data; name=\"devicemodel\"\r\n\r\n"+"devicemodel"; body+="\r\n--" +boundary+"\r\n"; //image.. body="\r\n--" +boundary+"\r\n"; body+= "Content-Disposition: form-data;name=\"image\"; filename=\""+"test.jpg"+"\r\n"; body+="Content-Type: application/octet-stream\r\n\r\n"; body+=new String(imgData); body+="\r\n--" +boundary+"--"+"\r\n"; conn.setRequestProperty("Content-Type", "multipart/form-data;"+boundary); os = conn.openOutputStream(); os.write(body.getBytes()); os.close(); /* // SEND THE IMAGE int index = 0; int size = 1024; do{ System.out.println("write:" + index); if((index+size)>imgData.length){ size = imgData.length - index; } os.write(imgData, index, size); index+=size; }while(index

0){ } }while(len>0);*/ respcode=conn.getResponseCode(); }catch(Exception e){ e.printStackTrace(); }finally{ //System.out.println("Close connection"); try{ os.close(); }catch(Exception e){} try{ is.close(); }catch(Exception e){} try{ conn.close(); }catch(Exception e){} } return Integer.toString(respcode); }

 

 

Regards

Pankaj Pareek.

 

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: upload image on server using post parameter.

[ Edited ]

This looks like a repeat of a previous Thread you were involved in:

 

http://supportforums.blackberry.com/t5/Java-Development/How-to-upload-image-on-server/m-p/378162#M71...

 

Just to make sure we don't cover the same ground, have you written the Serverside processing that will accept the image?

Please use plain text.
Developer
pankajpareek
Posts: 68
Registered: ‎09-29-2009
My Device: Not Specified

Re: upload image on server using post parameter.

Hello,

                   i am unaware of  server side code.I know only taht server using .php code which is calling from .html file.

and this .html i am calling from httpconnection.

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: upload image on server using post parameter.

"i am unaware of  server side code"

 

Then you can't do it, you do not enough information.  The people who wrote the Server Side processing must tell you what you should do in your code.  Go talk to them.  There is no point talking to us until you know what the Server is expecting.

 

Remember also that this is a BlackBerry forum, so you should not expect answers here to questions like "what are http headers?", and "what does HTTP code nnn really mean?". 

Please use plain text.
Developer
pankajpareek
Posts: 68
Registered: ‎09-29-2009
My Device: Not Specified

Re: upload image on server using post parameter.

How to upload image with some paramerers.Server has one webservice which has some credentials and it uploading image and write parameters in log file.I write some code but not getting success

On server side one . html file which is calling in url and this .html file calling one script which is written in .php and this php taking some paramerts and it uploading image.


Server Side code:-



$filename = $_POST['filename'];
$tag = $_POST['tag'];
$status = $_POST['status'];
$deviceid = $_POST['deviceid'];
$devicemodel = $_POST['devicemodel'];

$fp=fopen('./test/log.txt','w');
fwrite($fp,$filename,strlen($filename));
fwrite($fp,$tag ,strlen($tag ));
fwrite($fp,$deviceid ,strlen($deviceid));
fwrite($fp,$devicemodel ,strlen($devicemodel));
fwrite($fp,$status ,strlen($status));
fclose($fp);


$uploaddir = './test/';
$file = basename($_FILES['image']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
        echo "http://iphone.zcentric.com/uploads/{$file}";


}

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: upload image on server using post parameter.

I made the comment earlier that you should not expect answers to http questions.  Similarly, ability to read and write php is not too useful on a BlackBerry device, so I would next expect much expertise on the BlackBerry Java forum. 

 

I think you need to analyze the php and turn it into a POST request, so figure out the post data that you need to include in the http POST.

 

Another way to do this would be to get someone to write an html page for you that submitted data to this Server Code.  In fact I suspect you already have this, so that would be more useful than this php code.

 

You will find people on the forum who have written php but I think it would be much more useful for you to understand the process.  So you could also look up the php documentation and the html documentation and figure out what is going on at the server end.  Perhaps even write some php yourself.  Then you could help the next person who comes along with a request like this!

Please use plain text.
Developer
pankajpareek
Posts: 68
Registered: ‎09-29-2009
My Device: Not Specified

Re: upload image on server using post parameter.

Please use plain text.
Developer
n8r0n
Posts: 40
Registered: ‎03-29-2009
My Device: Storm 9550

Re: upload image on server using post parameter.

Just for the benefit of people who stumble on this thread .... I'm using the HttpMultipartRequest class linked to in the previous post.  It works fine for very small uploads (< 10KB for me), but does not work for larger uploads, when built into my BlackBerry app.  The code in question is just generic J2ME code, and is not necessarily tested to work correctly on BlackBerries, with arbitrary file upload sizes.

 

If I figure out how to modify this class to work for BlackBerry apps, I'll try to remember to post what I learn here.  But, I wanted people to know that they shouldn't expect this "solution" to necessarily solve their problem, without further work.

Please use plain text.