Android使用AsyncTask加http用post方式上传图片到服务器
2016-11-07 18:48
786 查看
android端代码展示:
/* 上传文件至Server的方法 */
private void uploadFile() {
System.out.print("正在发送请求!");
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
String urlpost=actionUrl;
URL url = new URL(urlpost);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
/* 设置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file1\";filename=\"" + newName + "\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
System.out.print("已经找到数据正在发送!");
int bufferSize = 1024*10;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while ((length = fStream.read(buffer)) != -1) {
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
/* 将Response显示于Dialog */
/* 关闭DataOutputStream */
ds.close();
try {
InfoUrl=InfoUrl+"?username="+username.getText()+"&password="+password.getText()+"&img="+b.toString();
Log.i("login", "uploadFile: "+InfoUrl);
HttpURLConnection huc= (HttpURLConnection) new URL(InfoUrl).openConnection();
huc.setDoInput(true);
huc.setDoOutput(true);
huc.setUseCaches(false);
/* 设置传送的method=POST */
huc.setRequestMethod("GET");
huc.connect();
InputStream is1 = huc.getInputStream();
int ch1;
StringBuffer b1 = new StringBuffer();
while ((ch1 = is1.read()) != -1) {
b1.append((char) ch1);
}
int result=Integer.parseInt(b1.toString());
if(result>0) {
showDialog("注册成功,请登录!");
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}else {
showDialog("注册失败,请检查!");
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.print("网络出现异常!");
showDialog("上传失败");
e.printStackTrace();
}
}
服务器端:
/* 上传文件至Server的方法 */
private void uploadFile() {
System.out.print("正在发送请求!");
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
String urlpost=actionUrl;
URL url = new URL(urlpost);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
/* 设置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file1\";filename=\"" + newName + "\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
System.out.print("已经找到数据正在发送!");
int bufferSize = 1024*10;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while ((length = fStream.read(buffer)) != -1) {
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
/* 将Response显示于Dialog */
/* 关闭DataOutputStream */
ds.close();
try {
InfoUrl=InfoUrl+"?username="+username.getText()+"&password="+password.getText()+"&img="+b.toString();
Log.i("login", "uploadFile: "+InfoUrl);
HttpURLConnection huc= (HttpURLConnection) new URL(InfoUrl).openConnection();
huc.setDoInput(true);
huc.setDoOutput(true);
huc.setUseCaches(false);
/* 设置传送的method=POST */
huc.setRequestMethod("GET");
huc.connect();
InputStream is1 = huc.getInputStream();
int ch1;
StringBuffer b1 = new StringBuffer();
while ((ch1 = is1.read()) != -1) {
b1.append((char) ch1);
}
int result=Integer.parseInt(b1.toString());
if(result>0) {
showDialog("注册成功,请登录!");
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}else {
showDialog("注册失败,请检查!");
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.print("网络出现异常!");
showDialog("上传失败");
e.printStackTrace();
}
}
服务器端:
PrintWriter out = response.getWriter(); FileUploadTool fut=new FileUploadTool(request,"photo"); String name=fut.getParameter("file1"); out.print(name);
相关文章推荐
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Android使用post方式上传图片到服务器的方法
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Android使用post方式上传图片到服务器的方法
- Android使用post方式上传图片到服务器的方法
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- Windows Phone 使用 HttpWebRequest 对象,通过POST 方式上传图片
- android----HttpClient的get,post和图片上传服务器
- Android使用HttpURLConnection进行POST请求,向服务器上传数据
- android----HttpClient的get,post和图片上传服务器
- android使用HttpUrlCollection上传图片到服务器demo
- Android中使用HttpPost上传图片和数据
- java(android)客户端post方式上传多图片至服务器
- android 用post方式上传图片到服务器
- UWP 使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- 使用最原始的http-post方式上传图片 ,上传工具类已封装 可直接使用
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)