【问题标题】:Android fileupload not workingAndroid文件上传不起作用
【发布时间】:2014-01-31 06:04:09
【问题描述】:

您好,我正在使用 android。我创建了一个将文件上传到服务器的 android 应用程序。我使用了以下代码

http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106

但我无法上传我的文件。该应用程序显示上传已开始,但没有文件上传到我的服务器位置。

【问题讨论】:

    标签: android upload


    【解决方案1】:
         private class SendHttpRequestTask extends AsyncTask<String, Void, String> {
    
            protected String doInBackground(String... params) {
    
               String url = params[0];
    
              String param1 = params[1];
    
              String param2 = params[2];
    
             Bitmap b = BitmapFactory.decodeResource(UploadActivity.this.getResources(), R.drawable.logo);
    
    
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
            b.compress(CompressFormat.PNG, 0, baos);
    
    
    
            try {
    
                 HttpClient client = new HttpClient(url);
    
                 client.connectForMultipart();
    
                 client.addFormPart("param1", param1);
    
                 client.addFormPart("param2", param2);
    
                 client.addFilePart("file", "logo.png", baos.toByteArray());
    
                 client.finishMultipart();
    
                  String data = client.getResponse();
    
             }
    
              catch(Throwable t) {
    
               t.printStackTrace();
    
           }
    
    
    
            return null;
    
         }
    
    
    
        @Override
    
          protected void onPostExecute(String data) {           
    
           item.setActionView(null);
    
    
    
           }
    
    
    
         }
    

    【讨论】:

      【解决方案2】:

      公共类保存扩展 AsyncTask { 私人 MyProgressDialog pDialog;

          @Override
          protected void onPreExecute() {
              pDialog = new MyProgressDialog(Send_message.this);
              pDialog.setCancelable(false);
              pDialog.show();
              super.onPreExecute();
          }
      
          @Override
          protected String doInBackground(String... paramss) {
              try {
                  String json = messagesend.sendmeesage("Your Url",params);
                  JSONObject jsonobject = new JSONObject(json);
                  String result = jsonobject.getString("result");
                  if (result.equals("TRUE")) {
                      return "true";
                  }
      
                  System.out.println("json is" + json);
              } catch (ClientProtocolException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (JSONException e) {
                  e.printStackTrace();
              }
              return "false";
          }
      
          @Override
          protected void onPostExecute(String result) {
              super.onPostExecute(result);
              pDialog.dismiss();
          }
      
      }
      

      //用于发送文件 公共类消息sendwithhttp {

      private String page;
      
      public String sendmeesage(String url, List<NameValuePair> nameValuePairs)throws ClientProtocolException, IOException 
      {
          HttpClient httpClient = new DefaultHttpClient();
          HttpContext localContext = new BasicHttpContext();
          HttpPost httpPost = new HttpPost(url);
      
          try {
              MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
      
              for (int index = 0; index < nameValuePairs.size(); index++) 
              {
                  if (index == 0 || index == 1) 
                  {
                      // If the key equals to "image", we use FileBody to transfer
                      // the data
                      entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
                  } else {
                      // Normal string data
                      if(nameValuePairs.get(index).getValue().equals("") || nameValuePairs.get(index).getValue().toString().equals(null))
                      {
                          entity.addPart(
                                  nameValuePairs.get(index).getName(),
                                  new StringBody(nameValuePairs.get(index).getValue()));
                      }
                      else
                      {
                          entity.addPart(nameValuePairs.get(index).getName(),
                              new FileBody(new File(nameValuePairs.get(index)
                                      .getValue())));
                      }
      
                  }
              }
      
      
              httpPost.setEntity(entity);
              HttpResponse response = httpClient.execute(httpPost, localContext);
              HttpEntity resEntity = response.getEntity();
              if (resEntity != null) 
              {
                  page = EntityUtils.toString(resEntity);
                  System.out.println("PAGE :" + page);
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
          return page;
      }
      

      }

      enter code here
      

      【讨论】:

      • 我想将文本文件、图像、其他附件等文件上传到服务器数据库,而不是消息
      【解决方案3】:

      您可以使用 httpmime 库上传任何文件(图片、音频、视频等) 请参考以下代码。

      enter code here
      
      
          HttpClient httpClient = new DefaultHttpClient();
          HttpContext localContext = new BasicHttpContext();
          HttpPost postRequest = new HttpPost(your url);
          MultipartEntity reqEntity = new MultipartEntity(
                  HttpMultipartMode.BROWSER_COMPATIBLE);
      
          try {
      
      
      
      
              File file = new File(filename);
              FileBody fileBody = new FileBody(file);
              reqEntity.addPart("video_file", fileBody);
      
              postRequest.setEntity(reqEntity);
              HttpResponse response = httpClient.execute(postRequest,
                      localContext);
              HttpEntity entity = response.getEntity();
      
              if (entity != null) {
                  // entity.consumeContent();
                  BufferedReader rd = new BufferedReader(new InputStreamReader(
                          entity.getContent()));
                  // Log.e("response", "response " + rd.toString());
                  String line;
                  String result1 = "";
                  while ((line = rd.readLine()) != null) {
                      result1 = result1 + line;
                      // Log.e("result", "line  " + line);
      
      
      
      
          }
                  // Log.e("result", "is   " + result1);
                  return result1;
              }
          } catch (Exception e) {
              return "Exception";
          }
          return "";
      }
      

      }

      【讨论】:

        【解决方案4】:

        她是上传到服务器程序的文件的下拉框链接 https://www.dropbox.com/s/qasj5o5mu3dq5ya/file_remote_send.zip 你只需更改波纹管代码

        您的服务器的 URL 放置在此处 字符串 upLoadServerUri = “http://example.com/UploadToServer.php&#8221;;

        here upload_file id php 基本文件名 conn.setRequestProperty(“uploaded_file”, 文件名);

        UploadToServer.php

        <?php
        
        $file_path = “uploads/”;
        
        $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
        if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo “success”;
        } else{
        echo “fail”;
        }
        ?>
        

        【讨论】:

        • 什么是upload_file ??我想在这个 php 代码中做任何编辑吗??
        猜你喜欢
        • 2017-10-06
        • 1970-01-01
        • 2016-01-10
        • 2017-03-28
        • 1970-01-01
        • 2010-12-27
        • 2014-01-09
        • 2013-03-17
        • 2013-12-09
        相关资源
        最近更新 更多