2012-01-20 30 views
3

この特定のサーバ側のヘルプphpコード私はPHPの知識がなく、アンドロイドから3つの画像をこのphpページにアップロードする必要があります。androidからPHPサーバに複数の画像をアップロードする

私は多くの方法を試してそれを検索しましたが、チュートリアルや何も私のアンドロイドコードが適切に動作していなかったのです。 DNSも構成されていますが、イメージはサーバー側に表示されません。 Javaコードで私を助けてください。

PHP:

<?php 
if ($_FILES["file1"]["error"] > 0) 
{ 
    header("HTTP/1.1 400 Bad Request"); 
    echo "Error: " . $_FILES["file1"]["error"] . "<br />"; 
} 
else if ($_FILES["file2"]["error"] > 0) 
{ 
    header("HTTP/1.1 400 Bad Request"); 
    echo "Error: " . $_FILES["file1"]["error"] . "<br />"; 
} 
else if ($_FILES["file3"]["error"] > 0) 
{ 
    header("HTTP/1.1 400 Bad Request"); 
    echo "Error: " . $_FILES["file1"]["error"] . "<br />"; 
} 
else 
{ 
    if ($_FILES["file1"]["error"] > 0) 
    { 
     echo "Error: " . $_FILES["file1"]["error"] . "<br />"; 
    } 
    else 
    { 
     echo "Upload: " . $_FILES["file1"]["name"] . "<br />"; 
     echo "Type: " . $_FILES["file1"]["type"] . "<br />"; 
     echo "Size: " . ($_FILES["file1"]["size"]/1024) . " Kb<br />"; 
     echo "Stored in: " . $_FILES["file1"]["tmp_name"]. "<br />"; 
    } 

    //$target_path = "uploads/"; 
    $target_path = "elp/pendingimages/"; 

    $target_path = $target_path . basename($_FILES['file1']['name']); 

    if(move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) { 
     echo "The file ". basename($_FILES['file1']['name']). 
      " has been uploaded"; 
    } 
    else{ 
     echo "There was an error uploading the file, please try again!"; 
    } 

    if ($_FILES["file2"]["error"] > 0) 
    { 
     echo "Error: " . $_FILES["file2"]["error"] . "<br />"; 
    } 
    else 
    { 
     echo "Upload: " . $_FILES["file2"]["name"] . "<br />"; 
     echo "Type: " . $_FILES["file2"]["type"] . "<br />"; 
     echo "Size: " . ($_FILES["file2"]["size"]/1024) . " Kb<br />"; 
     echo "Stored in: " . $_FILES["file2"]["tmp_name"]. "<br />"; 
    } 

    //$target_path = "uploads/"; 
    $target_path = "elp/pendingimages/"; 

    $target_path = $target_path . basename($_FILES['file2']['name']); 

    if(move_uploaded_file($_FILES['file2']['tmp_name'], $target_path)) { 
     echo "The file ". basename($_FILES['file2']['name']). 
     " has been uploaded"; 
    } 
    else{ 
     echo "There was an error uploading the file, please try again!"; 
    } 

    if ($_FILES["file3"]["error"] > 0) 
    { 
     echo "Error: " . $_FILES["file3"]["error"] . "<br />"; 
    } 
    else 
    { 
     echo "Upload: " . $_FILES["file3"]["name"] . "<br />"; 
     echo "Type: " . $_FILES["file3"]["type"] . "<br />"; 
     echo "Size: " . ($_FILES["file3"]["size"]/1024) . " Kb<br />"; 
     echo "Stored in: " . $_FILES["file3"]["tmp_name"]. "<br />"; 
    } 


    //$target_path = "uploads/"; 
    $target_path = "elp/pendingimages/"; 

    $target_path = $target_path . basename($_FILES['file3']['name']); 

    if(move_uploaded_file($_FILES['file3']['tmp_name'], $target_path)) { 
     echo "The file ". basename($_FILES['file3']['name']). 
      " has been uploaded"; 
    } 
    else{ 
     echo "There was an error uploading the file, please try again!"; 
    } 
} 

?> 

のJava:

public class TryprojectActivity extends Activity { 
    InputStream is; 
    int pic_count = 0; 
    Bitmap bitmap=null; 
    FileInputStream in1,in2,in3; 
    BufferedInputStream buf; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

     try { 
      in1 = new FileInputStream("/sdcard/1.jpg"); 
     } 
     catch (FileNotFoundException e2) { 
     // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 

     try { 
      in2 = new FileInputStream("/sdcard/2.jpg"); 
     } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    try { 
     in3 = new FileInputStream("/sdcard/3.jpg"); 
    } 
    catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    Bitmap bitmapOrg1 = BitmapFactory.decodeStream(in1); 
    ByteArrayOutputStream bao1 = new ByteArrayOutputStream(); 
    bitmapOrg1.compress(Bitmap.CompressFormat.JPEG, 90, bao1); 
    byte [] imagearray1 = bao1.toByteArray(); 
    String ba1=Base64.encode(imagearray1); 

    Bitmap bitmapOrg2 = BitmapFactory.decodeStream(in2); 
    ByteArrayOutputStream bao2 = new ByteArrayOutputStream(); 
    bitmapOrg2.compress(Bitmap.CompressFormat.JPEG, 90, bao2); 
    byte [] imagearray2 = bao2.toByteArray(); 
    String ba2=Base64.encode(imagearray2); 

    Bitmap bitmapOrg3 = BitmapFactory.decodeStream(in3); 
    ByteArrayOutputStream bao3 = new ByteArrayOutputStream(); 
    bitmapOrg3.compress(Bitmap.CompressFormat.JPEG, 90, bao3); 
    byte [] imagearray3 = bao3.toByteArray(); 
    String ba3=Base64.encode(imagearray3); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 

    nameValuePairs.add(new BasicNameValuePair("image1",ba1)); 
    nameValuePairs.add(new BasicNameValuePair("image2",ba2)); 
    nameValuePairs.add(new BasicNameValuePair("image3",ba3)); 

    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://helpdesk.cispl.com/upload_file.php"); 
     UrlEncodedFormEntity obj = new UrlEncodedFormEntity(nameValuePairs); 
     obj.setChunked(true); 
     httppost.setEntity(obj); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     //is = entity.getContent(); 
     httpclient.getConnectionManager().shutdown(); 
    } 
    catch(Exception e){ 
     //CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString()); 
     //CommonFunctions.showToast(ctx, "Unable to post captured image file: " + 
     //e.toString()); 
    } 
} 
+0

にそれらの画像を複数の画像を送信するには、私の記事をチェックしてくださいあなたにアップロードを行う方法、HTTPを使用してアップロードする必要がありますか? (あなたがJavaに言及して以来) – greut

+0

私はアンドロイドアプリで必要なJavaのアクティビティコードを意味します。私はhttpとmulitpartを使ってアップロードするのに疲れましたが、使用はありません。これは私がphpについて知らずにコードしようとしているために起こっているかもしれません。 – user1160020

+0

コードを教えてください。 – greut

答えて

2

あなたのPHPが正しいように見えます。

デバイスでは、データタイプがMultipartEntityのHTTP POST要求を使用します。あなたはMultipartEntityランニングを取得するために追加のライブラリをダウンロードする必要があります

:もっとhere

EDIT私のリンクから

例を読みます!

1)httpcomponents-client-4.1.zipをhttp://james.apache.org/download.cgi#Apache_Mime4Jからダウンロードし、apache-mime4j-0.6.1.jarをプロジェクトに追加してください。

2)httpcomponents-client-4.1-bin.zipをhttp://hc.apache.org/downloads.cgiからダウンロードし、httpclient-4.1.jar、httpcore-4.1.jarおよびhttpmime-4.1.jarをプロジェクトに追加します。

3)以下のサンプルコードを使用してください。

 


    private DefaultHttpClient mHttpClient; 


    public ServerCommunication() { 
     HttpParams params = new BasicHttpParams(); 
     params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
     mHttpClient = new DefaultHttpClient(params); 
    } 


    public void uploadUserPhoto(File image1, File image2, File image3) { 

     try { 

      HttpPost httppost = new HttpPost("some url"); 

      MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      multipartEntity.addPart("Title", new StringBody("Title")); 
      multipartEntity.addPart("Nick", new StringBody("Nick")); 
      multipartEntity.addPart("Email", new StringBody("Email")); 
      multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT)); 
      multipartEntity.addPart("file1", new FileBody(image1)); 
      multipartEntity.addPart("file2", new FileBody(image2)); 
      multipartEntity.addPart("file3", new FileBody(image3)); 
      httppost.setEntity(multipartEntity); 

      mHttpClient.execute(httppost, new PhotoUploadResponseHandler()); 

     } catch (Exception e) { 
      Log.e(ServerCommunication.class.getName(), e.getLocalizedMessage(), e); 
     } 
    } 

    private class PhotoUploadResponseHandler implements ResponseHandler { 

     @Override 
     public Object handleResponse(HttpResponse response) 
       throws ClientProtocolException, IOException { 

      HttpEntity r_entity = response.getEntity(); 
      String responseString = EntityUtils.toString(r_entity); 
      Log.d("UPLOAD", responseString); 

      return null; 
     } 

    } 


 
+0

私は3つすべての画像をbyteArrayに変換してから、リストに追加します。私はそれの後に何をしなければならないのですか?または私は間違った方法で行くつもりですか? – user1160020

+0

in1 =新しいFileInputStream( "/ sdcard/1.jpg"); ビットマップbitmapOrg1 = BitmapFactory.decodeStream(in1); ByteArrayOutputStream bao1 = new ByteArrayOutputStream(); bitmapOrg1.compress(Bitmap.CompressFormat.JPEG、90、bao1); \t byte [] imagearray1 = bao1.toByteArray(); 文字列ba1 = Base64.encode(imagearray1); これは3つの画像すべてに対して行っています – user1160020

+0

私の例を見てください。このアプローチにより、MultipartEntityはあなたのためにすべてを行います。ファイルからFileBodyを作成するだけで(イメージからFileを作成する)必要があります。 –

0

すべてのコードの最初には、次のように最適化することができます

$files = array('file1', 'file2', 'file3'); 
$path = 'elp/pendingimages/'; 

foreach ($files as $file) { 
    if ($_FILES[$file]['error'] > 0) { 
     echo 'Error: '. $_FILES[$file]['error'] .'<br />'; 
    } 
    else { 
     echo 'Upload: '. $_FILES[$file]['name'] .'<br />'; 
     echo 'Type: '. $_FILES[$file]['type'] .'<br />'; 
     echo 'Size: '. ($_FILES[$file]['size']/1024) .' Kb<br />'; 
     echo 'Stored in: '. $_FILES[$file]['tmp_name'] .'<br />'; 
    } 

    $basename = basename($_FILES[$file]['name']); 

    if (move_uploaded_file($_FILES[$file]['tmp_name'], $path . $basename) { 
     echo "The file {$basename} has been uploaded"; 
    } 
    else { 
     echo 'There was an error uploading the file, please try again!'; 
    } 
} 

あなたはファイルごとに別のフィールドを使用している場合、それは大丈夫です。

次それは、複数のアップロードだとき、あなたは自分自身に何$ _FILES配列の店を見ることができます:

$_FILES = array(
    ['files'] => array(
     ['name'] => array(
      [0] => 'WALL_video.jpg' 
      [1] => 'WALLc.jpg' 
     ) 
     ['type'] => array(
      [0] => 'image/jpeg' 
      [1] => 'image/jpeg' 
     ) 
     ['tmp_name'] => array(
      [0] => '/tmp/phpnbKcdM' 
      [1] => '/tmp/phpnrHSN1' 
     ) 
     ['error'] => array(
      [0] => 0 
      [1] => 0 
     ) 
     ['size'] => array(
      [0] => 885968 
      [1] => 839713 
     ) 
    ) 
) 

あなたはファイルの配列としてfiles[]のような名前の一つのフィールドを使用している場合、次のコードは、あなたのために動作します。

$target_path = 'elp/pendingimages/'; 

foreach ($_FILES['files']['name'] as $index => $name) { 
    if ($_FILES['files']['error'][$index] > 0) { 
     echo 'Error: ' . $_FILES['files']['error'][$index] . '<br />'; 
    } 
    else { 
     echo 'Upload: '. $_FILES['files']['name'][$index] .'<br />'; 
     echo 'Type: '. $_FILES['files']['type'][$index] .'<br />'; 
     echo 'Size: '. ($_FILES['files']['size'][$index]/1024) .' Kb<br />'; 
     echo 'Stored in: '. $_FILES['files']['tmp_name'][$index] .'<br />'; 
    } 

    $path = $target_path . basename($name); 

    if (move_uploaded_file($_FILES['files']['tmp_name'][$index], $path) { 
     echo "The file {$name} has been uploaded"; 
    } 
    else { 
     echo 'There was an error uploading the file, please try again!'; 
    } 
} 
関連する問題