android 图片上传 后台php
来源:互联网 发布:js 水球图 编辑:程序博客网 时间:2024/06/05 19:07
Android:
public class EX08_11 extends Activity{ /* 变量声明 * newName:上传后在服务器上的文件名称 * uploadFile:要上传的文件路径 * actionUrl:服务器对应的程序路径 */// private String newName="345444.jpg"; private String uploadFile="/sdcard/345444.jpg"; private String actionUrl="http://*********/upload.php"; private TextView mText1; private TextView mText2; private Button mButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mText1 = (TextView) findViewById(R.id.myText2); mText1.setText("文件路径:\n"+uploadFile); mText2 = (TextView) findViewById(R.id.myText3); mText2.setText("上传网址:\n"+actionUrl); /* 设定mButton的onClick事件处理 */ mButton = (Button) findViewById(R.id.myButton); mButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { uploadFile(); } }); } /* 上传文件吹Server的method */ private void uploadFile() {// String end = "\r\n";// String twoHyphens = "--"; String boundary = "*****"; try { URL url =new URL(actionUrl); HttpURLConnection con=(HttpURLConnection)url.openConnection(); /* 允许Input、Output,不使用Cache */// con.setReadTimeout(5 * 1000); 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("enctype", "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 */ int bufferSize = 1024; 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 */ showDialog(b.toString().trim()); /* 关闭DataOutputStream */ ds.close(); } catch(Exception e) { showDialog(""+e); } } /* 显示Dialog的method */ private void showDialog(String mess) { new AlertDialog.Builder(EX08_11.this).setTitle("Message") .setMessage(mess) .setNegativeButton("确定",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .show(); }}
PHP程序:
$data = file_get_contents('php://input');$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/image/345444.jpg', 'w');if ($handle){ fwrite($handle,$data); fclose($handle); echo "success";}
- android 图片上传 后台php
- android 图片上传以及php后台代码
- android上传图片到PHP后台全过程
- 实现android上传多张图片和文字给php后台
- android上传单张图片到后台
- android 图片上传 php端
- Android 客户端上传数据到 php后台
- php 数组命名上传图片后台获取格式
- 【imooc学习】【PHP】电商网站后台开发-图片上传
- PHP后台技术-上传图片与分页技术
- 后台图片上传限制
- android 图片上传java,php服务器
- android 上传图片到php服务器
- android使用PHP上传图片到服务器
- php接收ios/android上传图片
- Android 上传图片到服务器(PHP)
- PHP图片上传接口 Android/iOS
- Android OkHttp 结合php 多图片上传
- OpenDaylight学习 ( by quqi99 )
- Android onCreateContextMenu onContextItemSelected获取view信息
- 电驴提示“该内容尚未提供权利证明,无法提供下载”之解决办法详解
- 正则表达式概述与JAVA中正则表达式的应用
- HDU 1372 BFS
- android 图片上传 后台php
- cisco命令 login与login local的区别
- openssh(scp、sftp、slogin、ssh)
- Android Application对象必须掌握的七点
- poj 1330 nearest common ancestor
- Struts2(11):配置默认Action
- poj-2195-Going Home最小费用最大流
- 10375 - Choose and divide
- UILabel,DTAttributedLabel 关于计算高度