0

私人无效doFileUpload(字符串现有文件名){

    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);
    }

}

我有这段代码可以将 fie 上传到服务器,但是我无法上传大小超过 8MB 的文件;它给出了 FileNotFoundException 异常,即找不到 SERVER_URL+"/uploadpic2.php"。请任何机构帮助我解决此错误。谢谢。

4

1 回答 1

0

根据此网页,您需要更改 php 配置文件中的设置。

限制在那里执行

post_max_size = 16M
upload_max_filesize = 16M

我也认为这条线

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

应该读

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

更新

我发现了另一个与你类似的问题。根据那个问题,这看起来像是 phoneGap 的问题。

于 2012-05-31T11:14:37.807 回答