我花了最后一天左右试图调试这个问题而且我没有想法.基本上我有一个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));
}
Run Code Online (Sandbox Code Playgroud)
这是我在另一端使用的PHP代码:
$target = "userfiles/";
$target = $target . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target))
{
echo "SUCCESS";
}
else
{
echo "FAIL";
}
Run Code Online (Sandbox Code Playgroud)
我print_r($_FILES);在脚本的最开头部分确定$ _FILES在生产实例上是空的,但在测试实例上没有.任何想法将不胜感激.
不幸的是,我无法找到此问题的直接解决方案,但我找到了解决方法。conn.setChunkedStreamingMode我没有使用而是使用了conn.setFixedLengthStreamingMode,我之前尝试过但没有成功。开始工作的关键conn.setFixedLengthStreamingMode是将您尝试发送的数据(在本例中为文件)的完整长度以及标头的长度传递给它。幸运的是,标头长度通常在这样的代码中是固定的,因此一旦您计算出标头有多大(请记住文件名之类的内容,它在 Content-Disposition 下的标头中发送也是可变的),您就可以将其放入作为固定数字。为了计算出标头长度,我首先运行代码,但没有指定标头的任何长度。我收到的错误消息为我提供了发送字节数的预期值和实际值,这使我能够计算标头长度。