从Android设备发送ZIP文件到php服务器

时间:2014-09-19 20:32:05

标签: php android post

我正在尝试上传在Android设备上生成的zip文件。我知道由于屏幕之间的延迟,文件正在发送。我也可以循环$_FILES并查看文件名,但是当我尝试move_uploaded_file时,它会失败。

        FileInputStream fileInputStream = new FileInputStream(mfile);
        URL url = new URL("http://xxxxxxxxxx/test/test.php");
        // 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("imagezip", mfile.toString());

        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"imagezip\";filename=\"" + mfile.toString() + "\"" + 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 necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

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

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

        if(serverResponseCode == 200){


        }

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

当它到达php服务器时,我能够看到该文件但无法保存它

$fileLog = "test.txt";
$strLog = "==========================\n";
$strLog .= date("Y-m-d-H-i-s") . "\n";
$strLog .= "==========================\n";

$file_path = "images/";
$file_name = basename( $_FILES['imagezip']['name']);
$file_name = substr($file_name, 0, -4) . "_" . time() . ".zip";
$file_path = $file_path . $file_name;

foreach($_FILES as $file){
    $strLog .= $file['name'] . " --------- \n"; 
}


if(move_uploaded_file($_FILES['imagezip']['tmp_name'], $file_path)) {
    $strLog .= "success\n";
} else{
    $strLog .= "fail\n";
}
$strLog .= "---------------------------\n";
file_put_contents($file_path.$fileLog, $strLog, FILE_APPEND | LOCK_EX);

0 个答案:

没有答案