2009-12-25 5 views
1

로 이미지를 업로드하는 방법 몇 가지 자격 증명이 하나의 웹 서비스를 가지고 있으며 성공을어떤 paramerers.Server로 이미지를 업로드하는 방법 몇 가지 paramerers

에이 이미지를 업로드하고 코드를 작성 믿을수 로그에 매개 변수를 쓰기 만 못하고 서버 측. html 파일은 .html 파일이며 .php로 작성된 하나의 스크립트를 호출하고이 PHP는 일부 paramerts를 가져 와서 이미지를 업로드합니다.

내가 내 머리 글자면에서 내 블랙 베리 코드로 어떻게 할 수 있는지 말해주십시오. (자바)

클라이언트 코드 : -

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


} 

?> 

감사합니다

판 카지 Pareek는

+2

, 신 u를 축복 당신이 내 일 친구를 만들어 ... 더 읽을 수 있도록 – miku

답변

관련 문제