客户端是用java post一个multipart/form-data请求,包含json参数数组,和一个上传的文件。
请问在php服务器端如何解析/获取json数据?
回复讨论(解决方案) 下面是java客户端代码
public void dopost() {
log.d(tag, ======dohttpconnectionpost );
httpurlconnection conn = null;
dataoutputstream dos = null;
inputstream in = null;
try {
stringbuilder paramstr = new stringbuilder();
iterator i = mparam.keys();
while(i.hasnext()) {
string key = i.next();
log.d(tag, key : + key + , value : + mparam.getstring(key) );
if (!(key_logfile.equals(key))) {
if(paramstr.length()>0) {
paramstr.append(&);
}
paramstr.append(key);
paramstr.append(=);
try {
paramstr.append(urlencoder.encode(string.valueof(mparam.getstring(key)),utf-8));
} catch (unsupportedencodingexception e) {
paramstr.append(mparam.getstring(key));
}
}
}
if(murl.charat(murl.length()-1) != '?') {
murl = murl + ?;
}
murl = murl+paramstr;
log.d(tag, ====murl: + murl);
url url = new url(murl);
conn = (httpurlconnection) url.openconnection();
conn.setreadtimeout(http_param_timeout);
conn.setconnecttimeout(http_param_timeout);
conn.setdoinput(true);
conn.setdooutput(true);
conn.setrequestmethod(post);
conn.setrequestproperty(charset, charset);
conn.setrequestproperty(connection, keep-alive);
conn.setrequestproperty(content-type, multipart/form-data + ;boundary= + boundary);
dos = new dataoutputstream(conn.getoutputstream());
log.d(tag, ====after open conn.getoutputstream);
i = mparam.keys();
map files = new hashmap();
while(i.hasnext()) {
string key = i.next();
if ((key_logfile.equals(key))) {
log.d(tag, key : + key + , value : + mparam.getstring(key) );
if (!textutils.isempty(mparam.getstring(key))) {
files.put(key, new file(mparam.getstring(key)));
}
}
}
for (map.entry file : files.entryset()) {
if (file.getvalue() != null && file.getvalue().exists()) {
stringbuilder sb = new stringbuilder();
sb.append(prefix);
sb.append(boundary);
sb.append(linend);
sb.append(content-disposition: form-data; name=\ + file.getkey()
+ \; filename=\+file.getvalue().getname()+\+linend);
sb.append(content-type: application/octet-stream; charset=+charset+linend);
sb.append(content-transfer-encoding: 8bit + linend);
sb.append(linend);
log.d(tag, sb.tostring());
dos.write(sb.tostring().getbytes());
inputstream is = new fileinputstream(file.getvalue());
log.d(tag, filevalue=+file.getvalue());
byte[] bytes = new byte[1024];
int len = 0;
while((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
log.d(tag, in read file+len);
}
is.close();
dos.write(linend.getbytes());
}
}
byte[] end_data = (prefix + boundary + prefix + linend).getbytes();
dos.write(end_data);
dos.flush();
int res = conn.getresponsecode();
log.d(tag, res = + res);
if(res==200){
in = conn.getinputstream();
int ch;
stringbuilder sb2 = new stringbuilder();
while ((ch = in.read()) != -1)
{
sb2.append((char) ch);
}
log.d(tag, response entity string: + sb2);
mresponse = new jsonobject(sb2.tostring());
}else{
log.d(tag, conn.getresponsecode()+conn.getresponsemessage());
in = conn.geterrorstream();
}
} catch (ioexception e) {
e.printstacktrace();
} catch (jsonexception e) {
e.printstacktrace();
} finally {
try {
if (in != null) {
in.close();
}
if (dos != null) {
dos.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (ioexception e) {
e.printstacktrace();
}
}
}
file_put_contents('test.txt', print_r($_post, 1));
看看 test.txt 中有什么
json数据用_post获取不到吧
一切结论都在调查研究之后!
file_put_contents('test.txt', print_r($_post, 1));
输出到test.txt文件里是空的。
有高手帮忙看看这个问题?
不行?你就
file_put_contents('test.txt', file_get_contents('php://input'));
看看 test.txt 中有什么
你客户端是 java 的,谁没事会摹写一个替你做测试?
你说是 post 方式的,但 $_post 无值。显然是你的客户端的问题!
上传文件的同时传几个数据不是很正常的应用吗?
这个multipart/form-data有关系。
不能用$_post取到json数据。
这个不是客户端的问题,老大!同样的java代码其他的非php服务器是好使的。
你做过这种方式的上传吗?
这个问题对有点php经验的同学应该很简单吧,请帮助哈。
现在上传的文件是可以正常获取到,就是不知道如何获取json数据。
我让你
file_put_contents('test.txt', file_get_contents('php://input'));
看看 test.txt 中有什么
你做了吗?
老大,做了,里面没东西,我不是回复了吗
'php://input'获取不到multipart/form-data方式post的数据,你不知道吗?
你只回复说 file_put_contents('test.txt', print_r($_post, 1)); 没有内容
并没有说 file_put_contents('test.txt', file_get_contents('php://input')); 没有内容
php://input 获取的是没有被 php 解析过的(或是说是 php 解析后剩余的输入流)
如果 php://input 也没有内容,那就一定是被 php 解析了
你可以在观察 $_files 是否有值,很可能你的数据被当做上传文件给处理了
不要总以为 json 有什么特殊性,对于网络传输而言,也就只不过是一个字符串而已。只要你发送了,就一定在数据流中
multipart/form-data 是 application/x-www-form-urlencoded 的特例
你也可以视为是 application/x-www-form-urlencoded 的扩展
凡是要用 http 协议上传文件,就一定要有 multipart/form-data 声明,并以他约定的方式组织数据
至于我是否做过上传文件,那么你可以在精华区找到前几年我参与的有关各种上传文件的方法的讨论
只是由于完全按照规范来写,的确没有失败的经验
file_put_contents('test.txt', file_get_contents('php://input'));
文件里也没有内容 ;-(
file_put_contents('test.txt', print_r($_files, 1));
总该有内容了吧?
file_put_contents('test.txt', print_r($_files, 1));
打印出来是这个:
array
(
[logfile] => array
(
[name] => log.zip
[type] => application/octet-stream
[tmp_name] => /var/www/php7ax336
[error] => 0
[size] => 28571
)
)
嗯,这还是附加的数据没有命名的原因
php在解析post方式 数据时,会忽略 name= 这一节的数据
为此用 curl 做了一下测试 $file = realpath('0.txt');$fields = array( 'f' => '@'.$file, '' => 'abcd', //没有名字就收不到,反之就可以收到);$ch = curl_init(); curl_setopt($ch, curlopt_url,http://localhost/ajax_server.php?id=1); curl_setopt($ch, curlopt_post, 1 );curl_setopt($ch, curlopt_postfields, $fields);curl_exec ($ch); curl_close ($ch);
但是数据是完整的传到了 --------------------------6e77d33195b440f3content-disposition: form-data; name=f; filename=d:\\amp\\web\\0.txtcontent-type: application/octet-stream这是一个测试--------------------------6e77d33195b440f3content-disposition: form-data; name=abcd--------------------------6e77d33195b440f3--
所以最好是与客户端的开发人员协商,给每个数据项都起个名字
当然你也可以用 php 来解决这个问题,那就是在 php.ini 中加上
enable_post_data_reading = off
这就阻止了 php 解析 post 方式数据的动作
输入流就可以被 php://input 取到了
然后你再对读到的数据分类保存,你肯定比 php 勤快,不可能忽略掉没有名字的数据项了
注意:由于你阻止了 php 对 post 数据的解析额,所以 $_post、$_files 都无效了!应用时切切注意
所以最好是与客户端的开发人员协商,给每个数据项都起个名字。
这个每个数据项都起个名字是啥意思?我post的数据:
http://api.hdtv.com/save?modelname=%e8%ae%be%e7%bd%ae&type=s60&contact=777720&description=&version=v320r470c096b07166t&versionname=3.0.096t&model=s60&ui=3.0&hwversion=h5001&mac=be60ebe15a7c
解决办法只能是修改php.ini吗?
我#17下部第7行
content-disposition: form-data; name=
name 为空串,就是没有名字
找个人不也要喊名字吗?光喂喂喂的谁踩你?
你给的那个连接是什么意思?什么东西都不显示
大侠 您好,
我的name是有名字的啊?name=log.zip
那个链接是post数据的url,都有名字。
这个问题怎么解决,请问大侠?
你不是说:包含json参数数组,和一个上传的文件 吗?
打印 $_files 有
[logfile] => array
(
[name] => log.zip
这个是有名字的,表示文件上传正确
打印 $_post 是空的(#5)
表示那个 json 数据没有被接收到
你那个连接,一是没有任何显示,不知是否正确。而是看源码(不是很仔细),也没看到有上传或提交
再说那是 js 操作的页面,与 java 有什么关系?
我的代码是手机上android的java上传,
你说js是什么意思?
有什么办法吗?大侠
这段java代码和其他服务器配合是没有问题的,
我现在是想在服务器端用php接收而已。
求大侠,高手帮助啊
这个问题找到原因了
http://api.hdtv.com/save?modelname=%e8%ae%be%e7%bd%ae&type=s60&contact=777720&description=&version=v320r470c096b07166t&versionname=3.0.096t&model=s60&ui=3.0&hwversion=h5001&mac=be60ebe15a7c
这个url里的参数是得用_get去获取的,