本机用Ser-U建了个ftp服务器端,本机防火墙允许21端口及Ser-U访问 测试程序如下,但下载不正常,如题所述 package ftp; import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.SocketException; import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPFile; public class test { public test() { 
//null 
} public static void main(String[] arg0) { 
FTPClient ftpClient = new FTPClient(); 
String hostName = "127.0.0.1"; 
String userName = "hello"; 
String password = "hello"; 
String remoteDir = "/Region_INFO/txt"; try { 
ftpClient.connect(hostName,21); 
ftpClient.setControlEncoding("GBK"); 
ftpClient.login(userName, password); 
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); FTPFile[] files = ftpClient.listFiles(remoteDir); 
for (int i = 0; i <files.length; i++) { 
String name = files[i].getName(); 
if (!(name.equals(".")) && !(name.equals(".."))){ 
System.out.println("name:" + name); File file = new File("C:\\FTP\\txt.txt"); 
FileOutputStream fos = new FileOutputStream(file); 
ftpClient.retrieveFile(name, fos); System.out.println("down success"); 

} } catch (SocketException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} } 
} 我弄不明白红色部分的name参数,不知什么意思! 如果吧这一句去掉,照样能下载得到一个空的但文件名相同的文件。请各位论坛的前辈指教!

解决方案 »

  1.   

    kingfish前辈,我在 System.out.println("down success"); 之前加上关闭流的语句, 但问题依旧存在,望指教
      

  2.   

    不是什么前辈, 就是老了点而已 :)试试修改这2句看看:String remoteDir = "Region_INFO/txt/"; ftpClient.retrieveFile(remoteDir+name, fos); //需要完整路径; 如果返回false说明路径可能错误
      

  3.   

    lz看看这里的文章:
    http://blog.csdn.net/froole/archive/2008/07/09/2629297.aspx
      

  4.   

    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    import org.apache.commons.net.ftp.FTP;public class FtpTool {
    private FTPClient ftp; private String romateDir = ""; private String userName = ""; private String password = ""; private String host = ""; private String port = "21"; public FtpTool(String url) throws IOException {
    //String url="ftp://user:password@ip:port/ftptest/psd";
    int len = url.indexOf("//");
    String strTemp = url.substring(len + 2);
    len = strTemp.indexOf(":");
    userName = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1); len = strTemp.indexOf("@");
    password = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    host = "";
    len = strTemp.indexOf(":");
    if (len < 0)//没有设置端口
    {
    port = "21";
    len = strTemp.indexOf("/");
    if (len > -1) {
    host = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    } else {
    strTemp = "";
    }
    } else {
    host = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    len = strTemp.indexOf("/");
    if (len > -1) {
    port = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    } else {
    port = "21";
    strTemp = "";
    }
    }
    romateDir = strTemp;
    ftp = new FTPClient();
    ftp.connect(host, FormatStringToInt(port)); } public FtpTool(String host, int port) throws IOException {
    ftp = new FTPClient();
    ftp.connect(host, port);
    } public String login(String username, String password) throws IOException {
    this.ftp.login(username, password);
    return this.ftp.getReplyString();
    } public String login() throws IOException {
    this.ftp.login(userName, password);
    System.out.println("ftp用户: " + userName);
    System.out.println("ftp密码: " + password);
    if (!romateDir.equals(""))
    System.out.println("cd " + romateDir);
    ftp.changeWorkingDirectory(romateDir);
    return this.ftp.getReplyString();
    } public  String upload(String pathname, String filename,String strPk_doc_dir) throws IOException, BusinessException { int reply;
    String m_sfilename = null;
    filename = CheckNullString(filename);
    if (filename.equals(""))
    return null;
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    System.out.println("FTP server refused connection.");
    System.exit(1);
    }
    FileInputStream is = null;
    try {
    File file_in;
    if (pathname.endsWith(File.separator)) {
    file_in = new File(pathname + filename);
    } else {
    file_in = new File(pathname + File.separator + filename);
    }
    if (file_in.length() == 0) {
    System.out.println("上传文件为空!");
    return null;
    }   
        //产生随机数最大到99   
    //     j = (int)(Math.random()*1000); 
        m_sfilename = strPk_doc_dir + ".pdf"; // 生成的文件名
    is = new FileInputStream(file_in);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.storeFile(m_sfilename, is);
    ftp.logout();


    } finally {
    if (is != null) {
    is.close();
    }
    }
    System.out.println("上传文件成功!");
    System.out.println(m_sfilename);
    return m_sfilename;

    }


    public boolean delete(String filename) throws IOException {

    FileInputStream is = null;
    boolean retValue = false;
    try {
    retValue = ftp.deleteFile(filename);
    ftp.logout();
    } finally {
    if (is != null) {
    is.close();
    }
    }
    return retValue;

    } public void close() {
    if (ftp.isConnected()) {
    try {
    ftp.disconnect();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } public static int FormatStringToInt(String p_String) {
    int intRe = 0;
    if (p_String != null) {
    if (!p_String.trim().equals("")) {
    try {
    intRe = Integer.parseInt(p_String);
    } catch (Exception ex) { }
    }
    }
    return intRe;
    } public static String CheckNullString(String p_String) {
    if (p_String == null)
    return "";
    else
    return p_String;
    }/* public boolean downfile(String pathname, String filename) {

    String outputFileName = null;
    boolean retValue = false;
    try {
    FTPFile files[] = ftp.listFiles();
    int reply = ftp.getReplyCode(); ////////////////////////////////////////////////
    if (!FTPReply.isPositiveCompletion(reply)) {
    try {
    throw new Exception("Unable to get list of files to dowload.");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    /////////////////////////////////////////////////////
    if (files.length == 0) {
     System.out.println("No files are available for download.");
    }else {
     for (int i=0; i<files.length; i++) {
     System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());
     outputFileName = pathname + filename + ".pdf";
     //return outputFileName;
     File f = new File(outputFileName);
     
     //////////////////////////////////////////////////////
     retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f));
      if (!retValue) {
      try {
    throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      }
     
    }
    }
     
    /////////////////////////////////////////////////////////////
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return retValue;
    }
    */
    public String downfile(String strLocalPath, String filename)throws IOException, BusinessException {
    String strAbsolutePath = null;
    FileOutputStream is = null;
    boolean success = false;
    try{
    File file_in;
    file_in = new File(strLocalPath + File.separator + filename);
    is = new FileOutputStream(file_in);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    success=ftp.retrieveFile(filename, is);
    if(success == true){
    return strAbsolutePath = strLocalPath + File.separator + filename;
    }else{
    return strAbsolutePath;
    }

    }finally {
    if (is != null) {
    is.close();
    }
    }
    }
    public boolean CheckRemoteFileIsNotExist(String stRemotePath ,String filename){
    boolean NotExist=false;

    return NotExist;
    }
    }
      

  5.   

    本人也做过类似的FTP工具,有源码和jar文件http://jacky68147527.javaeye.com