【问题标题】:Compare the size of a file on the server with the one on the sdcard using http in android在android中使用http比较服务器上文件的大小和sdcard上的文件大小
【发布时间】:2013-01-16 09:37:40
【问题描述】:

我可以使用安卓下载管理器从服务器下载文件并将其保存在 sdcard 中。

现在我需要获取服务器中文件的大小,以与 sdcard 上下载的文件进行比较。我能够获得下载文件的大小,现在我需要帮助来创建一种方法来返回服务器中文件的大小 已经尝试过了,但不起作用..

  public  int getFileSizeAtURL(URL url)
        {


          int filesize = -1;

          try
          {
              URL urls = new URL(file_url);
              HttpURLConnection http = (HttpURLConnection)urls.openConnection();
              http.connect();
              filesize = http.getContentLength();
              http.disconnect();

          }
          catch(Exception e)
          {

          }

          return filesize;

        }

通过使用

           Toast.makeText(getApplicationContext(),getFileSizeAtURL(urls)+" kb", Toast.LENGTH_LONG).show(); 

它给了我初始化值..-1kb 我如何使用这个方法来返回文件大小

                              Solved it !!!!

我通过将此代码放在主要方法上解决了我的问题

   if (android.os.Build.VERSION.SDK_INT > 9) {
                            StrictMode.ThreadPolicy policy = 
                                new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                        } 
                      URL urls = new URL(file_url);
                      HttpURLConnection http = (HttpURLConnection)urls.openConnection();
                      http.connect();
                      int filesize = http.getContentLength();

                      http.disconnect();

它给了我服务器中文件的文件大小,现在我可以与 sdcard 上的文件进行比较

    File file=Environment.getExternalStorageDirectory();
                        String filename = file.getPath() + "/test/testing";
                        File myfile = new File(filename);
                        long size = myfile.length()  

享受吧!!!!

【问题讨论】:

    标签: android filesize


    【解决方案1】:
    final URLConnection connection = url.openConnection();
    final int length = connection.getContentLength();
    

    如上尝试使用URLConnection.getContentLength。如果服务器没有指定长度,那么你就无法知道。

    length 返回服务器文件大小。以便您可以比较两个文件

    【讨论】:

      【解决方案2】:
        if (android.os.Build.VERSION.SDK_INT > 9) {
                              StrictMode.ThreadPolicy policy = 
                                  new StrictMode.ThreadPolicy.Builder().permitAll().build();
                              StrictMode.setThreadPolicy(policy);
                          } 
                        URL urls = new URL(file_url);
                        HttpURLConnection http = (HttpURLConnection)urls.openConnection();
                        http.connect();
                        int filesize = http.getContentLength();
      
                        http.disconnect();
      

      它给了我服务器中文件的文件大小,现在我可以与 sdcard 上的文件进行比较

         File file=Environment.getExternalStorageDirectory();
                          String filename = file.getPath() + "/test/testing";
                          File myfile = new File(filename);
                          long size = myfile.length() 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-25
        • 1970-01-01
        • 1970-01-01
        • 2020-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多