欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取文件长度http,ftp

程序员文章站 2024-02-26 11:44:46
...

ftp:

FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(1000);
ftp.setDataTimeout(1000); 
ftp.connect(hostName, port);
ftp.login(userName, password);
ftp.setControlEncoding("gb2312");
FTPFile[] files = ftp.listFiles(realPath);
if(files.length > 0){
                         return files[0].getSize();
}
 

 

 

http:

long len = 0;
HttpURLConnection conn = null;
      try {
        URL u = new URL(path);
        conn = (HttpURLConnection) u.openConnection(); 
        conn.setConnectTimeout(1000);
        conn.connect(); 
        if(conn.getResponseCode() == conn.HTTP_OK)
              len =  conn.getContentLength();
         } catch (Exception e) {
                    len = 0;
                    e.printStackTrace();
          }finally{
                    if(conn != null) conn.disconnect();
           }
return len;