从HTTP和FTP上获取单个文件的大小

2 篇文章 0 订阅

问题场景

在维护命令行客户端导入命令时,需要初始化文件大小信息,而通过
URLConnection connection = new URL(this.importUrl).openConnection();
long blobSize = connection.getContentLengthLong();

的方式获取ftp上文件的大小效果行不通,只好通过试探法去FTP上获取。

CODE

private static final String ftp = "FTP";

    /**
     * get size from http or ftp
     * @param url
     * @return
     */
    public static long getSize(String url) {
        URLConnection connection = new URL(url).openConnection();
        connection.setRequestProperty("Accept-Encoding", "identity");
        long blobSize;
        if (StringUtils.startsWith(url.toLowerCase(), "ftp://")) {
            blobSize = getSizeFromFtp(url);
            if (blobSize == -1L) {
                System.out.println("\nThe file does not exist in ftp.");
                System.out.println("url: " + url);
                System.exit(0);
            }
        } else {
            blobSize = connection.getContentLengthLong();
        }
        return blobSize;
    }

    /**
     * get file size from ftp
     * @param url
     * @return
     * @throws IOException
     */
    public static long getSizeFromFtp(String url) throws IOException {
        long res = -1L;
        FTPClient ftpClient = new FTPClient();
        if (login(ftpClient, url)) {
            JSONObject properties = getPropertyFromFtpUrl(url);
            String ftpfilename = (String) properties.get("ftpfilename");
            String charsetName = "GBK";
            // 发送OPTS UTF8指令,尝试支持UTF-8
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {
                charsetName = "UTF-8";
            }
            FTPFile[] files = ftpClient.listFiles(new String(ftpfilename.getBytes(charsetName), "ISO-8859-1"));
            if (files.length == 1) {
                res = files[0].getSize();
            }
        }
        ftpClient.disconnect();
        return res;
    }

    /**
     * ftp://javaa:javaa@172.168.2.212:21/test/javaa.txt
     * ftp://用户名:密码@FTP服务器IP地址或域名:FTP命令端口/路径/文件名
     * @param url   url
     * @return      json object
     */
    public static JSONObject getPropertyFromFtpUrl(String url) {
        JSONObject obj = new JSONObject();
        if (checkFtpUrl(url)) {
            String username = null;
            String password = null;
            String hostname = null;
            String port = "21";
            String ftpfilename = null;

            String userPwdIp = url.substring(6, url.indexOf('/', 6));
            String ipAndPort;
            if (userPwdIp.contains("@")) {
                String userAndPwd = url.substring(6, url.lastIndexOf('@'));
                if (userAndPwd.contains(":")) {
                    username = userAndPwd.split(":")[0];
                    password = userAndPwd.split(":")[1];
                } else {
                    username = "anonymous";
                }

                ipAndPort = url.substring(url.lastIndexOf('@') + 1, url.indexOf('/', 6));
            } else {
                ipAndPort = userPwdIp;
            }

            if (ipAndPort.contains(":")) {
                hostname = ipAndPort.split(":")[0];
                port = ipAndPort.split(":")[1];
            } else {
                hostname = ipAndPort;
            }

            String fileNamePath = url.substring(url.indexOf('/', 6));
            if (fileNamePath.contains("/")) {
                ftpfilename = fileNamePath.substring(fileNamePath.lastIndexOf('/') + 1);
            }

            obj.put("username", username);
            obj.put("password", password);
            obj.put("hostname", hostname);
            obj.put("port", port);
            obj.put("ftpfilename", ftpfilename);
        }
        return obj;
    }

    /**
     * 检验ftp的url合法性
     * @param url   ftp://javaa:javaa@172.168.2.212:21/test/javaa.txt
     * @return      boolean
     */
    public static boolean checkFtpUrl(String url) {

        return url.toLowerCase().startsWith("ftp://");
    }

    /**
    * login for anonymous
    * @param ftpClient     client
    * @param url           url
    * @return              boolean
    * @throws IOException
    */
   public static boolean login(FTPClient ftpClient, String url) throws IOException {

       JSONObject properties = getPropertyFromFtpUrl(url);
       String hostname = (String) properties.get("hostname");
       String port = (String) properties.get("port");
       String username = (String) properties.get("username");
       String password = (String) properties.get("password");

       try {
           ftpClient.connect(hostname, Integer.parseInt(port));
       } catch (IOException e) {
           e.printStackTrace();
       }
       ftpClient.setControlEncoding("UTF-8");
       if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
           if (isAnonymous(username, password)) {
               username = ftp;
               password = ftp;
           }
           if (!ftpClient.login(username, password)) {
               throw new IOException("Wrong username or password!");
           } else {
               return true;
           }
       }
       return false;
   }

   /**
    * is it anonymous?
    * @param username  name
    * @param password  pwd
    * @return          boolean
    */
   public static boolean isAnonymous(String username, String password) {

       return ((username == null || username.equals("anonymous")) && password == null)
               || ((username != null && username.equals(ftp)) && password.equals(ftp));
   }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Java 中,我们可以通过使用 FTP 客户端库来连接到 FTP 服务器并获取全部文件。下面是一个简单的代码示例,演示了如何使用 Apache Commons Net 库来连接到 FTP 服务器并获取所有文件: ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FTPDemo { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { // 连接到FTP服务器 ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 获取服务器上的所有文件 FTPFile[] files = ftpClient.listFiles(); // 下载所有文件 for (FTPFile file : files) { String remoteFilePath = file.getName(); String localFilePath = "C:/downloads/" + remoteFilePath; OutputStream outputStream = new FileOutputStream(localFilePath); InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath); byte[] bytesArray = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(bytesArray)) != -1) { outputStream.write(bytesArray, 0, bytesRead); } boolean success = ftpClient.completePendingCommand(); if (success) { System.out.println("文件 " + remoteFilePath + " 下载成功。"); } inputStream.close(); outputStream.close(); } } catch (IOException ex) { System.out.println("连接到FTP服务器失败: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } } ``` 在上面的代码中,我们首先使用 `FTPClient` 对象连接到 FTP 服务器,然后使用 `listFiles()` 方法获取服务器上的所有文件。接着,我们使用 `retrieveFileStream()` 方法下载每个文件,并将其保存到本地文件系统中。最后,我们使用 `completePendingCommand()` 方法来通知服务器我们已经完成了文件下载。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值