shell脚本中怎么调用python 调用adb shell脚本中的带参函数

Http使用post方式提交数据(使用java标准接口) - 推酷
Http使用post方式提交数据(使用java标准接口)
本文内容:使用java标准接口,实现http用post方式提交数据。
-------------------------------------------------------------------------------------------------------------
程序组成部分:
1.客户端用eclipse HttpUtils.java 标准java接口,实现http用post方式提交数据。 (用post方式提交 username 和 password)
2.服务器端用myeclipse+tomcat 对客户端请求进行相应。(若用户名密码正确,则返回字符串&login is success !& ,不正确则返回字符串“login is fail !”)
重点注意点:
1. public static String sendPostMessage(Map&String,String& params , String encode)
&&& 目的: 在客户端向服务器端发送 数据 params , 最终获取从服务器返回的输入流,最终将该输入流转换成字符串。注意使用标准java接口如何实现http的post请求,成功与服务器连接,并且获得从服务器端响应返回的数据。
2. public String String changInputStream(InputStream inputStream , String encode)
&&& 目的: 将一个输入流按照指定编码方式转变成一个字符串。(本例中是指,将从服务器端返回的输入流InputStream转变成一个字符串String,编码方式是encode方式)
3. Map&String ,String& 的实例化方法及迭代方法
& Map& 的实例化方法:
& Map&String, String& params = new HashMap&String, String&();
&&params.put(&username&, &admin&);
&&params.put(&password&, &123&);
&& Map 的迭代方法:
&StringBuffer stringBuffer = new StringBuffer();
&&&for (Map.Entry&String, String& entry : params.entrySet()) {
&&&&&stringBuffer
&&&&&&&.append(entry.getKey())
&&&&&&&.append(&=&)
&&&&&&&.append(URLEncoder.encode(entry.getValue(), encode))
&&&&&&&.append(&&&);
&&&&} catch (UnsupportedEncodingException e) {
&&&&&// TODO Auto-generated catch block
&&&&&e.printStackTrace();
&&&// 删掉最后一个 & 字符
&&&stringBuffer.deleteCharAt(stringBuffer.length() - 1);
----------------------------------------------------------------------------------------------------------------
程序思路:
1. 客户端建立http链接httpURLConnection,使用OutputStream向服务器传入数据
2. 获得从服务器端返回的输入流InputStream
3. 将InputStream转换成字符串String
----------------------------------------------------------------------------------------------------------------
程序运行效果:
1.客户端运行效果
2. 服务器端运行结果
关键代码:
1. 客户端 HttpUtils.java
package com.http.
import java.io.ByteArrayOutputS
import java.io.IOE
import java.io.InputS
import java.io.OutputS
import java.io.UnsupportedEncodingE
import java.net.HttpURLC
import java.net.MalformedURLE
import java.net.URL;
import java.net.URLE
import java.util.HashM
import java.util.M
public class HttpUtils {
// 表示服务器端的url
private static String PATH = &http://192.168.0.100:8080/myhttp/servlet/LoginAction&;
private static URL
public HttpUtils() {
// TODO Auto-generated constructor stub
url = new URL(PATH);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
* params 填写的URL的参数 encode 字节编码
public static String sendPostMessage(Map&String, String& params,
String encode) {
StringBuffer stringBuffer = new StringBuffer();
if (params != null && !params.isEmpty()) {
for (Map.Entry&String, String& entry : params.entrySet()) {
stringBuffer
.append(entry.getKey())
.append(&=&)
.append(URLEncoder.encode(entry.getValue(), encode))
.append(&&&);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 删掉最后一个 & 字符
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
System.out.println(&--&&& + stringBuffer.toString());
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);// 从服务器获取数据
httpURLConnection.setDoOutput(true);// 向服务器写入数据
// 获得上传信息的字节大小及长度
byte[] mydata = stringBuffer.toString().getBytes();
// 设置请求体的类型
httpURLConnection.setRequestProperty(&Content-Type&,
&application/x-www-form-urlencoded&);
httpURLConnection.setRequestProperty(&Content-Lenth&,
String.valueOf(mydata.length));
// 获得输出流,向服务器输出数据
OutputStream outputStream = (OutputStream) httpURLConnection
.getOutputStream();
outputStream.write(mydata);
// 获得服务器响应的结果和状态码
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 获得输入流,从服务器端获得数据
InputStream inputStream = (InputStream) httpURLConnection
.getInputStream();
return (changeInputStream(inputStream, encode));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return &&;
* // 把从输入流InputStream按指定编码格式encode变成字符串String
public static String changeInputStream(InputStream inputStream,
String encode) {
// ByteArrayOutputStream 一般叫做内存流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = &&;
if (inputStream != null) {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
result = new String(byteArrayOutputStream.toByteArray(), encode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
Map&String, String& params = new HashMap&String, String&();
params.put(&username&, &admin&);
params.put(&password&, &123&);
String result = sendPostMessage(params, &utf-8&);
System.out.println(&-result-&&& + result);
服务器端: LoginAction.java
package com.login.
import java.io.IOE
import java.io.PrintW
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
public class LoginAction extends HttpServlet {
* Constructor of the object.
public LoginAction() {
* Destruction of the servlet. &br&
public void destroy() {
super.destroy(); // Just puts &destroy& string in log
// Put your code here
* The doGet method of the servlet. &br&
* This method is called when a form has its tag value method equals to get.
* @param request
the request send by the client to the server
* @param response
the response send by the server to the client
* @throws ServletException
if an error occurred
* @throws IOException
if an error occurred
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
* The doPost method of the servlet. &br&
* This method is called when a form has its tag value method equals to
* @param request
the request send by the client to the server
* @param response
the response send by the server to the client
* @throws ServletException
if an error occurred
* @throws IOException
if an error occurred
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(&text/charset=utf-8&);
request.setCharacterEncoding(&utf-8&);
response.setCharacterEncoding(&utf-8&);
//客户端 HttpUtils并没有写request方法是post ,但服务器端可自动识别
String method = request.getMethod();
System.out.println(&request method :&+method);
PrintWriter out = response.getWriter();
String username = request.getParameter(&username&);
System.out.println(&-username-&&&+username);
String password = request.getParameter(&password&);
System.out.println(&-password-&&&+password);
if (username.equals(&admin&) && password.equals(&123&)) {
// 表示服务器段返回的结果
out.print(&login is success !&);
out.print(&login is fail !&);
out.flush();
out.close();
* Initialization of the servlet. &br&
* @throws ServletException
if an error occurs
public void init() throws ServletException {
// Put your code here
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致dogegg250 的BLOG
用户名:dogegg250
文章数:133
评论数:30
访问量:236630
注册日期:
阅读量:5863
阅读量:12276
阅读量:364605
阅读量:1059823
51CTO推荐博文
&& &网上有很多讲解决Servlet中文乱码的问题,一般的解决方案是加一个过滤器,在doFilter方法中加入:
request.setCharacterEncoding(&UTF-8&);&
可是这样并不能解决GET方式传递的数据。如果你能修改tomcat的配置文件,你可以把网址的编码设为UTF-8(或其它):在server.xml的&Connector& 加入 URIEncoding=&UTF-8&。当然,更大的可能性是你没有权限修改这个文件,或者有多个网站,但使用了不同的编码。所以需要在doFilter中判断是get还是post后再作相应的处理
if(httpServletRequest.getMethod().toLowerCase().equals(&get&)){&&&&&&&&&&&&&for(String[]&strs&:&request.getParameterMap().values()){&&&&&&&&&&&&&&&&&for(int&i&=&0;&i&&&strs.&i++){&&&&&&&&&&&&&&&&&&&&&strs[i]&=&new&String(strs[i].getBytes(&ISO-8859-1&),&&UTF-8&);&&&&&&&&&&&&&&&&&&&&&System.out.println(strs[i]);&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&}&&&&&&&&&}&&&&&&&&&else{&&&&&&&&&&&&&request.setCharacterEncoding(&UTF-8&);&&&&&&&&&}&
这样看起来似乎是完美了,但是实际应用中,经常使用到混合POST和GET方式提交。如用POST方式提交一个单表到/deal?page=2。如果使用上述的过滤器,自然是按POST方式进行处理,那通过网址传过来的中文数据就会乱码了。
好在可以得到QueryString,似乎只要把QueryString中的出现的参数处理一下就行了(先忽略重复name的问题):
HttpServletRequest&httpServletRequest&=&(HttpServletRequest)&&String[]&expressions&=&httpServletRequest.getQueryString().split(&&&);&&&&&&&&&for(String&expression&:&expressions){&&&&&&&&&&&&&String[]&parts&=&expression.split(&=&);&&&&&&&&&&&&&if(parts.length&==&2){&&&&&&&&&&&&&&&&&request.getParameterMap().put(parts[0],&new&String[]{&decode(parts[1])});&&&&&&&&&&&&&}&&&&&&&&&}&
运行下。。。OMG。。。java.lang.IllegalStateException: No modifications are allowed to a locked ParameterMap。原来这个Map是不能添加值的。可是第一个过滤器中我却成功修改了值,所以产生了第一种方案,我不用put,我去改values。
public&void&doFilter(ServletRequest&request,&ServletResponse&response,&FilterChain&chain)&throws&IOException,&ServletException&{&&&&&&&&&request.setCharacterEncoding(&UTF-8&);&//这句得在下面那个强制转换之前,不知道为什么。这句用来处理post的情况&&&&&&&&&&&&&&&&&HttpServletRequest&httpServletRequest&=&(HttpServletRequest)&&&&&&&&&&
//建立一个ArrayList用来存所有的Get方式提交的name对应在Parameter里的value&&&&&&&&&ArrayList&String&&queryValues&=&new&ArrayList&String&();&&&&&&&&&Map&String,&String[]&&map&=&request.getParameterMap();&&&&&&&&&String&query&=&httpServletRequest.getQueryString();&&&&&&&&&if(query&!=&null){&&&&&&&&&&&&&String[]&expressions&=&query.split(&&&);&&&&&&&&&&&&&for(String&expression&:&expressions){&&&&&&&&&&&&&&&&&String[]&parts&=&expression.split(&=&);&&&&&&&&&&&&&&&&&if(parts.length&==&2){&&&&&&&&&&&&&&&&&&&&&String[]&values&=&map.get(parts[0]);&&&&&&&&&&&&&&&&&&&&&for(String&value&:&values){&&&&&&&&&&&&&&&&&&&&&&&&&queryValues.add(value);&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&}&&&&&&&&&}&&//遍历value,如果是get方式提交的(存在于queryValues中),就转一下编码&&&&&&&&for(String[]&strs&:&request.getParameterMap().values()){&&&&&&&&&&&&&for(int&i&=&0;&i&&&strs.&i++){&&&&&&&&&&&&&&&&&for(String&qStr&:&queryValues){&&&&&&&&&&&&&&&&&&&&&if(qStr.equals(strs[i])){&&&&&&&&&&&&&&&&&&&&&&&&&strs[i]&=&new&String(qStr.getBytes(&ISO-8859-1&),&&UTF-8&);&&&&&&&&&&&&&&&&&&&&&&&&&//ISO-8859-1是tomcat中默认的,UTF-8是urlencode的编码
break;&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&}&&&&&&&&&}&&&&&&&&&&chain.doFilter(request,&response);&&&&&}&
这样做蛮复杂的,效率又不高。如果post和get中有相同的name,就会出错,可以稍加修改:
public&void&doFilter(ServletRequest&request,&ServletResponse&response,&FilterChain&chain)&throws&IOException,&ServletException&{&&&&&&&&&request.setCharacterEncoding(&UTF-8&);&&&&&&&&&&&&&&&&&&HttpServletRequest&httpServletRequest&=&(HttpServletRequest)&&&&&&&&&&&&&&&&&&&ArrayList&String&&queryValues&=&new&ArrayList&String&();&&&&&&&&&Map&String,&String[]&&map&=&request.getParameterMap();&&&&&&&&&String&query&=&httpServletRequest.getQueryString();&&&&&&&&&if(query&!=&null){&&&&&&&&&&&&&String[]&expressions&=&query.split(&&&);&&&&&&&&&&&&&for(String&expression&:&expressions){&&&&&&&&&&&&&&&&&String[]&parts&=&expression.split(&=&);&&&&&&&&&&&&&&&&&if(parts.length&==&2){&&&&&&&&&&&&&&&&&&&&&&&&&//将上面这段修改为&&&&&&&&&&&&&&&&&&&&String&str&=&URLDecoder.decode(parts[1],&&UTF-8&);&&&&&&&&&&&&&&&&&&&&&System.out.println(str);&&&&&&&&&&&&&&&&&&&&&queryValues.add(new&String(str.getBytes(&UTF-8&),&&ISO-8859-1&));&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&}&&&&&&&&&}&&&&&&&&&&for(String[]&strs&:&request.getParameterMap().values()){&&&&&&&&&&&&&for(int&i&=&0;&i&&&strs.&i++){&&&&&&&&&&&&&&&&&for(String&qStr&:&queryValues){&&&&&&&&&&&&&&&&&&&&&if(qStr.equals(strs[i])){&&&&&&&&&&&&&&&&&&&&&&&&&strs[i]&=&new&String(qStr.getBytes(&ISO-8859-1&),&&UTF-8&);&&&&&&&&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&}&&&&&&&&&}&&&&&&&&&&chain.doFilter(request,&response);&&&&&}&
另一种方案是,把parameter中的成员都放到Attribute中,然后把QueryString中的成员也放到Attribute中,覆盖了parameter中的GET方式提交的成员。缺点是,数据得用getAttribute来读了...
&本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)
15:17:25 22:54:53 15:37:09 15:25:14servlet获取POST请求以及转换为json对象
* 根据Request以及Class获取对象
* @throws Exception
* @throws InstantiationException
*/ public static &T extends BaseBean& T createBaseBean(HttpServletRequest request,Class&T& cs) throws Exception{
t = cs.newInstance();
Method[] methods = cs.getMethods();
JSONObject jsonObject = new JSONObject(getPostParameter(request));
@SuppressWarnings("rawtypes")
Iterator i = jsonObject.keys();
while(i.hasNext()){
String s = (String) i.next();
(Method method:methods){
if(method.getName().equalsIgnoreCase("set"+s)){
method.invoke(t, jsonObject.getString(s));
catch(IOException e)
throw new Exception(e.get(),e);
catch(Exception e)
throw new Exception("Create BaseBean Error",e);
* 根据request获取Post参数
* @param request
* @throws IOException
*/ private static String getPostParameter(HttpServletRequest request) throws IOException{
BufferedInputStream buf =
int iContentLen = request.getContentLength();
byte sContent[] = new byte[iContentLen];
String sContent2 =
buf = new BufferedInputStream(request.getInputStream());
buf.read(sContent, 0, sContent.length);
sContent2 = new String(sContent,0,iContentLen,"UTF-8");
} catch (IOException e) {
throw new IOException("Parse data error!",e);
buf.close();
} catch (IOException e) {
return sContent2; }

我要回帖

更多关于 python 调用shell 的文章

 

随机推荐