简体   繁体   English

使用Java代码将目录的多媒体文件下载到我的本地计算机中

[英]Downloading multimedia files of a directory into my local machine using java code

I have a java code which i would be using to download the files from a directory in a remote machine.Now is use the ftp class available to do the same.The problem arises when i run the code which results in all the files being downloaded but when i see the size of all of them i see that all are of zero bytes and do not contain anything either.Kindly help if you can to analyze what could be the possible reason behind this? 我有一个Java代码,我将使用该代码从远程计算机上的目录下载文件。现在使用ftp类进行相同的操作。当我运行该代码导致所有文件都被下载时会出现问题但是当我看到它们的大小时,我看到它们都是零字节并且也不包含任何东西。如果您可以分析这背后的可能原因,请帮忙。

package login.multiple;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.*; 
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class Downloader {

    /**
     * @param args
     * @throws FTPException 
     * @throws FTPIllegalReplyException 
     * @throws IOException 
     * @throws IllegalStateException 
     * @throws FTPListParseException 
     * @throws FTPAbortedException 
     * @throws FTPDataTransferException 
     */
    public static void main(String[] args)  {
        // TODO Auto-generated method stub
        FTPFile[] list = null;
        String fileNames[];
        String path = "\\Users\\XXX\\Documents\\Downloads\\Coldplay - Mylo Xyloto [mp3-vbr-2011]";
        int i = 0;
        FileOutputStream fos = null;
        FTPClient client = new FTPClient();
        //client.connect("192.168.1.2");
        try {
            client.connect("127.0.0.1");
            client.login("xxx", "yyy");
            list = client.listFiles(path);
            for(i=0;i<list.length;i++){
            //for(i=0;i<1;i++){
                fos = new FileOutputStream(list[i].getName());
                client.retrieveFile(path + list[i].getName(), fos);
            }
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            client.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }
    }

Since you are opening the FileOutputStream , a good chance is that you are responsible for closing it, too. 由于您正在打开FileOutputStream ,因此很有可能FileOutputStream负责关闭它。 Add fos.close() after client.retrieveFile . client.retrieveFile之后添加fos.close() Your try-catch design is a mess, too. 您的try-catch设计也一团糟。 It will help the diagnostics if you get it right. 如果操作正确,它将有助于诊断。 Put everything after client.connect into a try {...} finally { client.disconnect(); } 最后,将client.connect 之后的所有内容放入try {...} finally { client.disconnect(); } try {...} finally { client.disconnect(); } and declare your main method as throws Exception . try {...} finally { client.disconnect(); }并宣布你的main方法throws Exception That will give you a fail-fast behaviour. 那会给您带来失败的快速反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM