0

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();
                }
Community
  • 1
  • 1
korunos
  • 768
  • 3
  • 11
  • 31

2 Answers2

0

I think you should have to try this : As PHP webservice expecting a file instead of Stream

Try this :

MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("multifile[]", new FileBody(new File(
                        path), "image/jpeg"));
Httppost.setEntity(multipartEntity);
Abhijeet
  • 392
  • 2
  • 13
0

I would suggest using one the best networking libraries for Android, okhttp.

And here is an example of how you can upload a file:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("param-name", fileName, RequestBody.create(MediaType.parse(contentType), yourFile)
            .build();
com.squareup.okhttp.Request request = new com.squareup.okhttp.Request.Builder()
            .url(url)
            .post(requestBody)
            .build();
Response response = okhttpClient.newCall(request).execute();
moud
  • 729
  • 9
  • 19
  • Thank you so far! I'' give it a try. Maybe this works for me... do you know what i have to put in "param_name"? – korunos Aug 06 '15 at 09:24
  • @korunos you should replace it with the name expected by your web service for the file, like you did for "boundry". I also suggest you test your endpoint using "postman" for example to make sure your webservice works properly. – moud Aug 06 '15 at 09:28