0

private void doFileUpload(String exsistingFileName) {

    System.out.println("exsistingFileName: " + exsistingFileName);

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    int bytesRead, bytesAvailable, bufferSize;

    byte[] buffer;

    int maxBufferSize = 1 * 1024 * 1024;

    String urlString = "SERVER_URL+"/uploadpic2.php";

    try {
        System.out.println("exsistingFileName: " + exsistingFileName);
        FileInputStream fileInputStream = new FileInputStream(new File(
                exsistingFileName));
        URL url = new URL(urlString);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        /*conn.setReadTimeout(TIMEOUT_CONNECTION);
        conn.setConnectTimeout(TIMEOUT_SOCKET);*/
        // conn.setChunkedStreamingMode(1024);
        conn.setFixedLengthStreamingMode(fileInputStream.available() + 89
                + exsistingFileName.length());
        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);
        dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        System.out.println("exsistingFileName: " + exsistingFileName);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                + exsistingFileName + "\"" + lineEnd);
        dos.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);

        buffer = new byte[bufferSize];

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        try {
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);

                System.out.println("MAX BUFFER SIZE IS : " + maxBufferSize);
                System.out
                        .println("BYTES AVAILABLE IS : " + bytesAvailable);
                System.out.println("BUFFER SIZE IS : " + bufferSize);

                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
        } catch (Exception e) {

            System.out.println("OUT OF MEMORY ERROR");
        }

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) {
                Log.e("Error: ", "Server Response" + str);
            }
            inStream.close();

            // Toast.makeText(this, "File Uploaded Successfully",
            // Toast.LENGTH_LONG).show();

        } catch (IOException ioex) {
            Log.e("Error: ", "error: " + ioex.getMessage(), ioex);
        }
        try {
            fileInputStream.close();
            dos.flush();
            dos.close();
            dos = null;
        } catch (Exception e) {
            System.out.println("STREAM CLOSED ERROR");
        }

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

    catch (IOException ioe) {
        Log.e("Error:", "error: " + ioe.getMessage(), ioe);
    }

}

i have this code to upload fie to server but with this i m not able to upload a file with size more than 8MB;it gives the exception as FileNotFoundException that SERVER_URL+"/uploadpic2.php" not found. Please any body help me to fix this error. Thanks.

user1109610
  • 11
  • 1
  • 3
  • any body have idea why such exception is giving and how to get rid of it.plz suggest something to solve this problem – user1109610 May 31 '12 at 13:45

1 Answers1

0

According to this web page you need to change a setting in your php config file.

The limits are enforced there

post_max_size = 16M
upload_max_filesize = 16M

Also I think the line

String urlString = "SERVER_URL+"/uploadpic2.php";

should read

String urlString = SERVER_URL + "/uploadpic2.php";

UPDATE

I found another question similar to yours. This looks like an issue with phoneGap according to that question.

Community
  • 1
  • 1
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • but actually if i try to upload a 17MB file through filezilla the file gets uploaded but it won't get uploaded through my android app and gives the exception that server sides file not found. – user1109610 May 31 '12 at 11:19
  • FileZilla uses FTP. So it's understandable it works. Did you trying increase the max file sizes in the php configuration? – Ranhiru Jude Cooray May 31 '12 at 11:20
  • Okay i will try doing as u suggested. – user1109610 May 31 '12 at 11:24
  • hii..Ranhiru the max file size in php configurations is already set to 100MB..so what do i do now...? – user1109610 May 31 '12 at 11:38
  • Are both `post_max_size` and `upload_max_size` are set to 100MB? If yes, it's obvious that the error is somewhere else. I see another error too in the line `String urlString = "SERVER_URL+"/uploadpic2.php";` See my answer for more information. – Ranhiru Jude Cooray May 31 '12 at 11:42
  • If it works for all the file with less than 8MB of size, some configuration is blocking you. Please double check your PHP configuration files. – Ranhiru Jude Cooray May 31 '12 at 11:48
  • i think the connection is getting closed some where and thats why i m getting the filenotfound exception..but not getting how to get rid of this..plz help me its urgent..want to upload large files to the server... – user1109610 May 31 '12 at 12:11