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 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录