如何使用一些参数上传图像

时间:2009-12-25 11:21:47

标签: blackberry java-me web-services

如何使用一些paramerers上传图像.Server有一个web服务,它有一些凭据,它在日志文件中上传图像和写入参数。我写了一些代码但没有成功

在服务器端一。在url中调用的html文件和这个.html文件调用一个用.php编写的脚本,这个php采用一些参数并上传图像。

请告诉我如何在clinet方面处理我的黑莓代码。

客户端代码(在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;
}

服务器代码: -

HTML代码: -

<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代码: -

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


}

?>

由于

Pankaj Pareek

1 个答案:

答案 0 :(得分:2)