-2

私はJavaが初めてです.. URLからファイルサイズを取得しようとしましたが、失敗しました.URLサイズをバイト単位で取得する方法を教えてください。

4

3 に答える 3

2
    File file =new File("xxxxx"); // file path

    if(file.exists()){
        double bytes = file.length();
        double kilobytes = (bytes / 1024);
        System.out.println("bytes : " + bytes);
        double megabytes = (kilobytes / 1024);
        System.out.println("kilobytes : " + kilobytes);
        double gigabytes = (megabytes / 1024);
        System.out.println("megabytes : " + megabytes);
        double terabytes = (gigabytes / 1024);
        System.out.println("gigabytes : " + gigabytes);
        double petabytes = (terabytes / 1024);
    }else{
         System.out.println("File does not exists!");
    }
于 2014-03-16T05:33:18.773 に答える
2

から取得されるのgetContentLengthメソッドを使用します。URLConnectionURL#openConnection

于 2014-03-16T05:41:28.300 に答える
0
public int getFileSizeInBytes(String path){
try {
        URL url=new URL(path);
        URLConnection urlConnection=url.openConnection();
        urlConnection.connect();
        int file_size=urlConnection.getContentLength();
        return  file_size;

    } catch (MalformedURLException e) {

    }catch (IOException e){

    }
    return -1;
}
于 2016-06-17T16:19:56.897 に答える