gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:41:03 25 4
gpt4 key购买 nike

我必须做一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com