社区
Java EE
帖子详情
FTPClient.connect()的问题
gemmo
2011-08-22 05:05:14
超级简单。
connect时,我不知道一个FTP server的IP地址,只知道 ftp://aa.bb.com/,如何连接啊。
connect("ftp://aa.bb.com/") connect("aa.bb.com/")都不行
...全文
824
3
打赏
收藏
FTPClient.connect()的问题
超级简单。 connect时,我不知道一个FTP server的IP地址,只知道 ftp://aa.bb.com/,如何连接啊。 connect("ftp://aa.bb.com/") connect("aa.bb.com/")都不行
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
gemmo
2011-08-25
打赏
举报
回复
已经解决了。
gemmo
2011-08-23
打赏
举报
回复
没人回答啊
wula0010
2011-08-22
打赏
举报
回复
connect("aa.bb.com"),这个,需要有dns解析,............
Java的
FTP
任务代码实例
/** 46. *
FTP
登陆 47. * @throws IOException 48. */ 49. public void login() throws Exception { 50.
ftp
Client
= new
FTP
Client
(); 51.
ftp
Client
.configure(get
FTP
Client
Config()); 52.
ftp
Client
.setDefaultPort(port); 53.
ftp
Client
.setControlEncoding("UTF-8"); 54.
ftp
Client
.
connect
(hostname); 55. if (!
ftp
Client
.login(username, password)) { 56. throw new Exception("
FTP
log error");
FileZilla Server端,纯绿色的开源
FTP
服务器
轻便好用的免费
FTP
服务器。 连接: protected boolean login(String server, int port, String user, String password) { for (int i = 0; i < LOGIN_REPEAT_TIMES; i++) { try {
ftp
Client
.set
Connect
Timeout(5000); if (!
ftp
Client
.is
Connect
ed()) {
connect
(server, port); if (!
ftp
Client
.is
Connect
ed()) continue; } if (
ftp
Client
.login(user, password)) { //
ftp
Client
.setFileTransferMode(
FTP
Client
.BINARY_FILE_TYPE);
ftp
Client
.setFileType(
FTP
Client
.BINARY_FILE_TYPE);
ftp
Client
.setDataTimeout(120000);
ftp
Client
.setControlEncoding("GBK"); return true; }
ftp
Client
.logout(); } catch (IOException e) { logError(e); } try { Thread.sleep(100L); } catch (InterruptedException e) { } } dis
connect
(); return false; }
FTP
Client
的jar包
FTP
Client
的jar包
FTP
Client
ftp
Client
= new
FTP
Client
();
ftp
Client
.
connect
("
ftp
.foo.com");
ftp
Client
.login("user01", "pass1234");
ftp
Client
.download("C:\\Temp\\&quo; t;, "README.txt"); // Eventually other operations here ...
ftp
Client
.dis
connect
();
ftp
上传文本、移动文件、删除文件
public class
FTP
Util { private
FTP
Client
ftp
Client
=null; private boolean result = false; private FileInputStream fis; String
ftp
Host = "10.16.111.111"; String port = 21 String
ftp
UserName = "
ftp
user11; String
ftp
Password = "1234561"; /** * 登录服务器 * @param
ftp
Info * @return * @throws IOException */ public
FTP
Client
login() throws IOException {
ftp
Client
= new
FTP
Client
();
ftp
Client
.
connect
(
ftp
Host); boolean login =
ftp
Client
.login(
ftp
UserName,
ftp
Password); int reply =
ftp
Client
.getReplyCode(); if (!
FTP
Reply.isPositiveCompletion(reply)) {
ftp
Client
.dis
connect
(); } if(login){ System.out.println("
ftp
连接成功!"); }else{ System.out.println("
ftp
连接失败!"); } //
ftp
Client
.setControlEncoding("GBK"); return
ftp
Client
; } /** * 字符串作为文件上传指定目录 下 * @param content 源字符串 * @param uploadDir 上传目录 * @param
ftp
FileName 上传文件名称 * @throws Exception */ public void
ftp
UploadByText(String content ,String uploadDir,String
ftp
FileName) throws Exception{ try {
ftp
Client
= this.login(); //创建目录 createDir(
ftp
Client
,uploadDir); // 设置上传目录 这个也应该用配置文件读取
ftp
Client
.changeWorkingDirectory(uploadDir);
ftp
Client
.setBufferSize(1024);
ftp
Client
.setControlEncoding("GBK"); // 设置文件类型(二进制)
ftp
Client
.setFileType(
FTP
Client
.BINARY_FILE_TYPE); String fileName = new String(
ftp
FileName.getBytes("GBK"),"iso-8859-1"); OutputStream os =
ftp
Client
.storeFileStream(fileName); byte[] bytes = content.getBytes(); os.write(bytes); os.flush(); os.close(); } catch (Exception e) {
ftp
Client
.dis
connect
();
ftp
Client
= null; e.printStackTrace(); throw e; }finally{
ftp
Client
.dis
connect
();
ftp
Client
= null; } } /** * 移动文件 * @param
ftp
Info * @return * @throws Exception */ public boolean moveFile(
FTP
Info
ftp
Info)throws Exception { boolean flag = false; try {
ftp
Client
= this.login(); flag = this.moveFile(
ftp
Client
,
ftp
Info.getChangeWorkingDirectoryPath(),
ftp
Info.getFilePath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try {
ftp
Client
.dis
connect
();
ftp
Client
= null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭
FTP
连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 删除文件 * @param
ftp
Info * @return * @throws Exception */ public boolean deleteFile(
FTP
Info
ftp
Info)throws Exception { boolean flag = false; try {
ftp
Client
= this.login(); flag = this.deleteByFolder(
ftp
Client
,
ftp
Info.getChangeWorkingDirectoryPath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try {
ftp
Client
.dis
connect
();
ftp
Client
= null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭
FTP
连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 实现文件的移动,这里做的是一个文件夹下的所有内容移动到新的文件, * 如果要做指定文件移动,加个判断判断文件名 * 如果不需要移动,只是需要文件重命名,可以使用
ftp
.rename(oleName,newName) * @param
ftp
* @param oldPath * @param newPath * @return */ public boolean moveFile(
FTP
Client
ftp
,String oldPath,String newPath){ boolean flag = false; try {
ftp
.changeWorkingDirectory(oldPath);
ftp
.enterLocalPassiveMode(); //获取文件数组
FTP
File[] files =
ftp
.listFiles(); //新文件夹不存在则创建 if(!
ftp
.changeWorkingDirectory(newPath)){
ftp
.makeDirectory(newPath); } //回到原有工作目录
ftp
.changeWorkingDirectory(oldPath); for (
FTP
File file : files) { if(file.isDirectory()) { moveFile(
ftp
,oldPath+file.getName()+"/" ,newPath+file.getName()+"/"); }else{ //转存目录 flag =
ftp
.rename(oldPath+new String(file.getName().getBytes("GBK"),"ISO-8859-1"), newPath+"/"+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } if(flag){ System.out.println(file.getName()+"移动成功"); }else{ System.out.println(file.getName()+"移动失败"); } }
ftp
.removeDirectory(new String(oldPath.getBytes("GBK"),"ISO-8859-1")); } catch (Exception e) { e.printStackTrace(); System.out.println("移动文件失败"); } return flag; } /** * 删除
FTP
上指定文件夹下文件及其子文件方法,添加了对中文目录的支持 * @param
ftp
FTP
Client
对象 * @param
Ftp
Folder 需要删除的文件夹 * @return */ public boolean deleteByFolder(
FTP
Client
ftp
,String
Ftp
Folder){ boolean flag = false; try {
ftp
.changeWorkingDirectory(new String(
Ftp
Folder.getBytes("GBK"),"ISO-8859-1"));
ftp
.enterLocalPassiveMode();
FTP
File[] files =
ftp
.listFiles(); for (
FTP
File file : files) { //判断为文件则删除 if(file.isFile()){
ftp
.deleteFile(
Ftp
Folder+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } //判断是文件夹 if(file.isDirectory()){ String childPath =
Ftp
Folder +file.getName()+ "/"; //递归删除子文件夹 deleteByFolder(
ftp
,childPath); } } //循环完成后删除文件夹 flag =
ftp
.removeDirectory(new String(
Ftp
Folder.getBytes("GBK"),"ISO-8859-1")); if(flag){ System.out.println(
Ftp
Folder+"文件夹删除成功"); }else{ System.out.println(
Ftp
Folder+"文件夹删除成功"); } } catch (Exception e) { e.printStackTrace(); System.out.println("删除失败"); } return flag; } /** * 创建目录 * @param createpath * @param s
ftp
*/ public void createDir(
FTP
Client
ftp
Client
,String createpath) throws Exception { try { if(
ftp
Client
.changeWorkingDirectory(createpath)) { return; } String pathArry[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArry) { if (path.equals("")) { continue; } filePath.append(path + "/"); if(!
ftp
Client
.changeWorkingDirectory(filePath.toString())) {
ftp
Client
.makeDirectory(filePath.toString());
ftp
Client
.changeWorkingDirectory(filePath.toString()); } }
ftp
Client
.changeWorkingDirectory(createpath); }catch (Exception e) { e.printStackTrace(); throw new Exception("创建路径错误:" + createpath); } } }
JAVA程序中
FTP
的连接以及上传文件
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import sun.net.TelnetOutputStream; import sun.net.TelnetInputStream; import sun.net.
ftp
.
Ftp
Client
; public class
ftp
test {
Ftp
Client
ftp
Client
; private String localfilename; private String remotefilename; static String a = new String(); static int b; static String c = new String(); static String d = new String(); static String e = new String(); File aFile = new File("e:\\12\\"); String[] fileList = aFile.list(); public void
connect
Server(String ip, int port,String user, String password,String path) { try {
ftp
Client
= new
Ftp
Client
();
ftp
Client
.openServer(ip,port);
ftp
Client
.login(user, password); System.out.println("login success!"); if (path.length() != 0) {
ftp
Client
.cd(path); }
ftp
Client
.binary(); } catch (IOException ex) { System.out.println("not login"); System.out.println(ex); } } public void close
Connect
() { try {
ftp
Client
.closeServer(); System.out.println("dis
connect
success"); } catch (IOException ex) { System.out.println("not dis
connect
"); System.out.println(ex); } } public void upload() { //this.localfilename = "e://12//bb.txt"; //this.remotefilename = "bb.txt"; for(int i = 0; i
ftp
Client
.put(fileList[i]); java.io.File file_in = new java.io.File("e://12//" + fileList[i]); FileInputStream is = new FileInputStream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } System.out.println("upload success"); is.close(); os.close(); } catch (IOException ex) { System.out.println("not upload"); System.out.println(ex); } } }
Java EE
67,512
社区成员
225,880
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章