من توسط این تاپیک و با کدهایی که آقای شهرکی گذاشته بودن سعی کردم از گوشی به سرور عکس آپلود کنم ولی مشکلی که وجود داره اینه که هیچ عکسی به سرور نمیره و وقتی var_dump میگیرم از متغییر $_FILES هیچ فایلی ارسال نشده
کلاس UploadImage
package WebServer;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLCoection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import ir.amlakmadar.amlakmadar.App;
public class UploadImage {
private String destination;
private String source;
private ProgressDialog dialog;
private int BUFFER_SIZE=8 * 1024;
public UploadImage setSource(String source) {
this.source = source;
return this;
}
public UploadImage setDestination(String destination) {
this.destination = destination;
return this;
}
public UploadImage setDialog(ProgressDialog dialog) {
this.dialog = dialog;
return this;
}
public String upload() {
final String[] result = {""};
final File sourceFile = new File(source);
if (!sourceFile.isFile()) {
dialog.dismiss();
} else {
Thread thread = new Thread(new Ruable() {
@Override
public void run() {
try {
HttpURLCoection coection;
DataOutputStream dataOutputStream;
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesAvailable, bufferSize;
byte[] buffer;
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(App.Url+"action=upimg");
coection = (HttpURLCoection) url.openCoection();
coection.setDoInput(true);
coection.setDoOutput(true);
coection.setUseCaches(false);
coection.setChunkedStreamingMode(BUFFER_SIZE);
coection.setRequestMethod("POST");
coection.setRequestProperty("Coection", "Keep-alive");
coection.setRequestProperty("Cache-Control", "no-cache");
coection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
coection.setRequestProperty("photo", destination);
dataOutputStream = new DataOutputStream(coection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name="photo";filename="" + destination + """ + lineEnd);
dataOutputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,BUFFER_SIZE);
buffer = new byte[bufferSize];
while (fileInputStream.read(buffer, 0, bufferSize) > 0) {
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,BUFFER_SIZE);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
coection.coect();
InputStream inputStream = coection.getInputStream();
result[0] = HelperString.convertInputStreamToString(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
while (thread.isAlive()) ;
}
return result[0];
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK && data != null) {
Uri picUri = data.getData();
ImageView img = (ImageView) findViewById(R.id.btn_one_add_image_src);
img.setImageURI(picUri);
img.setVisibility(View.VISIBLE);
String imagePath = HelperString.getRealPathFromUri(picUri);
String imagePathIndex = MediaStore.Images.Media.DATA;
Cursor cursor = getContentResolver().query(picUri, new String[]{imagePathIndex}, null, null, null);
cursor.moveToFirst();
String fileName = cursor.getString(cursor.getColumnIndex(imagePathIndex));
new AsyncUploadPhoto(imagePath, "photo.jpg").execute();
//imgLogo.setImageBitmap(BitmapFactory.decodeFile(fileName));
}
}
}
class AsyncUploadPhoto extends AsyncTask<String, String, String> {
private String source;
private String destination;
private ProgressDialog progressDialog;
public AsyncUploadPhoto(String source, String destination) {
this.source = source;
this.destination = destination;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(App.ACTIVITY);
progressDialog.setTitle("آپلود عکس به سرور");
progressDialog.setMessage("لطفاً صبر کنید...");
progressDialog.show();
}
@Override
protected String doInBackground(String... strings) {
return new UploadImage()
.setSource(source)
.setDestination(destination)
.setDialog(progressDialog)
.upload();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
App.Toast(s);
try {
JSONObject json = new JSONObject(s);
if (json.getInt("status") == 1) {
Toast.makeText(App.context, "تصویر با موفقیت روی سرور آپلود شد.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(App.context, "در زمان آپلود تصویر به سرور خطایی رخ داد. مجدداً تلاش کنید.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
progressDialog.dismiss();
}
}