0

I have used this code snippet to write an AsyncTask class that sends an image to a server. Here is a part of code in doInBackground method that is responsible for sending the image and a few arguments:

    String fileName = params[3];

    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(fileName);

    if (!sourceFile.isFile()) {

        Log.e("debug", "Source File not exist: " + fileName);


    } else {
        try {
            /////////////////////////////////////////////////////////////////////////////
            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(params[0]);

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            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("pic", fileName);

            dos = new DataOutputStream(conn.getOutputStream());

            //here I add some additional data
            addFormField(dos,"idapp", params[1]);
            addFormField(dos,"idlesson", params[2]);
            addFormField(dos,"ip", params[4]);

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"pic\";filename=\"" + fileName + "\"" + lineEnd);
            dos.writeBytes(lineEnd);

            // create a buffer of  maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            String byteArray = "";
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                byteArray += buffer.toString();
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("debug", "HTTP Response is : "
                    + serverResponseMessage + ": " + serverResponseCode);

            BufferedInputStream in;
            try {
                in = new BufferedInputStream(conn.getInputStream());
            } catch (IOException e) {
                String err = (e.getMessage() == null) ? "IOException in creating BufferedInputStream" : e.getMessage();
                Log.e("debug", err);
                return err;
            }

            try {
                resultToDisplay = IOUtils.toString(in, "UTF-8");
                //to [convert][1] byte stream to a string
            } catch (IOException e) {
                Log.e("debug", e.getMessage());
            }

            Log.i("debug", "result: " + resultToDisplay);
            //close the streams //
            //////////////////////////////////////
            fileInputStream.close();
            dos.flush();
            dos.close();
            //////////////////////////////////////
        } catch (MalformedURLException ex) {

            Log.e("debug", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {
            Log.e("debug", "error: " + e.getMessage(), e);
        }

    } // End else block
    return resultToDisplay;

And here is how do I invoke it:

        CallAPI c = new CallAPI();
        c.execute(CallAPI.uploadURL, getResources().getString(R.string.appID), currentLessonID+"", pathToTempFile, CallAPI.IP);

The problem is that according to the response from the server, it recieves no image at all. This is a part of code that is responsible for the answer that I currently recieve:

if(!isset($_FILES['pic'])) ex_fail("pic not set");
if($_FILES['pic']['error'] != 0) ex_fail("pic upload error ");

I get "pic not set" error. Though according to the logs, I actually write the image into DataOutputStream, I compared the bytes count - it is the same.

What could cause the problem?

Sam Stone
  • 477
  • 2
  • 10
  • 33

0 Answers0