Java FTP工具类持续更新中非原创
1 package com.ftp; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.net.InetSocketAddress; 10 import java.net.SocketAddress; 11 12 import sun.net.ftp.FtpClient; 13 import sun.net.ftp.FtpProtocolException; 14 15 public class FTPUtil { 16 /** 17 * 连接ftp服务器 JDK 1.7 18 * @param url 19 * @param port 20 * @param username 21 * @param password 22 * @return FtpClient 23 * @throws FtpProtocolException 24 * @throws IOException 25 */ 26 public static FtpClient connectFTP(String url,int port,String username,String password) { 27 //创建ftp 28 FtpClient ftp = null; 29 try { 30 //创建地址 31 SocketAddress addr = new InetSocketAddress(url,port); 32 //连接 33 ftp = FtpClient.create(); 34 ftp.connect(addr); 35 //登陆 36 ftp.login(username,password.toCharArray()); 37 ftp.setBinaryType(); 38 System.out.println(ftp.getWelcomeMsg()); 39 } catch (FtpProtocolException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } catch (IOException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } 46 return ftp; 47 } 48 49 /**切换目录 50 * @param ftp 51 * @param path 52 */ 53 public static void changeDirectory(FtpClient ftp,String path) { 54 try { 55 ftp.changeDirectory(path); 56 57 System.out.println(ftp.getWorkingDirectory()); 58 } catch (FtpProtocolException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } catch (IOException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 67 /** 68 * 关闭ftp 69 * @param ftp 70 */ 71 public static void disconnectFTP(FtpClient ftp){ 72 try { 73 ftp.close(); 74 System.out.println("disconnect success!!"); 75 } catch (IOException e) { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 } 79 } 80 81 /** 82 * 上传文件 83 * @param localFile 84 * @param ftpFile 85 * @param ftp 86 */ 87 public static void upload(String localFile,String ftpFile,FtpClient ftp){ 88 OutputStream os = null; 89 FileInputStream fis = null; 90 try { 91 //将ftp文件加入输出流中。输出到ftp上 92 os = ftp.putFileStream(ftpFile); 93 File file = new File(localFile); 94 //创建一个缓冲区 95 fis = new FileInputStream(file); 96 byte[] bytes = new byte[1024]; 97 int c; 98 while((c = fis.read(bytes))!=-1){ 99 os.write(bytes,0,c); 100 } 101 System.out.println("upload success!!"); 102 } catch (FtpProtocolException e) { 103 // TODO Auto-generated catch block 104 e.printStackTrace(); 105 } catch (IOException e) { 106 // TODO Auto-generated catch block 107 e.printStackTrace(); 108 }finally{ 109 try { 110 if(os!=null) 111 os.close(); 112 if(fis!=null) 113 fis.close(); 114 } catch (IOException e) { 115 // TODO Auto-generated catch block 116 e.printStackTrace(); 117 } 118 } 119 120 121 } 122 123 /** 124 * 文件下载 125 * @param localFile 126 * @param ftpFile 127 * @param ftp 128 */ 129 public static void download(String localFile,String ftpFile,FtpClient ftp){ 130 InputStream is = null; 131 FileOutputStream fos = null; 132 try { 133 //获取ftp上的文件 134 is = ftp.getFileStream(ftpFile); 135 File file = new File(localFile); 136 byte[] bytes = new byte[1024]; 137 int i; 138 fos = new FileOutputStream(file); 139 while((i = is.read(bytes))!=-1){ 140 fos.write(bytes, 0, i); 141 } 142 System.out.println("download success!!"); 143 144 } catch (FtpProtocolException e) { 145 // TODO Auto-generated catch block 146 e.printStackTrace(); 147 } catch (IOException e) { 148 // TODO Auto-generated catch block 149 e.printStackTrace(); 150 }finally{ 151 try { 152 if(fos!=null) 153 fos.close(); 154 if(is!=null){ 155 is.close(); 156 } 157 } catch (IOException e) { 158 // TODO Auto-generated catch block 159 e.printStackTrace(); 160 } 161 } 162 163 } 164 165 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。