gpt4 book ai didi

Java/Android HttpURLConnection setChunkedStreamingMode 不适用于所有 PHP 服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:38 24 4
gpt4 key购买 nike

我花了大约一天的时间来调试这个问题,但我没有想法。基本上我有一个 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); 以确定 $_FILES 在生产实例中为空,但在测试实例中不为空。任何想法将不胜感激。

最佳答案

不幸的是,我无法找到解决此问题的直接方法,但我找到了解决方法。我没有使用 conn.setChunkedStreamingMode,而是使用了 conn.setFixedLengthStreamingMode,我之前尝试过但没有成功。让 conn.setFixedLengthStreamingMode 工作的关键是将您尝试发送的数据的完整长度(在本例中为文件)加上 header 的长度传递给它。幸运的是标题长度通常在这样的代码中是固定的,所以一旦你弄清楚你的标题有多大(记住像文件名这样的东西,它在 Content-Disposition 下的标题中发送也是可变的)你可以把它作为一个固定的数字。为了计算出我的 header 长度,我首先运行了代码,但没有为 header 指定任何长度。我收到的错误消息为我提供了发送字节数的预期值和实际值,这使我能够计算 header 长度。

关于Java/Android HttpURLConnection setChunkedStreamingMode 不适用于所有 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12249885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com