doutao4938 2015-01-21 10:11
浏览 50
已采纳

从PHP转移到Java时数据不完整

While trying to send a long string (a Base64 encoded image) from php to java only a fraction of the data is passed over. When I tested the application on my localhost apache server the string was sent over completely and the application worked properly. But when I migrated the code to an external server, only a fraction of the data was received in Java. When I output the data in the browser the long string does display properly.

This is the code I have used to send the data:

//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))

This is the code I have used to receive the data:

//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;

My question is: Why is the data from the external server incomplete while the data received from the local server is complete and what can I do to solve this problem.

  • 写回答

1条回答 默认 最新

  • dongliao3450 2015-01-21 14:08
    关注

    I have found the solution, the problem was that spaces were included in the path of some files(pretty bad design i think), because of this a 404 page was being encoded in base64 and in this way the incorrect data was added. in my local server application i had not configured a 404 page so it would just return null and i had a fallback to handle that situation. I replaced all spaces with %20 and the errors were resolved.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况