随笔-71  评论-5  文章-0  trackbacks-0
 /**
 * Description: 向FTP服务器上传文件
 * @Version      1.0
 * @param url FTP服务器hostname
 * @param port  FTP服务器端口
 * @param username FTP登录账号
 * @param password  FTP登录密码
 * @param path  FTP服务器保存目录
 * @param filename  上传到FTP服务器上的文件名
 * @param input  输入流
 * @return 成功返回true,否则返回false *
 */
public static boolean uploadFile(String url,int port,String username,String password,String path,String filename,InputStream input){
 boolean success = false;
 FTPClient ftp = new FTPClient();
 ftp.setControlEncoding("GBK");
 try {
  int reply;
  ftp.connect(url,port);// 连接FTP服务器
  // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
  ftp.login(username, password);// 登录
  reply = ftp.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
   ftp.disconnect();
   return success;
  }
  ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  ftp.makeDirectory(path);
  ftp.changeWorkingDirectory(path);
  ftp.storeFile(filename, input);
  input.close();
  ftp.logout();
  success = true;
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  if (ftp.isConnected()) {
   try {
    ftp.disconnect();
   } catch (IOException ioe) {
   }
  }
 }
 return success;
}
/**
 * 将本地文件上传到FTP服务器上 *
 * 
 *    int port,// FTP服务器端口
  String username, // FTP登录账号
  String password, // FTP登录密码
  String path, // FTP服务器保存目录
  String filename, // 上传到FTP服务器上的文件名
  String orginfilename // 输入流文件名
  
 */
public static void upLoadFromProduction(String url,int port,String username,String password,String path,String filename,String orginfilename) {
 try {
  FileInputStream in = new FileInputStream(new File(orginfilename));
  boolean flag = uploadFile(url, port,username, password, path,filename, in);
  System.out.println(flag);
 } catch (Exception e) {
  e.printStackTrace();
 }
}
 
 
public static void main(String[] args) {
 
upLoadFromProduction("127.40.1.80",21,"www", "huahua", "/ali/www/Cash/photo", "123.jpg", "C:/Users/Administrator/Desktop/11.jpg");
}
 






/**
 * Description: 向FTP服务器上传文件
 * @Version      1.0
 * @param url FTP服务器hostname
 * @param port  FTP服务器端口
 * @param username FTP登录账号
 * @param password  FTP登录密码
 * @param path  FTP服务器保存目录
 * @param filename  上传到FTP服务器上的文件名
 * @param input  输入流
 * @return 成功返回true,否则返回false *
 */
public static boolean uploadFile(String url,int port,String username,String password,String path,String filename,String customerNo,InputStream input){
 boolean success = false;
 FTPClient ftp = new FTPClient();
 ftp.setControlEncoding("GBK");
 try {
  int reply;
  ftp.connect(url,port);// 连接FTP服务器
  // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
  ftp.login(username, password);// 登录
  reply = ftp.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
   ftp.disconnect();
   return success;
  }
//读取所有文件,根据文件名模糊删除远程照片
 FTPFile[] ftpFiles = null;
 ftpFiles = ftp.listFiles(path);
 for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
  FTPFile file = ftpFiles[i];
  if (file.isFile()) {
  if(file.getName().startsWith(customerNo)|| file.getName().endsWith(customerNo)){
  ftp.deleteFile(path+"/"+file.getName());
  }
  
  }
 }
  ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  ftp.makeDirectory(path);
  ftp.changeWorkingDirectory(path);
  ftp.storeFile(filename, input);
  input.close();
  ftp.logout();
  success = true;
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  if (ftp.isConnected()) {
   try {
    ftp.disconnect();
   } catch (IOException ioe) {
   }
  }
 }
 return success;
}
posted on 2015-03-12 18:13 藤本蔷薇 阅读(137) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: