php - Android中通过php上传照片到Ubuntu服务器

标签 php android mysql image

我必须做一个 Android 应用程序,我在将照片上传到服务器时遇到问题。我尝试了一些示例,但不起作用。

我的 View 包含一个用于从图库或相机中选择图像的按钮,然后使用另一个按钮我必须通过 php 文件上传到服务器。

我的 Android 代码如下:

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);

应用程序可以运行,但图像无法上传到服务器。我将Ubuntu服务器中的权限更改为777。

我可以上传照片的 Ubuntu 服务器文件夹位于 var/www/xxxx/photos 中,php 文件位于 var/www/xxx/upload_photo.php

我还知道如何保存 mySQL 数据库中存储的路径。

感谢您的帮助。

最佳答案

这是我的上传代码,它可以工作。您需要导入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);

JAVA代码

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);

关于php - Android中通过php上传照片到Ubuntu服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21768838/

相关文章:

python - 我尝试安装 "pip install mysqlclient"但每次都失败。尝试了所有其他方法,但也没有成功。我应该怎么办?

java - 我在哪里可以找到多种语言的正则表达式的单元测试?

php - 使用多个值的内连接查询

php - 什么是 <?xml version ="1.0"?>?

android - React Native 实时获取数据

php - 警告 : mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool 值在 ../第 7 行给出

php - "Liking"PHP/MySQL 中的个别评论

android - 如果没有 @Provides 方法,则无法提供 Dagger 2 Named

android - 如何在 Flutter 中使 Expanded 中的文本可滚动

php - 创建包含来自 mySQL 数据源的数组和对象的 JSON