使用Android(Java)将文件上载到php脚本。文件大小为0,但没有错误。服务器上仍有可用的房间

使用Android(Java)将文件上载到php脚本。文件大小为0,但没有错误。服务器上仍有可用的房间,java,php,android,html,pdf,Java,Php,Android,Html,Pdf,通过阅读以下内容将代码组合在一起后: 上传看起来正常,并将返回200到android设备。而且PHP文件没有错误。但是,虽然它具有正确的名称和其他信息,但文件大小为0 Android (Java) Code Private String httpPost(Uri fileUri) { String url = "http://MyUrl/testPhone.php"; String result; File file = new File(fileUri.getPath

通过阅读以下内容将代码组合在一起后:

上传看起来正常,并将返回200到android设备。而且PHP文件没有错误。但是,虽然它具有正确的名称和其他信息,但文件大小为0

Android (Java) Code Private String httpPost(Uri fileUri) {
     String url = "http://MyUrl/testPhone.php";
    String result;
    File file = new File(fileUri.getPath());
    try {

        String charset = "UTF-8";
        String param = "value";
        File binaryFile = new File(fileUri.getPath());
        String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
        String CRLF = "\r\n"; // Line separator required by multipart/form-data.

        URLConnection connection = new URL(url).openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        try (
                OutputStream output = connection.getOutputStream();
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
        ) {
            // Send normal param.
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
            writer.append(CRLF).append(param).append(CRLF).flush();



          //  output.flush(); // Important before continuing with writer!
          //  writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

            // Send binary file.
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" +binaryFile.getName() + "\"").append(CRLF);
            writer.append("Content-Type: application/pdf").append(CRLF);
            writer.append("Content-Transfer-Encoding: binary").append(CRLF);
            writer.append(CRLF).flush();

            output.flush(); // Important before continuing with writer!
            writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

            // End of multipart/form-data.
            writer.append("--" + boundary + "--").append(CRLF).flush();
        }

        int responseCode = ((HttpURLConnection) connection).getResponseCode();

        System.out.println(responseCode); // Should be 200
        result = "http://MyUrl/testPhone.php?uuid=" + responseCode+ "Test"+binaryFile.length();

    }
     catch (Exception e) {
        result = "http://MyUrl/testPhone.php" + "?uuid=" + e;
     }
    return result;
}
PHP代码:

echo $_GET["uuid"];

$file = $_FILES["binaryFile"]["tmp_name"];


if (file_exists($file))
{

foreach($_FILES['binaryFile'] as $key => $val){
    error_log(date('j/n/o H:i:s')." ". $key ." = ".$val);
}
error_log (disk_free_space("/"));

   // $result .= $thisEventDB->execSql('insert into Files values (0, ?, ?,    NOW(), ?, ?, ?, 0)', array(6,$file, $fileData, $fileData, md5_file($file)));


}
错误日志:

[Wed Aug 26 14:41:13 2015][error]26/8/2015 14:41:13 name=>Getting Started.pdf
[2015年8月26日星期三14:41:13][error]26/8/2015 14:41:13 type=>application/pdf
[Wed Aug 26 14:41:13 2015][error]26/8/2015 14:41:13 tmp_name=>/usr/local/var/php/php7P1M4j
[2015年8月26日星期三14:41:13][error]26/8/2015 14:41:13 error=>0
[2015年8月26日星期三14:41:13][error]26/8/2015 14:41:13尺码=>0
[2015年8月26日星期三14:41:13][错误]7182110720

Length在android代码中返回131990,因此似乎具有正确的文件路径


我不确定从这里走到哪里。

我看不到您将文件写入请求流的位置

writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();

output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush();
您如何编写实际的文件内容

此处提出的其中一种方法可能适用于您:


是的,我觉得我错过了那一部分。。。我只是不知道它应该去哪里。我会读出来/测试一下,然后再给你回复。干杯@ID10T我不确定,因为我从未以如此低的级别提交过多部分表单,但我的直觉是您需要在两个
writer.append(CRLF)
调用之间将文件内容写入
output
。我希望输出流能从那里正确地处理一切。@ID10T您有更高级别的选项吗?我更熟悉这种类型的结构:即创建“部分”并将它们“附加”到一个请求中,该请求可以为您处理所有边界和编写内容。我不是100%确定您的意思,但我相信我有更高级别的选项可用。我在kitkat 4.4.2上,我无法导入此处提到的类“MultipartEntityBuilder”,它似乎在androidStudio中找不到