标签:android 郑传余 文件上传 服务器 httppost
项目演示及讲解
优酷 http://v.youku.com/v_show/id_XODk5NjkwOTg4.html
爱奇艺 http://www.iqiyi.com/w_19rs1v2m15.html#vfrm=8-7-0-1
土豆 http://www.tudou.com/programs/view/fv0H93IHfhM
项目下载
1、手机端选择文件上传至服务器端
http://download.csdn.net/detail/u010134178/8457679
2、手机端拍照上传至服务器端
http://download.csdn.net/detail/u010134178/8457673
3、总下载地址
http://down.51cto.com/7851921/up
这些都是需要积分的,如果积分不够的话发送到我邮箱whsgzcy@foxmail.com我直接把项目发送过来
大家在调试的同时一定要注意
1、网络下载、上传这些操作,就是耗时的操作,要另外开线程操作,不能放到主线程里面。
2、网络下载、上传里面不能有ui操作,不能在里面显示ui。就是不能在子线程里面操作UI。如果操作完毕ui提示用户等操作,可以使用利用handler结合Thread更新UI,或者AsyncTask异步更新UI。
3、上传到本地Tomcat服务器,要关闭防火墙
接下来将给出两个项目部分代码,当然两个项目都有一个工具类HttpPost
public class HttpPost {
/**
* 通过拼接的方式构造请求内容,实现参数传输以及文件传输
*
* @param acti
* .nUrl
* @param params
* @param files
* @return
* @throws IOException
*/
public static String post(String actionUrl, Map<String, String> params, Map<String, File> files) throws IOException {
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000); // 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false); // 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
// 首先组拼文本类型的参数
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());
// 发送文件数据
if (files != null)
for (Map.Entry<String, File> file : files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.write(LINEND.getBytes());
}
// 请求结束标志
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
// 得到响应码
int res = conn.getResponseCode();
InputStream in = conn.getInputStream();
if (res == 200) {
int ch;
StringBuilder sb2 = new StringBuilder();
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
}
outStream.close();
conn.disconnect();
return in.toString();
}
}手机拍照上传至服务器主要代码
public void uploadPhoto() {
Map<String, String> params = new HashMap<String, String>();
params.put("strParamName", "strParamValue");
Map<String, File> files = new HashMap<String, File>();
files.put(System.currentTimeMillis()+".jpg", new File(uploadFile));//uploadFile = "/sdcard/AnBo/laolisb.jpg";
try {
String str = HttpPost.post(actionUrl, params, files);
System.out.println("str--->>>" + str);
} catch (Exception e) {
}
}选择文件上传至服务器主要代码
/******************************************************/
private String fun(String msg){
int i = msg.length();
int j = msg.lastIndexOf("/") + 1;
String a = msg.substring(j, i) ;
System.out.println(a);
return a;
}
/******************************************************/
private void uploadFile() {
show.setVisibility(View.VISIBLE);
new Thread() {
@Override
public void run() {
Message msg = Message.obtain();
// 服务器的访问路径
String uploadUrl = "http://192.168.0.104:8080/UploadPhoto1/UploadServlet";
Map<String, File> files = new HashMap<String, File>();
String name = fun(picturePath);
files.put(name, new File(picturePath));
//files.put("test.jpg", new File(picturePath));
Log.d("str--->>>", picturePath);
try {
String str = HttpPost.post(uploadUrl, new HashMap<String,String>(), files);
System.out.println("str--->>>" + str);
msg.what = SUCCESS;
} catch (Exception e) {
msg.what = FAILD;
}
mHandler.sendMessage(msg);
}
}.start();
}本文出自 “7851921” 博客,请务必保留此出处http://7861921.blog.51cto.com/7851921/1615556
标签:android 郑传余 文件上传 服务器 httppost
原文地址:http://7861921.blog.51cto.com/7851921/1615556