• <bdo id='IZySA'></bdo><ul id='IZySA'></ul>
    <tfoot id='IZySA'></tfoot>

    <small id='IZySA'></small><noframes id='IZySA'>

        <legend id='IZySA'><style id='IZySA'><dir id='IZySA'><q id='IZySA'></q></dir></style></legend>
      1. <i id='IZySA'><tr id='IZySA'><dt id='IZySA'><q id='IZySA'><span id='IZySA'><b id='IZySA'><form id='IZySA'><ins id='IZySA'></ins><ul id='IZySA'></ul><sub id='IZySA'></sub></form><legend id='IZySA'></legend><bdo id='IZySA'><pre id='IZySA'><center id='IZySA'></center></pre></bdo></b><th id='IZySA'></th></span></q></dt></tr></i><div id='IZySA'><tfoot id='IZySA'></tfoot><dl id='IZySA'><fieldset id='IZySA'></fieldset></dl></div>

        Android:使用 php 在服务器上上传图像

        Android: Uploading image on server with php(Android:使用 php 在服务器上上传图像)
          <tbody id='wyPO1'></tbody>

        1. <i id='wyPO1'><tr id='wyPO1'><dt id='wyPO1'><q id='wyPO1'><span id='wyPO1'><b id='wyPO1'><form id='wyPO1'><ins id='wyPO1'></ins><ul id='wyPO1'></ul><sub id='wyPO1'></sub></form><legend id='wyPO1'></legend><bdo id='wyPO1'><pre id='wyPO1'><center id='wyPO1'></center></pre></bdo></b><th id='wyPO1'></th></span></q></dt></tr></i><div id='wyPO1'><tfoot id='wyPO1'></tfoot><dl id='wyPO1'><fieldset id='wyPO1'></fieldset></dl></div>

          <tfoot id='wyPO1'></tfoot>
          <legend id='wyPO1'><style id='wyPO1'><dir id='wyPO1'><q id='wyPO1'></q></dir></style></legend>
          • <small id='wyPO1'></small><noframes id='wyPO1'>

              • <bdo id='wyPO1'></bdo><ul id='wyPO1'></ul>
                  本文介绍了Android:使用 php 在服务器上上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  始终在服务器上上传 0KB 的图片 test.jpg!

                  Always upload image test.jpg with 0KB on the server!

                  图片如下:

                  所以你可以看到我在服务器上得到了什么.

                  So you can see what am i getting on the server.

                  我将不胜感激任何帮助或建议,我希望我能解决问题..

                  I would appreciate any help or suggesting and i hope i will solve the problem..

                  安卓代码:

                       public class Main extends Activity {
                  
                  InputStream inputStream;
                     @Override
                     public void onCreate(Bundle icicle) {
                      super.onCreate(icicle);
                      setContentView(R.layout.main);
                  
                      Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);   
                      ByteArrayOutputStream stream = new ByteArrayOutputStream();
                      bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
                      byte [] byte_arr = stream.toByteArray();
                      String image_str = Base64.encodeBytes(byte_arr);
                      ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();
                  
                      nameValuePairs.add(new BasicNameValuePair("image",image_str));
                  
                      try{
                          HttpClient httpclient = new DefaultHttpClient();
                          HttpPost httppost = new HttpPost("http://server/uploader2.php");
                          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                          HttpResponse response = httpclient.execute(httppost);
                          String the_string_response = convertResponseToString(response);
                          Toast.makeText(this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
                      }catch(Exception e){
                            Toast.makeText(this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
                            System.out.println("Error in http connection "+e.toString());
                      }
                  }
                  
                  public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{
                  
                       String res = "";
                       StringBuffer buffer = new StringBuffer();
                       inputStream = response.getEntity().getContent();
                       int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
                       Toast.makeText(this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
                       if (contentLength < 0){
                       }
                       else{
                              byte[] data = new byte[512];
                              int len = 0;
                              try
                              {
                                  while (-1 != (len = inputStream.read(data)) )
                                  {
                                      buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
                                  }
                              }
                              catch (IOException e)
                              {
                                  e.printStackTrace();
                              }
                              try
                              {
                                  inputStream.close(); // closing the stream…..
                              }
                              catch (IOException e)
                              {
                                  e.printStackTrace();
                              }
                              res = buffer.toString();     // converting stringbuffer to string…..
                  
                              Toast.makeText(this, "Result : " + res, Toast.LENGTH_LONG).show();
                              //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
                       }
                       return res;
                  
                  
                  }}
                  

                  和php代码...(uploader2.php)

                  and php code... (uploader2.php)

                  <?php
                  $base=$_REQUEST['image'];
                  $binary=base64_decode($base);
                  header('Content-Type: image/jpg; charset=utf-8');
                  $file = fopen('test.jpg', 'wb');
                  fwrite($file, $binary);
                  fclose($file);
                  echo 'Image upload complete!!, Please check your php file directory……';
                  echo "<img src=test.jpg>";
                  ?>
                  

                  有什么问题?

                  推荐答案

                  doFileUpload函数:

                  doFileUpload Function :

                  private void doFileUpload(){
                      HttpURLConnection conn = null;
                      DataOutputStream dos = null;
                      DataInputStream inStream = null; 
                      String exsistingFileName = "/sdcard/six.3gp";
                      // Is this the place are you doing something wrong.
                      String lineEnd = "
                  ";
                      String twoHyphens = "--";
                      String boundary =  "*****";
                      int bytesRead, bytesAvailable, bufferSize;
                      byte[] buffer;
                      int maxBufferSize = 1*1024*1024;
                      String urlString = "http://192.168.1.5/upload.php";
                      try
                      {
                          Log.e("MediaPlayer","Inside second Method");
                          FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
                          URL url = new URL(urlString);
                          conn = (HttpURLConnection) url.openConnection();
                          conn.setDoInput(true);
                          // Allow Outputs
                          conn.setDoOutput(true);
                          // Don't use a cached copy.
                          conn.setUseCaches(false);
                          // Use a post method.
                          conn.setRequestMethod("POST");
                          conn.setRequestProperty("Connection", "Keep-Alive");
                          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
                          dos = new DataOutputStream( conn.getOutputStream() );
                          dos.writeBytes(twoHyphens + boundary + lineEnd);
                          dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + exsistingFileName +""" + lineEnd);
                          dos.writeBytes(lineEnd);
                          Log.e("MediaPlayer","Headers are written");
                          bytesAvailable = fileInputStream.available();
                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
                          buffer = new byte[bufferSize];
                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                          while (bytesRead > 0)
                          {
                              dos.write(buffer, 0, bufferSize);
                              bytesAvailable = fileInputStream.available();
                              bufferSize = Math.min(bytesAvailable, maxBufferSize);
                              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                          }
                          dos.writeBytes(lineEnd);
                          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                          String inputLine;
                          while ((inputLine = in.readLine()) != null) 
                              tv.append(inputLine);
                          // close streams
                          Log.e("MediaPlayer","File is written");
                          fileInputStream.close();
                          dos.flush();
                          dos.close();
                      }
                      catch (MalformedURLException ex)
                      {
                          Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
                      }
                      catch (IOException ioe)
                      {
                          Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
                      }
                  
                      //------------------ read the SERVER RESPONSE
                      try {
                          inStream = new DataInputStream ( conn.getInputStream() );
                          String str;            
                          while (( str = inStream.readLine()) != null)
                          {
                              Log.e("MediaPlayer","Server Response"+str);
                          }
                          /*while((str = inStream.readLine()) !=null ){
                  
                          }*/
                          inStream.close();
                      }
                      catch (IOException ioex){
                          Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
                      }
                  }
                  

                  上传.php

                  <?php
                  
                  move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "./upload/".$_FILES["uploadedfile"]["name"]);
                  
                  mysql_connect("localhost","root","");
                          mysql_select_db("chat");
                  
                  
                  if(isset($_REQUEST['msg']))
                  {
                      $a = $_REQUEST['msg'];
                      $sql = "INSERT INTO  upload(image)VALUES('$a')";
                      mysql_query($sql);
                      }
                  ?>
                  

                  这篇关于Android:使用 php 在服务器上上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tbody id='0Smem'></tbody>
                  <i id='0Smem'><tr id='0Smem'><dt id='0Smem'><q id='0Smem'><span id='0Smem'><b id='0Smem'><form id='0Smem'><ins id='0Smem'></ins><ul id='0Smem'></ul><sub id='0Smem'></sub></form><legend id='0Smem'></legend><bdo id='0Smem'><pre id='0Smem'><center id='0Smem'></center></pre></bdo></b><th id='0Smem'></th></span></q></dt></tr></i><div id='0Smem'><tfoot id='0Smem'></tfoot><dl id='0Smem'><fieldset id='0Smem'></fieldset></dl></div>
                  <legend id='0Smem'><style id='0Smem'><dir id='0Smem'><q id='0Smem'></q></dir></style></legend>
                • <small id='0Smem'></small><noframes id='0Smem'>

                      • <bdo id='0Smem'></bdo><ul id='0Smem'></ul>

                          <tfoot id='0Smem'></tfoot>