一.首先了解http协议
https://www.cnblogs.com/ranyonsue/p/5984001.html(推荐这篇文章)
二.使用http协议上传
public static string Upload(string urlPath, string content)
{
string mContent = “”;
try
{
string BOUNDARY = “———7d4a6d158c9”; // 定义数据分隔线
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
// 发送POST请求必须设置如下两行
conn.DoOutput = true;
conn.DoInput = true;
conn.UseCaches = false;
conn.RequestMethod = “POST”;
conn.SetRequestProperty(“connection”, “Keep-Alive”);
conn.SetRequestProperty(“user-agent”, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)”);
conn.SetRequestProperty(“Charsert”, “UTF-8”);
conn.SetRequestProperty(“Content-Type”, “application/x-www-form-urlencoded; boundary=” + BOUNDARY);
Java.IO.OutputStream os = new Java.IO.DataOutputStream(conn.OutputStream);
Java.Lang.StringBuilder sb = new Java.Lang.StringBuilder();
/设置头参数/
sb.Append(“–”);
sb.Append(BOUNDARY);
sb.Append(“\r\n”);
/*填充内容*/
sb.Append("Content-Disposition: form-data; name=\"userName\" \r\n\r\n");
sb.Append(content);
sb.Append("\r\n");
/*设置尾参数*/
sb.Append("\r\n--");
sb.Append(BOUNDARY);
sb.Append("--\r\n");
byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
os.Write(data);
os.Flush();
os.Close();
// 定义BufferedReader输入流来读取URL的响应
Java.IO.BufferedReader reader = new Java.IO.BufferedReader(new Java.IO.InputStreamReader(conn.InputStream));
string line = null;
while ((line = reader.ReadLine()) != null)
{
mContent += line;
}
System.Console.WriteLine(mContent + "//");
return mContent;
}
catch (Java.Lang.Exception e)
{
//System.out.println("发送POST请求出现异常!" + e);
//e.printStackTrace();
}
return mContent;
}
服务器端接收值的代码是:
<%@page import=”com.jspsmart.upload.SmartUpload”%>
<%@ page language=”java” contentType=”text/html; charset=UTF-8”
pageEncoding=”UTF-8”%>
<%
SmartUpload su = new SmartUpload();
//初始化组件上下文
su.initialize(pageContext);
//设置编码
su.setCharset(“UTF-8”);
try {
su.upload();
} catch (Exception e) {
}
String clientContent=su.getRequest().getParameter("userName");
System.out.print("从android客户端传过来的用户名是:"+clientContent);
%>
这样就实现点击客户端的Button按钮之后,在java服务器端的控制台中救打印出了 admin字符串
三.使用http协议也能实现向服务器端上传
private string UpLoadUserName(string serverUrl, string content)
{
string mContent = “”;
try
{
URL url = new URL(serverUrl);
HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
// 发送POST请求必须设置如下两行
conn.DoOutput = true;
conn.DoInput = true;
conn.UseCaches = false;
conn.RequestMethod = “POST”;
conn.SetRequestProperty(“connection”, “Keep-Alive”);
conn.SetRequestProperty(“user-agent”, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)”);
conn.SetRequestProperty(“Charsert”, “UTF-8”);
//conn.SetRequestProperty(“Content-Type”, “application/x-www-form-urlencoded; boundary=” + BOUNDARY);
Java.IO.OutputStream os = new Java.IO.DataOutputStream(conn.OutputStream);
Java.Lang.StringBuilder sb = new Java.Lang.StringBuilder();
sb.Append(“userName=” + content);
byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
os.Write(data);
os.Flush();
os.Close();
// 定义BufferedReader输入流来读取URL的响应
Java.IO.BufferedReader reader = new Java.IO.BufferedReader(new Java.IO.InputStreamReader(conn.InputStream));
string line = null;
while ((line = reader.ReadLine()) != null)
{
mContent += line;
}
System.Console.WriteLine(mContent + “//”);
return mContent;
}
catch (Java.Lang.Exception e)
{
System.Console.WriteLine(“发送POST请求出现异常!!!” + e);
}
return mContent;
}
服务器端接收数据的代码:
<%@page import=”com.jspsmart.upload.SmartUpload”%>
<%@page import=”net.sf.json.JSONObject”%>
<%@ page language=”java” contentType=”text/html;charset=UTF-8”
pageEncoding=”UTF-8”%>
<%
String getName = request.getParameter(“userName”);
String clientContent = new String(getName.getBytes(“iso-8859-1”));//防止出现乱码
System.out.print(clientContent);
%>
区别就在于,不使用http协议上传,服务器端用的是系统自带的request类来接收客户端的请求!
如果使用http协议请求,服务器端用的是第三方的库 SmartUpLoad来接收客户端的传过来的数据!