2014-02-14 3 views
0

나는 안드로이드 응용 프로그램을해야 서버로 사진을 업로드하는 데 문제가 있습니다. 몇 가지 예제를 시도했지만 작동하지 않습니다.안드로이드에서 우분투 서버를 통해 사진 업로드

내보기는 갤러리 또는 카메라의 이미지를 선택하기위한 버튼으로 구성되어 있으며 다른 버튼을 사용하여 PHP 파일을 통해 서버에 업로드해야합니다.

class ImageGalleryTask extends AsyncTask<Void, Void, String> { 
    protected String doInBackground(Void... unsued) { 
      InputStream is; 
      BitmapFactory.Options bfo; 
      Bitmap bitmapOrg; 
      ByteArrayOutputStream bao ; 

      bfo = new BitmapFactory.Options(); 
      bfo.inSampleSize = 2; 
      //bitmapOrg = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo); 

      bao = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
      byte [] ba = bao.toByteArray(); 
      String ba1 = Base64.encodeBytes(ba); 
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("fotoUp",ba1)); 
      nameValuePairs.add(new BasicNameValuePair("name","image_android")); 
      Log.v("log_tag", System.currentTimeMillis()+".jpg");   
      try{ 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new 
        // Here you need to put your server file address 
        HttpPost("http://xxx.xxx.xxx.xxxx/xxxxxxxxxxxx/upload_photo.php"); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 
        Log.v("log_tag", "Success"); 
       }catch(Exception e){ 
        Log.v("log_tag", "Error in http connection "+e.toString()); 
       } 
     return "Success"; 
     // (null); 
     } 

      @Override 
     protected void onProgressUpdate(Void... unsued) { 
       } 

     @Override 
     protected void onPostExecute(String sResponse) { 
     try { 
      if (dialog.isShowing()) 
       dialog.dismiss(); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), 
        e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
      Log.e(e.getClass().getName(), e.getMessage(), e); 
     } 
    } 
     } 

와 PHP는 다음과 같습니다 :

$ruta = "photos/" . basename($_FILES['fotoUp']['name']); 
    if(move_uploaded_file($_FILES['fotoUp']['tmp_name'], $ruta)) 
    chmod ("uploads/".basename($_FILES['fotoUp']['name']), 0644); 

응용 프로그램이 작동을하지만 이미지가 서버에 업로드하지 않는

내 Android 코드는 다음과 같다. 우분투 서버에서 권한을 777로 변경합니다.

사진을 업로드 할 수있는 내 우분투 서버 폴더는 var/www/xxxx/photos에 있으며 PHP 파일은 var/www/xxx/upload_photo.php에 있습니다

또한 mySQL 데이터베이스에 저장되는 경로를 어떻게 저장할 수 있는지 알고 있습니다.

도움 주셔서 감사합니다.

답변

0

이것은 내 업로드 코드이며, 작업 내용입니다. 당신은 가져올 필요 httpmime jar

PHP 코드

$uploads_dir = '/Library/WebServer/Documents/Upload/upload/'.$_FILES['userfile']['name']; 
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { 
    echo $_POST["contentString"]."\n"; 
    echo "File path = ".$uploads_dir; 
    move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $uploads_dir); 
} else { 
    echo "\n Upload Error"; 
    echo "filename '". $_FILES['userfile']['tmp_name'] . "'."; 
    print_r($_FILES); 

자바 코드 나는 다음과 같은 예제 사진을 업로드 가지고 마무리에

HttpClient client = new DefaultHttpClient(); 
HttpPost postMethod = new HttpPost("http://localhost/Upload/index.php"); 

File file = new File(filePath); 

MultipartEntity entity = new MultipartEntity(); 
FileBody contentFile = new FileBody(file); 
entity.addPart("userfile",contentFile); 

StringBody contentString = new StringBody("This is contentString"); 
entity.addPart("contentString",contentString); 

postMethod.setEntity(entity); 
HttpResponse response = client.execute(postMethod); 
HttpEntity httpEntity = response.getEntity(); 
String state = EntityUtils.toString(httpEntity);