我花了最后一天左右的时间试图调试这个问题,但我没有想法。基本上我有一个 Android 应用程序将一些数据发布到 PHP/Apache Web 服务器。当我将它指向我的本地测试服务器时,这段代码似乎工作正常。当我将它指向我的生产服务器时,它似乎也可以正常工作,但只有当我注释掉该行时conn.setChunkedStreamingMode(maxBufferSize);
。启用该行后,该帖子仅适用于我的本地测试服务器,但在发布到生产服务器时,PHP $_FILES 数组为空。我尝试将许多值传递给setChunkedStreamingMode
(包括 0 和 1024),但这些似乎都不能解决问题。
在这一点上,我假设问题与配置生产服务器的 PHP 的方式有关,但据我所知,服务器上的所有重要参数都与我的测试实例上的相同。此外,它们都运行相同版本的 Apache 和 PHP。我的生产服务器由 Bluehost 运行。
这是我用来上传的 Java 代码:
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "***************************************************";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 212144; // 1024*1024 = 1MB. 212144 is a quarter MB.
FileInputStream fileInputStream = null;
try
{
// ------------------ CLIENT REQUEST
fileInputStream = new FileInputStream(new File(existingFileWithFullPath));
// open a URL connection to the Servlet
URL url = new URL(BACKUP_POST_URL);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Send in chunks (to avoid out of memory error)
conn.setChunkedStreamingMode(maxBufferSize);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="
+ boundary);
conn.setReadTimeout(200000); // 200 seconds...
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";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)
{
try {
dos.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError oome) {
Log.e(CommonStatic.LOG_NAME, "Out of memory error caught...");
oome.printStackTrace();
fileInputStream.close();
throw new Exception("Out Of Memory!");
}
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);
fileInputStream.close();
dos.flush();
dos.close();
// close streams
Log.d(CommonStatic.LOG_NAME, "Backup file written to server successfully...");
}
catch (Exception ex)
{
Log.e(CommonStatic.LOG_NAME, "Backup File Upload Error: " + ex.getMessage(), ex);
throw new Exception (c.getString(R.string.SAVE_TO_CLOUD_ERROR));
}
这是我在另一端用来接收的 PHP 代码:
$target = "userfiles/";
$target = $target . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target))
{
echo "SUCCESS";
}
else
{
echo "FAIL";
}
我print_r($_FILES);
在脚本的最开始添加了 a 以确定 $_FILES 在生产实例上为空,但在测试实例上不是。任何想法将不胜感激。