Android使用AsyncTask加http用post方式上传图片到服务器
来源:互联网 发布:素描照片的软件 编辑:程序博客网 时间:2024/05/23 15:42
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(); } }
服务器端:
PrintWriter out = response.getWriter();FileUploadTool fut=new FileUploadTool(request,"photo");String name=fut.getParameter("file1");out.print(name);
0 0
- Android使用AsyncTask加http用post方式上传图片到服务器
- Android使用post方式上传图片到服务器的方法
- Android使用post方式上传图片到服务器的方法
- android 用post方式上传图片到服务器
- android 用post方式上传图片到服务器
- 将本地图片或者网上图片用post方式上传到图片服务器
- 将本地图片或者网上图片用post方式上传到图片服务器
- post上传图片到服务器
- Android上传图片到服务器,android-async-http上传图片到服务器
- android使用PHP上传图片到服务器
- Android使用Retrofit上传图片到服务器
- 通过 HTTP POST 上传文件到服务器.
- 通过 HTTP POST 上传文件到服务器.
- 通过 HTTP POST 上传文件到服务器
- iOS 用HTTP post上传图片到OSS
- Qt Post上传图片文件到服务器
- 关于使用http请求上传图片到服务器
- 纯vc sdk实现http post 方式上传数据到web服务器
- Android基础动画,组合动画,帧动画,布局动画,Activity跳转动画
- Python 机器学习的开发环境搭建(numpy,scipy,matplotlib)
- 苹果助手开发随笔系列:3、后续功能所需要的服务
- 如何设计一个RPC系统
- JSP基础
- Android使用AsyncTask加http用post方式上传图片到服务器
- Extjs中创建Store数据源的方式
- easyui pagination buttons元素上添加title显示
- Android NetUtils-跟网络相关的工具类
- ViewDragHelper 使用
- 单例模式的俩种表现形式
- Spring MVC <welcome-file-list> 问题
- 【牛客网】二叉搜索树的后序遍历序列
- Linux下syslog日志函数使用