从 PHP 传输到 Java 时数据不完整


Data incomplete while transferring from PHP to Java

在尝试将长字符串(Base64编码图像)从php发送到java时,只有一小部分数据被传递。当我在本地主机 apache 服务器上测试应用程序时,字符串被完全发送,应用程序正常工作。但是当我将代码迁移到外部服务器时,Java中只接收了一小部分数据。当我在浏览器中输出数据时,长字符串确实正确显示。

这是我用来发送数据的代码:

//get the data from mysql result
while($row = mysqli_fetch_assoc($result))
{
    // get the fotopath
    $fotoRow = mysqli_fetch_array ( $fotoResult, MYSQL_NUM );
    $foto = $fotoRow[0];
    //replace backslashes
    $fotoFormatted = $foto; //str_replace($backslash, $slash, $foto);
    //get the picture data
    $im = file_get_contents("http://www.mywebsite.com" . $fotoFormatted, true);
    //encode the picture data
    $imdata = base64_encode($im);
    //add the encoded string to the data array
    $row ['foto'] = $imdata;
    $output[] = $row;
}
//output the data as a json array
print(json_encode($output))

这是我用来接收数据的代码:

//get the url to the script to connect with
String link = "http://mywebsite/script.php";
//get the data to send to the script
String data = URLEncoder.encode("userid", "UTF-8") + "="
                + URLEncoder.encode(userid, "UTF-8");
//make a new HttpURLConnection
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
//make a new OutputStreamWriter with the HttpURLConnection
OutputStreamWriter wr = new OutputStreamWriter(
                   conn.getOutputStream());
//write the data and flush the remaining data
wr.write(data);
wr.flush();
//make a new BufferedReader with the given connection
BufferedReader reader = new BufferedReader(new InputStreamReader(
               conn.getInputStream()));
//define variables
JSONArray jsonArray = null;
String line = null;
//read the data from the server     
while ((line = reader.readLine()) != null) {
        jsonArray = new JSONArray(line);
        }
//disconnect the connection
conn.disconnect();
//return the data for further use
return jsonArray;

我的问题是:为什么来自外部服务器的数据不完整,而从本地服务器接收的数据是完整的,我能做些什么来解决这个问题。

我已经找到了解决方案,问题是某些文件的路径中包含空格(我认为设计非常糟糕),因此在 base64 中编码了 404 页面,并以这种方式添加了不正确的数据。 在我的本地服务器应用程序中,我没有配置 404 页面,因此它只会返回 null,并且我有一个回退来处理这种情况。我用 %20 替换了所有空格,错误已解决。