android开发_表单上传图片及其它参数
来源:互联网 发布:多用户商城源码 编辑:程序博客网 时间:2024/06/01 08:20
安卓中,对图片用表单上传方式去上传到服务器,一直都是我心中的痛....开始的时候,在上传文本的http请求服务器的方式去提交,提交图片失败,后来,使用了第三方的框架去做这个事情成功了,再后来,回归到自己用原生的代码去写这个提交图片,今天当是做一个总结吧。把一件重要的事情记录下来,给它画上句号吧。
图片表单上传方法:
1、
// modified by LPY//表单方式上传图片及其它参数private void doUploadImage() {try {String url = RequestUrl.BASE_WEB_URL + "imageupload.do?";HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(url);ContentBody fileBody = new FileBody(photoFile, "image/jpeg");MultipartEntity entity = new MultipartEntity();// TODO : mobile字符串该则么传要确认下entity.addPart("mobile", new StringBody(mobile));entity.addPart("key", new StringBody("WZDFKYT++"));entity.addPart("file", fileBody);post.setEntity(entity);HttpResponse response = client.execute(post);int statusCode = response.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {String result = EntityUtils.toString(response.getEntity(), "utf-8");JSONObject jsonObject = new JSONObject(result);logo = jsonObject.getString("path");Log.i("LPY_DEBUG","拍照后上传返回的网络图片路径logo = " + logo);Message msg = new Message();msg.what = jsonObject.getInt("result");msg.obj = jsonObject.getString("path");bindhandler.sendMessage(msg);} else {bindhandler.sendEmptyMessage(-1);}client.getConnectionManager().shutdown();} catch (Exception e) {bindhandler.sendEmptyMessage(-1);// e.printStackTrace();}}private Handler bindhandler = new Handler() {public void handleMessage(android.os.Message msg) {// progressDialog.dismiss();if (msg.what == 1) {}}};
2、把方法提取出来
/** * 提交参数里有文件的数据 * * @param url * 服务器地址 * @param param * 参数 * @return 服务器返回结果 * @throws Exception */public static String uploadSubmit(String url, Map<String, String> param,File file) throws Exception {HttpPost post = new HttpPost(url);MultipartEntity entity = new MultipartEntity();if (param != null && !param.isEmpty()) {for (Map.Entry<String, String> entry : param.entrySet()) {if (entry.getValue() != null&& entry.getValue().trim().length() > 0) {entity.addPart(entry.getKey(),new StringBody(entry.getValue()));}}}// 添加文件参数if (file != null && file.exists()) {entity.addPart("file", new FileBody(file));}post.setEntity(entity);HttpResponse response = httpClient.execute(post);int stateCode = response.getStatusLine().getStatusCode();StringBuffer sb = new StringBuffer();if (stateCode == HttpStatus.SC_OK) {HttpEntity result = response.getEntity();if (result != null) {InputStream is = result.getContent();BufferedReader br = new BufferedReader(new InputStreamReader(is));String tempLine;while ((tempLine = br.readLine()) != null) {sb.append(tempLine);}}}post.abort();return sb.toString();}
0 0
- android开发_表单上传图片及其它参数
- Android 图片及其参数一同上传
- 图片文件上传及其它操作
- Android 模拟表单上传 多图片上传
- android开发:使用Retrofit2框架,如果上传图片+json参数?
- Servlet获取form表单上传文件及其他参数
- ajax异步上传图片及其他表单项
- ios 表单上传图片和参数的2种方法
- Android 上传图片、上传文件、提交表单 工具类
- Android 开发之上传图片
- 计算机学习入门、进阶、Android开发及其它
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- 数据库游标使用mysql(全表操作)
- UIViewController模拟器的调用顺序
- 把Android原生的View渲染到OpenGL Texture
- Ubuntu 中文输入法的安装
- 关于C++中模板例化产生的代码
- android开发_表单上传图片及其它参数
- Eclipse安装SVN插件
- Nutch 实战
- 亿级Web系统搭建——单机到分布式集群
- Codeforces Round #271 (Div. 2) A B D
- linux内核空间和用户空间的是怎样区别的,如何交互,如何从用户空间进入内核空间
- HDU2102A计划(BFS)
- 走马观花: Linux 系统调用 open 七日游(六)
- Java中的static静态代码块的使用详解