Hier sind ein paar Beispiele für PHP und Java-Codes, die mit Android arbeiten. Sehen Sie sich den Android Java-Beispielcode unter dem PHP-Code an.
PHP (upload.php):
<?php
$attachment = $_FILES['attachment'];
define ("MAX_SIZE","9000");
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
// Method to extract the uploaded file extention
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// Method to extract the uploaded file parameters
function getFileAttributes($file)
{
$file_ary = array();
$file_count = count($file['name']);
$file_key = array_keys($file);
for($i=0;$i<$file_count;$i++)
{
foreach($file_key as $val)
{
$file_ary[$i][$val] = $file[$val][$i];
}
}
return $file_ary;
}
// Check if the POST Global variable were set
if(!empty($attachment) && isset($_POST['dirname']))
{
$dirname = $_POST['dirname'];
$uploaddir = "/var/www/html/uploads/".$dirname."/";
//Check if the directory already exists.
if(!is_dir($uploaddir)){
//Directory does not exist, so create it.
mkdir($uploaddir, 0777, true);
}
$file_attributes = getFileAttributes($attachment);
//print_r($file_attributes);
$count = count($file_attributes);
$response["status"] = array();
$response["count"] = $count; // Count the number of files uploaded
array_push($response["status"], $file_attributes);
$file_dirs = array();
foreach($file_attributes as $val)
{
$old_file = $val['name'];
$ext = getExtension($old_file);
$ext = strtolower($ext);
if(in_array($ext, $valid_formats))
{
$response["files"] = array();
$new_file = date('YmdHis',time()).mt_rand(10,99).'.'.$ext;
move_uploaded_file($val['tmp_name'], $uploaddir.$new_file);
$file_dirs[] = 'http://192.168.50.10/gallery/uploads/'.$dirname."/".$new_file;
}
else
{
$file_dirs[] = 'Invalid file: '.$old_file;
}
}
array_push($response["files"], $file_dirs);
echo json_encode($response);
}
?>
Android:
Die Upload-Service-Schnittstelle FileUploadService.java:
public interface FileUploadService {
@Multipart
@POST("upload.php")
Call<ResponseBody> uploadMultipleFilesDynamic(
@PartMap() Map<String, RequestBody> partMap, /* Associated with 'dirname' POST variable */
@Part List<MultipartBody.Part> files); /* Associated with 'attachment[]' POST variable */
}
Die uploadPhotos() Methode auf einer anderen Klasse, z.B. UploadActivity.java:
public void uploadPhotos(final List<Uri> uriList, final String folderName)
{
List<MultipartBody.Part> parts = new ArrayList<>();
for(Uri uri: uriList){
parts.add(prepareFilePart("attachment[]", uri)); // Note: attachment[]. Not attachment
}
RequestBody requestBodyFolderName = createPartFromString(folderName);
HashMap<String, RequestBody> requestMap = new HashMap<>();
requestMap.put("dirname", requestBodyFolderName); // Note: dirname
FileUploadService service = RetrofitClient.getApiService();
Call<ResponseBody> call = service.uploadMultipleFilesDynamic(requestMap, parts);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.code() == 200)
{
// multiple dynamic uploads were successful
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
// errors
}
});
}
Die prepareFilePart() Methode:
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
File file = new File(fileUri.getPath());
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
// MultipartBody.Part is used to send also the actual file name
return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}
Die createPartFromString() Methode:
private RequestBody createPartFromString(String descriptionString) {
return RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
}
Und schließlich die RetrofitClient.java Klasse :
public class RetrofitClient {
private static final String ROOT_URL = "http://192.168.50.10/gallery/";
public RetrofitClient() { }
private static Retrofit getRetroClient() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static FileUploadService getApiService() {
return getRetroClient().create(FileUploadService.class);
}
}
nicht auf Android –
ich die Antwort wirklich schlecht, so muß im Zusammenhang i relevant android Anzeige geschrieben, weil vielleicht nicht nur ein PHP-Entwickler wissen, was diese über –
ist nicht brauchen, von PHP zu ändern, wenn Sie Bild von regulärer Post-Methode erhalten ist hinter –