I try to send a File to a PHP Server.
Given method:
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
45 if (is_uploaded_file($_FILES['file']['tmp_name'])) {
if (strlen($sWorkSpacePath) > 0) {
$uploadfile = $sWorkSpacePath . "/" . basename($_FILES['file']['name']);
} else {
$uploadfile = $doc_root . "/temp/" . basename($_FILES['file']['name']);
}
}
I've take a look at this Thread:
Uploading file in php server from android device
But the Server still not receive the File
Here is my Code. I think the mistake is while setting the RequestProperty
@Override
protected Boolean doInBackground(Void... params)
if (cd.isConnectingToInternet()) {
String sURL = "http://test.com/android.php?fx=1232&ids=" + sessionId + "&id_task=" + jobid;
File file = new File(getFilesDir(), jobid + ".kx_task");
try {
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(getFilesDir(), jobid + ".kx_task");
if (sourceFile.isFile()) {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(sURL);
//Starte HTTP Verbindung zur URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); //Erlaubt input
conn.setDoOutput(true); //Erlaubt output
conn.setUseCaches(false); //Keine Cache kopie
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE",
"multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("bill", file.getAbsolutePath());
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name =\"bill\";tmp_name=\""
+ file.getAbsolutePath() + "\"" + lineEnd);
dos.writeBytes(lineEnd);
//Erstelle einen Buffer von maximaler Größe
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
//Lese Datei und schreibe sie in...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead>0) {
dos.write(buffer,0,bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
//sende multipart form daten die notwendig sind nach der datei
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
fileInputStream.close();
dos.flush();
dos.close();
}