在Android上通过php将照片上传到Ubuntu服务器
问题描述:
我必须做一个Android应用程序,我有一个上传照片到服务器的问题。我试过一些例子,但不起作用。在Android上通过php将照片上传到Ubuntu服务器
我的观点包含一个按钮,用于从图库或照相机中选择图像,然后用另一个按钮,我必须通过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);
答
在完成我上传的照片下面的例子中,我把代码是否有必要为别人: http://www.internetria.com/blog/2013/04/12/android-enviar-imagenes-por-webservice/
这是西班牙文,但如果有任何疑问,请问我。