java ftp上传工具类 package com.lownsun.utils;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import org.apache.commons
package com.lownsun.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPListParseEngine; import org.apache.commons.net.ftp.FTPReply; import sun.net.ftp.FtpClient; public class FtpUtils { private FTPClient ftpClient = null; //FTP 客户端代理 public boolean ftpConnect(){ boolean flag = true; try { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ftp.properties"); Properties p = new Properties(); p.load(inputStream); String serverIp=p.getProperty("serverIp"); String userName=p.getProperty("userName"); String password=p.getProperty("password"); int port=Integer.parseInt(p.getProperty("port")); if (ftpClient == null) { int reply; ftpClient = new FTPClient(); ftpClient.setControlEncoding("UTF-8"); //文件名乱码,默认ISO8859-1,不支持中文 ftpClient.setDefaultPort(port); ftpClient.connect(serverIp); ftpClient.login(userName, password); reply = ftpClient.getReplyCode(); ftpClient.setDataTimeout(120000); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); flag = false; } } } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } public String[] getServerIp(){ String[] server=new String[4]; try { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ftp.properties"); Properties p = new Properties(); p.load(inputStream); server[0]=p.getProperty("serverIp"); server[1]=p.getProperty("userName"); server[2]=p.getProperty("password"); server[3]=p.getProperty("port"); } catch (Exception e) { e.printStackTrace(); } return server; } public String getIp(){ String ip=""; try { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ftp.properties"); Properties p = new Properties(); p.load(inputStream); ip=p.getProperty("serverIp"); } catch (Exception e) { e.printStackTrace(); } return ip; } public void listRemoteAllFiles(String path) { try { FTPListParseEngine f = ftpClient.initiateListParsing(path); while (f.hasNext()) { FTPFile[] files = f.getNext(5); for(FTPFile file:files){ disFile(file,path); } } } catch (IOException e) { e.printStackTrace(); } } /** * *删除ftp上的文件
* @param srcFname * @return true || false */ public boolean removeFile(String srcFname){ boolean flag = false; if( ftpClient!=null ){ try { flag = ftpClient.deleteFile(srcFname); } catch (IOException e) { e.printStackTrace(); closeConnect(); } } return flag; } public void disFile(FTPFile file,String path){ if(file.isDirectory() && !file.getName().equals(".")&& !file.getName().equals("..")){ System.out.println(File.separator + file.getName()); listRemoteAllFiles(path+ File.separator +file.getName()); }else if(!file.getName().equals(".")&& !file.getName().equals("..")){ System.out.println(file.getName()); } } //关闭连接 public void closeConnect() { try { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } public void downFile(String remotePath,String fileName,String localPath) { FTPFile[] fs; try { ftpClient.changeWorkingDirectory(remotePath);//转移到FTP服务器目录 fs = ftpClient.listFiles(); for(FTPFile ff:fs){ if(ff.getName().equals(fileName)){ File localFile = new File(localPath+ File.separator +ff.getName()); FileOutputStream is = new FileOutputStream(localFile); ftpClient.retrieveFile(ff.getName(), is); is.close(); } } } catch (IOException e) { e.printStackTrace(); } } public void upFile(String path, String filename, String localFilePath){ try { File dir=new File(path); if(!dir.exists()){ ftpClient.makeDirectory(path); } FileInputStream in=new FileInputStream(new File(localFilePath)); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //ftpClient. ftpClient.changeWorkingDirectory(path); ftpClient.storeFile(filename, in); in.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 上传 * @param dirPath 图片在ftp存储地址 * @param fileName 图片名称 * @param dirFile 压缩后原图片 * @param smallFile 缩略图片 */ public boolean upload(String jjdbh,String dirPath,String fileName,File dirFile) throws Exception { boolean uploadSuccess=false;//图片上传失败 try { createDirectory("/",dirPath); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); FileInputStream in=new FileInputStream(dirFile); ftpClient.changeWorkingDirectory(dirPath); ftpClient.enterLocalPassiveMode(); ftpClient.storeFile( jjdbh+"\\"+fileName, in); in.close(); /*判断源文件是否成功上传*/ FTPFile[] dirFE = ftpClient.listFiles(); for(FTPFile ff:dirFE){ if(ff.getName().equals(fileName)){ return true; } } } catch (Exception e) { throw new Exception(e.getMessage()); } return uploadSuccess; } /** * 判断文件是否存在 * @param path * @return * @throws Exception */ public boolean existsFile(String path) throws Exception{ boolean existsFile=false; try { FTPFile[] dirFE = ftpClient.listFiles(path); if(null!=dirFE && dirFE.length>0){ existsFile=true; } } catch (Exception e) { throw new Exception(e.getMessage()); } return existsFile; } public boolean changeDirectory(String olddirPath ,String newDirectory)throws Exception{ boolean changeSuccess=false; try { ftpClient.changeWorkingDirectory(olddirPath); changeSuccess=ftpClient.rename(olddirPath, newDirectory); //System.out.println(changeSuccess); } catch (Exception e) { throw new Exception(e.getMessage()); } return changeSuccess; } public static void main(String[] args) throws Exception { FtpUtils ftp=new FtpUtils(); // File Filed=new File( "F:\\Comic\\2.jpg"); if(ftp.ftpConnect()){ //ftp.upload("/upload/jysg/201401/2014011011291721","333.jpg",Filed,Filed); //ftp.upFile("/upload","333.jpg","F:\\Comic\\2.jpg"); boolean flas= ftp.changeDirectory("/upload/jysg/201403/2014021722562704","/upload/jysg/201403/2014021722562704new"); //boolean flas=ftp.existsFile("/upload/sgz/201402/20140213095259622.jpg"); ftp.closeConnect(); } } public boolean createDirectory(String dirPath, String dirName) { boolean flag = false; try { if (dirPath != null && dirPath.compareTo("") != 0&& ftpClient.changeWorkingDirectory(dirPath)) { if(!StringUtils.isEmpty(dirName)){ String [] subDirNames = dirName.split("/"); int length = subDirNames.length; if(length==1){ ftpClient.makeDirectory(dirName); flag = true; }else if(length>1){ int index = 0; String nowDirName = ""; String nowDirPath = ""; while(indexftp 工具类 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FtpUtil { /** * Description: 向FTP服务器上传文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param basePath FTP服务器基础目录 * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath * @param filename 上传到FTP服务器上的文件名 * @param input 输入流 * @return 成功返回true,否则返回false */ public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port);// 连接FTP服务器 // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 ftp.login(username, password);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } //切换到上传目录 if (!ftp.changeWorkingDirectory(basePath+filePath)) { //如果目录不存在创建目录 String[] dirs = filePath.split("/"); String tempPath = basePath; for (String dir : dirs) { if (null == dir || "".equals(dir)) continue; tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } } } } //设置上传文件的类型为二进制类型 ftp.setFileType(FTP.BINARY_FILE_TYPE); //上传文件 if (!ftp.storeFile(filename, input)) { return result; } input.close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /** * Description: 从FTP服务器下载文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param remotePath FTP服务器上的相对路径 * @param fileName 要下载的文件名 * @param localPath 下载后保存到本地的路径 * @return */ public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 ftp.login(username, password);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } public static void main(String[] args) { try { FileInputStream in=new FileInputStream(new File("D:\\temp\\image\\gaigeming.jpg")); boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } }