zoukankan      html  css  js  c++  java
  • 文件上传到ftp服务工具类


    直接引用此java工具类就好
    3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 11 import org.apache.commons.net.ftp.FTP; 12 import org.apache.commons.net.ftp.FTPClient; 13 import org.apache.commons.net.ftp.FTPFile; 14 import org.apache.commons.net.ftp.FTPReply; 15 16 /** 17 * ftp上传下载工具类 18 * <p>Title: FtpUtil</p> 19 * <p>Description: </p> 20 * <p>Company: www.itcast.com</p> 21 * @author 入云龙 22 * @date 2015年7月29日下午8:11:51 23 * @version 1.0 24 */ 25 public class FtpUtil { 26 27 /** 28 * Description: 向FTP服务器上传文件 29 * @param host FTP服务器hostname 30 * @param port FTP服务器端口 31 * @param username FTP登录账号 32 * @param password FTP登录密码 33 * @param basePath FTP服务器基础目录 34 * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath 35 * @param filename 上传到FTP服务器上的文件名 36 * @param input 输入流 37 * @return 成功返回true,否则返回false 38 */ 39 public static boolean uploadFile(String host, int port, String username, String password, String basePath, 40 String filePath, String filename, InputStream input) { 41 boolean result = false; 42 FTPClient ftp = new FTPClient(); 43 try { 44 int reply; 45 ftp.connect(host, port);// 连接FTP服务器 46 // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 47 ftp.login(username, password);// 登录 48 reply = ftp.getReplyCode(); 49 if (!FTPReply.isPositiveCompletion(reply)) { 50 ftp.disconnect(); 51 return result; 52 } 53 //切换到上传目录 54 if (!ftp.changeWorkingDirectory(basePath+filePath)) { 55 //如果目录不存在创建目录 56 String[] dirs = filePath.split("/"); 57 String tempPath = basePath; 58 for (String dir : dirs) { 59 if (null == dir || "".equals(dir)) continue; 60 tempPath += "/" + dir; 61 if (!ftp.changeWorkingDirectory(tempPath)) { 62 if (!ftp.makeDirectory(tempPath)) { 63 return result; 64 } else { 65 ftp.changeWorkingDirectory(tempPath); 66 } 67 } 68 } 69 } 70 //设置上传文件的类型为二进制类型 71 ftp.setFileType(FTP.BINARY_FILE_TYPE); 72 //上传文件 73 if (!ftp.storeFile(filename, input)) { 74 return result; 75 } 76 input.close(); 77 ftp.logout(); 78 result = true; 79 } catch (IOException e) { 80 e.printStackTrace(); 81 } finally { 82 if (ftp.isConnected()) { 83 try { 84 ftp.disconnect(); 85 } catch (IOException ioe) { 86 } 87 } 88 } 89 return result; 90 } 91 92 /** 93 * Description: 从FTP服务器下载文件 94 * @param host FTP服务器hostname 95 * @param port FTP服务器端口 96 * @param username FTP登录账号 97 * @param password FTP登录密码 98 * @param remotePath FTP服务器上的相对路径 99 * @param fileName 要下载的文件名 100 * @param localPath 下载后保存到本地的路径 101 * @return 102 */ 103 public static boolean downloadFile(String host, int port, String username, String password, String remotePath, 104 String fileName, String localPath) { 105 boolean result = false; 106 FTPClient ftp = new FTPClient(); 107 try { 108 int reply; 109 ftp.connect(host, port); 110 // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 111 ftp.login(username, password);// 登录 112 reply = ftp.getReplyCode(); 113 if (!FTPReply.isPositiveCompletion(reply)) { 114 ftp.disconnect(); 115 return result; 116 } 117 ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 118 FTPFile[] fs = ftp.listFiles(); 119 for (FTPFile ff : fs) { 120 if (ff.getName().equals(fileName)) { 121 File localFile = new File(localPath + "/" + ff.getName()); 122 123 OutputStream is = new FileOutputStream(localFile); 124 ftp.retrieveFile(ff.getName(), is); 125 is.close(); 126 } 127 } 128 129 ftp.logout(); 130 result = true; 131 } catch (IOException e) { 132 e.printStackTrace(); 133 } finally { 134 if (ftp.isConnected()) { 135 try { 136 ftp.disconnect(); 137 } catch (IOException ioe) { 138 } 139 } 140 } 141 return result; 142 } 143 144 public static void main(String[] args) { 145 try { 146 FileInputStream in=new FileInputStream(new File("D:\temp\image\gaigeming.jpg")); 147 boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in); 148 System.out.println(flag); 149 } catch (FileNotFoundException e) { 150 e.printStackTrace(); 151 } 152 } 153 }

    这个工具类实现了上传和下载

  • 相关阅读:
    遗弃.Forsaken.2015.BluRay.720p.x264.DTS-beAst
    三体
    Hexo博客maupassant主题添加Google Adsense广告
    拼凑自定义控件
    Redis 的安装和使用
    ES6中的数组
    机器学习 —— 数据预处理
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-file
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-home
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-trash
  • 原文地址:https://www.cnblogs.com/xing-12/p/6825376.html
Copyright © 2011-2022 走看看