近有个项目下需要接收POST过来的XML数据包,在struts中的Action方法中,调用request.getInputStream() 发现无法读到任何数据。 以下是POST数据的程序代码: Java代码
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();
view plaincopy to clipboardprint?
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();
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();这个是读取程序代码: Java代码
BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
view plaincopy to clipboardprint?
BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
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程序中加入: Java代码
httpURLConnection.setRequestProperty("content-type", "text/html");
view plaincopy to clipboardprint?
httpURLConnection.setRequestProperty("content-type", "text/html");
httpURLConnection.setRequestProperty("content-type", "text/html");,测试成功! 根据以上现象,我们不难看出struts对没有指定content-type的request请求,封装时候作了一些处理,导致无法在Action中获取request.getInputStream() 和 request.getReader()。
http://www.bitscn.com/pdb/java/200806/143983.html
分享到:
相关推荐
下面小编就为大家带来一篇浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
下面小编就为大家带来一篇浅谈request.getinputstream只能读取一次的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在我们平时开发中,特别是需要与接口打交道时,无论是写接口还是用接口,拿到接口后肯定都得提前测试一下,这样的话就非常需要有一个比较给力的Http请求模拟工具,现在流行的这种工具也挺多的,像火狐浏览器插件-...
request.getInputStream()方法................................................8 request.getReader()方法 .......................................................8 request.getParameter(String name)方法,...
NULL 博文链接:https://superherosk123.iteye.com/blog/611933
提供了ServletRequest过滤程序,重新构造对象内容,并有效规避request.getParameter()、request.getInputStream()冲突的问题,同时提供了对跨站脚本攻击XSS和SQL注入的过滤程序。
很好的xwork的资源包public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DiskFileItemFactory factory = new DiskFileItemFactory(); ...
1.获取输入的URL地址,判断是否为空2.建立子线程,获取URl对象newURL(path)3.打开连接获取HttpURLConnectionconn=(HttpURLConnection)url.openConnection...获取conn.getInputStream(),9.使用BitmapFactory.decode(in),将
BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream())); License license = new License(); license.setLicense(br); } catch (Exception var3) { var3.printStackTrace...
ServletInputStream input_Stream = request.getInputStream(); DataInputStream d_Inputstream = new DataInputStream(input_Stream ); String data_MIDlet = d_Inputstream.readUTF(); d_Inputstream.close();...
游戏开发中客户端与服务器端数据的传输, URL url = new URL(path); //创建一个Url对象 //得到打开的链接对象 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //设置请求超时与请求...
利用spring resource读取配置文件。
示例:System.out.println(SimpleString.getString(SimpleHttpGet.getInputStream("http://www.baidu.com", null)));
Java调用Linux命令 调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例, (注意:Runtime.getRuntime().exec(command)返回的是一...用户需要用这些stream来向子进程输入数据或获取子进程的输出
b=(Button)this.findViewById(R.id.b);... InputStream is = conn.getInputStream();//取得返回的InputStream bitmap = BitmapFactory.decodeStream(is);//将InputStream变成Bitmap is.close();//关闭InputStream
@RequestMapping(value="/user/import",method = RequestMethod.POST) public Result importUser(@RequestParam(name="file") MultipartFile file) throws Exception { //1.解析Excel //1.1.根据Excel文件创建...
读取返回的输入流中的数据,并将其中的数据转换为byte数组 使用InputStream 的read方法以及ByteArrayOutputStream的wirte方法 inputStream.read(buffer) outputStream.write(buffer, 0, len) outputStream....
示例代码: ... in = connection.getInputStream(); byte[] response = readFully(in); return new String(response, "UTF-8"); } finally { if (in != null) in.close(); } } 标签:okhttp