【问题标题】:Uploading file via HttpURLConnection do not do it completely通过 HttpURLConnection 上传文件不完全这样做
【发布时间】:2021-09-11 14:55:26
【问题描述】:

我有一个密码。 前端:

public int uploadFile(String sourceFileUri) {

    String fileName = sourceFileUri;
    HttpURLConnection conn;
    DataOutputStream dos;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {

        dialog.dismiss();
        Log.e("uploadFile", "Source File not exist :"
                +uploadFilePath + "" + uploadFileName);

        runOnUiThread(new Runnable() {
            public void run() {
                messageText.setText("Source File not exist :"
                        +uploadFilePath + "" + uploadFileName);
            }
        });

        return 0;
    }
    else
    {
        try {
            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(upLoadServerUri);
            // 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("uploaded_file", fileName);

            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;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);

            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            // send multipart form data necessary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

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

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

            if(serverResponseCode == 200){

                runOnUiThread(new Runnable() {
                    public void run() {

                        String msg = "File Upload Completed.\n\n "
                                +uploadFileName;

                        messageText.setText(msg);
                        Toast.makeText(UploadToServer.this, "File Upload Complete.",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }

            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            dialog.dismiss();
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("MalformedURLException Exception : check script url.");
                    Toast.makeText(UploadToServer.this, "MalformedURLException",
                            Toast.LENGTH_SHORT).show();
                }
            });

            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {

            dialog.dismiss();
            e.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("Got Exception : see logcat ");
                    Toast.makeText(UploadToServer.this, "Got Exception : see logcat ",
                            Toast.LENGTH_SHORT).show();
                }
            });
            Log.e("Upload file Exception", "Exception : "
                    + e.getMessage(), e);
        }
        dialog.dismiss();
        return serverResponseCode;

    } // End else block
}

后端:

$file_path = "uploads/files/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path))
{
echo "success";
}else{
echo "fail";
}

此代码有效,但是:

  1. 我无法上传小文件(10-1000 字节),服务器响应没有错误,但我没有看到任何小文件
  2. 大文件更改,例如我向服务器发送 3925 字节大小的文件,但服务器仅保存 1002 字节(文件末尾)。 26216 字节 -> 26173 等。 你能帮我解释一下这种奇怪错误的原因吗,或者你知道如何解决它? 谢谢。

更新: 看来我已经解决了问题。有一个新代码,工作正常。关键解决方案 - 使用System.lineSeparator() 而不是String lineEnd = "\r\n";

public int uploadFile(String sourceFileUri) {

    String fileName = sourceFileUri;
    HttpURLConnection conn;
    DataOutputStream dos;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    //String boundary = "*****file_Antares*****";
    String boundary = "WebKitFormBoundary7$G67whGfe341h6@fr561";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 8*1024;

    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {

        dialog.dismiss();
        Log.e("uploadFile", "Source File not exist :"
                +uploadFilePath + "" + uploadFileName);

        runOnUiThread(new Runnable() {
            public void run() {
                messageText.setText("Source File not exist :"
                        +uploadFilePath + "" + uploadFileName);
            }
        });

        return 0;
    }
    else
    {
        try {
            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(upLoadServerUri);
            // Open a HTTP  connection to  the URL
            conn = (HttpsURLConnection) 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("uploaded_file", fileName);
            conn.connect(); //new!!!
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + System.lineSeparator());// add new System.lineSeparator()) 
            dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;filename=" + fileName + System.lineSeparator());
            dos.writeBytes(System.lineSeparator());

            // create a buffer of  maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            
            while ((bytesRead = fileInputStream.read(buffer)) >= 0) {
                dos.write(buffer, 0, bytesRead);
            }

            // send multipart form data necessary after file data...
            dos.writeBytes(System.lineSeparator());
            dos.writeBytes(twoHyphens + boundary + twoHyphens + System.lineSeparator());
            //close the streams //
            dos.close();
            dos.flush();
            fileInputStream.close();
            
            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

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

            if(serverResponseCode == 200){

                runOnUiThread(new Runnable() {
                    public void run() {

                        String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                +uploadFileName;
                        messageText.setText(msg);
                        Toast.makeText(UploadToServer.this, "File Upload Complete.",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }

        } catch (MalformedURLException ex) {

            dialog.dismiss();
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("MalformedURLException Exception : check script url.");
                    Toast.makeText(UploadToServer.this, "MalformedURLException",
                            Toast.LENGTH_SHORT).show();
                }
            });

            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {

            dialog.dismiss();
            e.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("Got Exception : see logcat ");
                    Toast.makeText(UploadToServer.this, "Got Exception : see logcat ",
                            Toast.LENGTH_SHORT).show();
                }
            });
            Log.e("Upload file Exception", "Exception : "
                    + e.getMessage(), e);
        }
        dialog.dismiss();
        return serverResponseCode;

    } // End else block
}

【问题讨论】:

  • 感谢您的回复。但是随着 while (bytesRead >= 0) 这段代码循环并且永远不会停止。
  • 我已经更新了您的问题以使用 android,而不是 android studio,因为您并没有真正询问 IDE。使用正确的标签很重要,否则人们不会看到您的问题。一般来说,使用android,如果您特别询问IDE,请使用android-studio
  • 您知道如果边界***** 包含在您的文件数据中,那么此代码将失败。
  • 是的,我知道,即使在没有 ***** 的 TXT 文件上我也有问题

标签: java android httpurlconnection


【解决方案1】:

你打电话给conn.getResponseCode();太早了。

这样做会结束请求,但您刷新和关闭 dos 的代码将在稍后执行:

dos.flush(); // can be omitted as close is called directly afterwards
dos.close();

将此代码块移到conn.getResponseCode();之前。

此外,您的 while 循环是有缺陷的,因为您总是将 buferSize 而不是 bytesRead 字节写入 dos。此外,关于缓冲区大小的整个计算也是完全没有必要的。只需使用固定大小的缓冲区,这将始终有效。

        buffer = new byte[8192];

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

或更短:

        buffer = new byte[8192];
        while ((bytesRead = fileInputStream.read(buffer)) >= 0) {
            dos.write(buffer, 0, bytesRead);
        }
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        dos.close();

【讨论】:

  • 感谢您帮助我,非常感谢。我更改了代码,但没有结果。 :( 我已经发送了 3925 字节大小的文件,并且仍然在服务器上获得 1002 字节的文件。我什至在 'bytesRead = fileInputStream.read(buffer);' 的调试器中看到正确的缓冲区大小,但我仍然有同样的问题,我不知道为什么。
  • @AlexDem 可能是您的服务器代码也有缺陷?
  • 可能不是,我有这个 PHP 服务器,它带有从 HTML + JavaScript 上传文件的代码,它工作正常。
  • @AlexDem 如果您可以通过 HTTP 访问您的服务器,请这样做并通过 Wireshark/tshark 记录上传以查看实际上传的内容。将其与不同客户端的上传(例如通过网站)进行比较。这是确定问题出在哪里(客户端或服务器端)的最佳方法。
  • 我发现了另一个问题。请使用固定的缓冲区大小,例如 8K = 8192。available() 方法在 Java 中是可选的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2012-04-14
  • 1970-01-01
  • 2017-06-25
相关资源
最近更新 更多