struts2 中使用request.getInputStream() 获取POST数据
来源:互联网 发布:淘宝嘉年华活动时间 编辑:程序博客网 时间:2024/06/07 02:37
转自 http://waterdh.iteye.com/blog/202608
最近有个项目下需要接收POST过来的XML数据包,在struts中的Action方法中,调用request.getInputStream() 发现无法读到任何数据。
以下是POST数据的程序代码:
- URL url = new URL(urlBuf);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url
- .openConnection();
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- httpURLConnection.setRequestMethod("POST"); httpURLConnection.setConnectTimeout(5000);
- httpURLConnection.setReadTimeout(5000);
- httpURLConnection.connect();
- BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
- httpURLConnection.getOutputStream(), "GBK"));
- out.write(word);
- out.flush();
这个是读取程序代码:
- BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
- String line = null;
- StringBuilder sb = new StringBuilder();
- while((line = br.readLine())!=null){
- sb.append(line);
- }
开始还以为是犯了什么低级错误,不断调试查找原因,无果。baidu上搜索了下,众说纷纭,也没有很好的解决方案。我也左思右想,文件上传也是在Action立面处理的,组件肯定用到inpputStream一点问题没有,没可能这里不行,后来灵光一闪,在POST程序中加入:
- httpURLConnection.setRequestProperty("content-type", "text/html");
根据以上现象,我们不难看出struts对没有指定content-type的request请求,封装时候作了一些处理,导致无法在Action中获取request.getInputStream() 和 request.getReader()。
0 0
- struts2 中使用request.getInputStream() 获取POST数据
- struts2 中使用request.getInputStream() 获取POST数据
- struts2 中使用request.getInputStream() 获取POST数据
- struts2 中使用request.getInputStream() 获取POST数据
- struts2 中使用request.getInputStream() 获取POST数据
- struts2 中使用request.getInputStream() 获取POST数据
- 使用request.getInputStream() 获取POST数据
- 使用request.getInputStream() 获取POST数据
- 使用request.getInputStream() 获取POST数据
- 使用request.getInputStream() 获取POST数据
- Shiro导致request.getInputStream()无法获取数据
- webservice使用XFire中request.getInputStream不能多次读取
- request.getInputStream不能使用两次
- request.getParameter() 、 request.getInputStream()使用冲突
- request.getInputStream()只能获取一次的问题
- request.getInputStream()只能获取一次的问题
- request.getInputStream()只能获取一次的问题
- Struts2中获取Session,Request
- Error:Execution failed for task ':transformClassesWithJarMergingForDebug'
- 字典树查找字符串(java版)
- 怎么去掉Xcode工程中的某种类型的警告
- UVA10881----Piotr's Ants
- tag 滚动变换
- struts2 中使用request.getInputStream() 获取POST数据
- Windows下git安装及使用技巧
- match()方法及exec()方法
- 正则表达式(括号)、[中括号]、{大括号}的区别小结
- sp<>强指针类的用法
- C# 利用 OpenCV 进行视频捕获 (笔记)
- 管理产品文档prd的终极解决方案,再也不用头疼prd的更新和查看了!
- CPU占用率分析
- hdu 5154 Harry and Magical Computer(拓扑排序)