java用jdk1.7写的下载ftp软件,32位系统正常,64报错java.net.ConnectException: Connection timed out: connect

不是书虫的书虫 发布于 2015/08/29 20:16
阅读 1K+
收藏 0

用java jdk1.7版本写了个ftp下载程序,可以再32位上面运行下载文件,但是在64位上面只可以连接成功ftp,但是不能下载文件。在运行到InputStream is = ftpclient.getFileStream(ftpFile);的时候报java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at sun.net.ftp.impl.FtpClient.openPassiveDataConnection(FtpClient.java:669)
        at sun.net.ftp.impl.FtpClient.openDataConnection(FtpClient.java:708)
        at sun.net.ftp.impl.FtpClient.nameList(FtpClient.java:1711)

 程序

 

package com.xjky.dxq.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;

//适用于jdk1.7
public class FtpUtilSeven {
 public FtpClient ftpclient;

 public FtpUtilSeven(String url, int port, String username, String password) {
  // 创建ftp
  try {
   // 创建地址
   SocketAddress addr = new InetSocketAddress(url, port);
   // 连接
   ftpclient = FtpClient.create();
   ftpclient.connect(addr);
   // 登陆
   ftpclient.login(username, password.toCharArray());
   ftpclient.setBinaryType();
   System.out.println(ftpclient.getWelcomeMsg());
  } catch (FtpProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 切换目录
  *
  * @param ftp
  * @param path
  */
 public void changeDirectory(String path) {
  try {
   ftpclient.changeDirectory(path);
  } catch (FtpProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 关闭ftp
  *
  * @param ftp
  */
 public void disconnectFTP() {
  try {
   ftpclient.close();
   System.out.println("disconnect success!!");
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 上传文件
  *
  * @param localFile
  * @param ftpFile
  * @param ftp
  */
 public void upload(String localFile, String ftpFile) {
  OutputStream os = null;
  FileInputStream fis = null;
  try {
   // 将ftp文件加入输出流中。输出到ftp上
   os = ftpclient.putFileStream(ftpFile);
   File file = new File(localFile);
   // 创建一个缓冲区
   fis = new FileInputStream(file);
   byte[] bytes = new byte[1024];
   int c;
   while ((c = fis.read(bytes)) != -1) {
    os.write(bytes, 0, c);
   }
   System.out.println("upload success!!");
  } catch (FtpProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (os != null)
     os.close();
    if (fis != null)
     fis.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 /**
  * 文件下载
  *
  * @param localFile
  * @param ftpFile
  * @param ftp
  */
 public Boolean download(String localFile, String ftpFile) {
  InputStream is = null;
  FileOutputStream fos = null;
  try {
   // 获取ftp上的文件
   is = ftpclient.getFileStream(ftpFile);
   File file = new File(localFile);
   byte[] bytes = new byte[1024];
   int i;
   fos = new FileOutputStream(file);
   while ((i = is.read(bytes)) != -1) {
    fos.write(bytes, 0, i);
   }
   System.out.println(ftpFile + "  download success!!");
   return true;
  } catch (FtpProtocolException e) {
   e.printStackTrace();
   return false;
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  } finally {
   try {
    if (fos != null)
     fos.close();
    if (is != null) {
     is.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

 

加载中
0
zigzagroad
zigzagroad
连接超时。先在运行程序的机器上测试可连接性,然后再运行程序。注意防火墙是否限制访问了。
返回顶部
顶部