当前位置:Gxlcms > PHP教程 > Android图片异步上传到PHP服务器实例

Android图片异步上传到PHP服务器实例

时间:2021-07-01 10:21:17 帮助过:20人阅读

背景

网上很多上传到java服务器上的,找了好久,找到了上传到php的了,思路跟我当初想的差不多,就是POST过去。废话不多说,直接上图看代码。

PHP代码

PHP代码

  1. $target_path = "./upload/" ; //接收文件目录
  2. $target_path = $target_path . basename ( $_FILES [ 'uploadedfile' ][ 'name' ]);
  3. if (move_uploaded_file( $_FILES [ 'uploadedfile' ][ 'tmp_name' ], $target_path )) {
  4. echo "The file " . basename ( $_FILES [ 'uploadedfile' ][ 'name' ]). " has been uploaded" ;
  5. } else {
  6. echo "There was an error uploading the file, please try again!" . $_FILES [ 'uploadedfile' ][ 'error' ];
  7. }
  8. ?>

Android代码

上传的主要代码:

Java代码

  1. private void uploadFile(String uploadUrl)
  2. {
  3. String end = "\r\n" ;
  4. String twoHyphens = "--" ;
  5. String boundary = "******" ;
  6. try
  7. {
  8. URL url = new URL(uploadUrl);
  9. HttpURLConnection httpURLConnection = (HttpURLConnection) url
  10. .openConnection(); //http连接
  11. // 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
  12. httpURLConnection.setChunkedStreamingMode( 128 * 1024 ); // 128K
  13. // 允许输入输出流
  14. httpURLConnection.setDoInput( true );
  15. httpURLConnection.setDoOutput( true );
  16. httpURLConnection.setUseCaches( false );
  17. // 使用POST方法
  18. httpURLConnection.setRequestMethod( "POST" );
  19. httpURLConnection.setRequestProperty( "Connection" , "Keep-Alive" ); //保持一直连接
  20. httpURLConnection.setRequestProperty( "Charset" , "UTF-8" ); //编码
  21. httpURLConnection.setRequestProperty( "Content-Type" ,
  22. "multipart/form-data;boundary=" + boundary); //POST传递过去的编码
  23. DataOutputStream dos = new DataOutputStream(
  24. httpURLConnection.getOutputStream()); //输出流
  25. dos.writeBytes(twoHyphens + boundary + end);
  26. dos.writeBytes( "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
  27. + srcPath.substring(srcPath.lastIndexOf( "/" ) + 1 )
  28. + "\""
  29. + end);
  30. dos.writeBytes(end);
  31. FileInputStream fis = new FileInputStream(srcPath); //文件输入流,写入到内存中
  32. byte [] buffer = new byte [ 8192 ]; // 8k
  33. int count = 0 ;
  34. // 读取文件
  35. while ((count = fis.read(buffer)) != - 1 )
  36. {
  37. dos.write(buffer, 0 , count);
  38. }
  39. fis.close();
  40. dos.writeBytes(end);
  41. dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
  42. dos.flush();
  43. InputStream is = httpURLConnection.getInputStream(); //http输入,即得到返回的结果
  44. InputStreamReader isr = new InputStreamReader(is, "utf-8" );
  45. BufferedReader br = new BufferedReader(isr);
  46. String result = br.readLine();
  47. Toast.makeText( this , result, Toast.LENGTH_LONG).show(); //将结果输出
  48. dos.close();
  49. is.close();
  50. } catch (Exception e)
  51. {
  52. e.printStackTrace();
  53. setTitle(e.getMessage());
  54. }
  55. }

因为安卓4.0之后耗时间的操作要求都在非UI线程中操作,即将前面的AsyncTask拿来用了吧~

AsyncTask传送门: http://www.cnblogs.com/yydcdut/p/3707960.html

在这个类中,将上传的操作放在doInBackground当中,可以有ProgressDialog显示上传了多少:

Java代码

  1. // Read file
  2. bytesRead = fileInputStream.read(buffer, 0 , bufferSize);
  3. while (bytesRead > 0 ) {
  4. outputStream.write(buffer, 0 , bufferSize);
  5. length += bufferSize;
  6. progress = ( int ) ((length * 100 ) / totalSize);
  7. publishProgress(progress);
  8. bytesAvailable = fileInputStream.available();
  9. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  10. bytesRead = fileInputStream.read(buffer, 0 , bufferSize);
  11. }
  12. outputStream.writeBytes(lineEnd);
  13. outputStream.writeBytes(twoHyphens + boundary + twoHyphens
  14. + lineEnd);
  15. publishProgress( 100 );

还有就是,注意权限哟:

XML/HTML代码

  1. < uses-permission android:name = "android.permission.INTERNET" />

人气教程排行