views:

247

answers:

1

Hello,

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.

Please tell me how can i do with my blackberry code at clinet side.

Client Code(in java):-

   private final String CrLf = "\r\n";
   private String httpConn(String file){
   HttpConnection conn = null;
   OutputStream os = null;
   InputStream is = null;

   String url = "";
   String result="test";

   url="http://www.test.com/demo.html";      

    try{

        String login = "usertest:passtest";
        //Encode the login information in Base64 format.
        byte[] encoded = Base64OutputStream.encode(login.getBytes(), 0, login.length(), false, false);


        conn = (HttpConnection)Connector.open(url);
        conn.setRequestMethod(HttpConnection.POST);


        String postData = "";

        String name="file:///" + file;
        FileConnection fc= (FileConnection)Connector.open(name);
           is=fc.openInputStream();


         byte[] ReimgData = IOUtilities.streamToBytes(is);

        //Resize Image according to setting.
        byte[] imgData= reszieImage(ReimgData);
        is.read(imgData);


        String message1 = "";
        message1 += "-----------------------------4664151417711" + CrLf;
        message1 += "Content-Disposition: form-data; name=\"image\"; filename=\"" + bef.getText() + "\"" + CrLf;

        message1 += "Content-Type: image/jpeg" + CrLf;
        message1 += CrLf;

        // the image is sent between the messages in the multipart message.

        String message2 = "";
        message2 += CrLf + "-----------------------------4664151417711--" + CrLf;          


        URLEncodedPostData _postData = new URLEncodedPostData("",false);
           _postData.append("filename","test.jpg");
           _postData.append("tag","tag");
           _postData.append("status","st");
           _postData.append("deviceid","di");
           _postData.append("devicemodel","dm");
          // _postData.append("image",new String(imgData));

           String encodedData = _postData.toString();

        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");

        // might not need to specify the content-length when sending chunked data.
         conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length )));
         conn.setRequestProperty("Authorization", "Basic " + new String(encoded));
       // System.out.println("open os");
        os = conn.openOutputStream();

        os.write(message1.getBytes());

        // 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<imgData.length);


        os.write(message2.getBytes());
       // os.write(_postData.getBytes());
        os.flush();


        //Response from webservice..
        is = conn.openInputStream();



       char buff = 512;
        int len;
        byte []data = new byte[buff];
        do{
           // System.out.println("READ");
            len = is.read(data);

            if(len > 0){
               result="1"+Integer.toString(len);
            }
        }while(len>0);
        result="2"+Integer.toString(len);


    }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 result;
}

Server Code:-

HTML code:-

<html>
<title>Image Upload App Test</title>

<body>
    <h2>Image Upload</h2>
<form id="image_upload" enctype="multipart/form-data" action="http://sys7/brij/test.php" method="post" class="browse">
    <p>
        <label>Upload:</label>
        <input id="multifile" type="file" name="image" />
        <input name="filename" type="text" value="" size="50" maxlength="128" />

        <input name="tag" type="text" value="" size="50" maxlength="128" />
        <input name="deviceid" type="text" value="" size="50" maxlength="128" />
        <input name="devicemodel" type="text" value="" size="50" maxlength="128" />

        <input class="button" type="submit" name="image_upload" value="Upload" />
    </p>
</form>  

</body>
</html>

PHP Code:-

<?php

$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}";


}

?>

Thanks

Pankaj Pareek