0

プライベートボイドdoFileUpload(文字列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);
    }

}

ファイルをサーバーにアップロードするためのこのコードがありますが、これではサイズが 8MB を超えるファイルをアップロードできません。このエラーを修正するために私を助けてください。ありがとう。

4

1 に答える 1