Q1:curl命令行中返回结果有中文,在windows cmd中出现乱码。
本以为是本身utf-8编码没有设置,但是设置之后发现问题依然存在,于是乎转向怀疑windows cmd本身的问题,查阅资料得知windows环境下cmd默认编码是GBK,若要改成utf-8显示需要手动设置,设置方法见下:http://jingyan.baidu.com/article/e75aca85440f01142edac636.html
Q2:curl -d 和curl -f不可以同时使用,那么很多朋友就会问表单post提交的数据既有用户数据,又有上传的文件,那该怎么办?首先不能同时使用的原因是:(来源于curl官网帮助http://curl.haxx.se/docs/manpage.html)
-d, --data <data>
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using thecontent-type application/x-www-form-urlencoded. Compare to -F, --form.
-F, --form <name=content>
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-dataaccording to RFC 2388.
因为两种编码格式不同,注定不能在curl的一条命令行里共存。在这里同时传递两种数据我们统一使用-f。
curl -F uploadFiles=@D:\ibotest\test.ppt -F "DATA={\"sender\":\"aaaa\",\"receiver\":\"bbbb\"}" http://localhost:8080/WebTest/webtest因为两种编码格式不同,注定不能在curl的一条命令行里共存
在服务器端接收的时候java后台代码如下(这里使用了SmartUpload组件)
SmartUpload su=new SmartUpload();
su.initialize(getServletConfig(), request, response);
com.jspsmart.upload.Request req = su.getRequest();
su.upload();
revdata=req.getParameter("DATA");
System.out.println(revdata);