sun.net.ftp.FtpClient 上传文件和文件夹应用(改变相关文件中的内容)

package com.teenysoft.usermanager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
* Class    FtpFileUpload.java
* Function 使用ftp上传文件、目录
*/

public class FtpFileUpload {
 private FtpClient ftpClient=null;
 public FtpFileUpload(){
  ftpClient=new FtpClient();
  ftpClient.setConnectTimeout(5000);
 }
 public void connect(String host,int port,String user,String password)throws IOException{
  ftpClient.openServer(host,port);
  ftpClient.login(user,password);
  ftpClient.binary();
 }
 public void cd(String relativePath)throws IOException{
  ftpClient.cd(relativePath);
 }
 public void uploadDirectory(String directory,boolean bl,String nameFtp,String n,String serverIp,String username,String password)throws IOException{
  File file=new File(directory);
  String name=null;//待上传文件名
  if(file.isDirectory()){//如果为目录,则按目录传
   File[] files=file.listFiles();  
   //在ftp服务器上创建对应目录
   ftpClient.ascii();
   String dir = file.getName();
   if(bl){
    dir = nameFtp;
    bl = false;
   }
   ftpClient.sendServer("XMKD " + dir + "\r\n");
   ftpClient.readServerResponse();
   ftpClient.cd(dir);
   ftpClient.binary();
   //循环传递目录下的所有文件与目录
   int i=0;
   for(i=0;i<files.length;i++){
    File tmpFile=files[i];
       if(tmpFile.isDirectory()){
        ftpClient.cd("..");
        ftpClient.cd(dir);
        uploadDirectory(tmpFile.getAbsolutePath(),bl,nameFtp, n, serverIp, username, password);
       }else{
        ftpClient.cd("..");
     ftpClient.cd(dir);
     name=tmpFile.getName();
     upload(directory+"/"+name, n, serverIp, username, password);
       }
      }
       ftpClient.cd("..");
   }
     else //如果为文件,则按文件上传
      upload(directory,file.getName(), n, serverIp, username, password);
 }
 public void upload(String srcFile,String n,String serverIp,String username,String password)throws IOException{
  File file=new File(srcFile);
  FileInputStream fin=new FileInputStream(srcFile);
  TelnetOutputStream tos = ftpClient.put(file.getName());
  if(file.getName().equals("spring.xml")){
   InputStreamReader ie=new InputStreamReader(fin, "utf-8");
   BufferedReader reader = new BufferedReader(ie);
   String line = null;
   while ((line = reader.readLine()) != null) {
    if(line.indexOf("serverIp")!=-1){
     line=line.replaceAll("serverIp", serverIp);
     line=line.replaceAll("dataName", n);
    }
    if(line.indexOf("datausername")!=-1){
     line=line.replaceAll("datausername", username);
    }
    if(line.indexOf("datapassword")!=-1){
     line=line.replaceAll("datapassword", password);
    }
    tos.write(line.getBytes("utf-8"));
    System.out.println("----------------1--------");
   }
   System.out.println("----------------2--------");
   reader.close();
   ie.close();
  }else{
   int readLength = 0;
   byte[] buf = new byte[1024];
   while ( (readLength = fin.read(buf)) != -1) {
       tos.write(buf, 0, readLength);
   }
  }
  fin.close();
  tos.close();
 }
 public void upload(String srcFile,String destFile,String n,String serverIp,String username,String password)throws IOException{
  upload(srcFile, n, serverIp, username, password);
  File file=new File(srcFile);
  ftpClient.rename(file.getName(), destFile);
 }
 public void close(){
  try {
   ftpClient.closeServer();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {
      FtpFileUpload ftpUpload=new FtpFileUpload();
      try {
       ftpUpload.connect("192.168.0.253",21,"wgr","123");
       ftpUpload.uploadDirectory("E:/ztTest/appTest/t8",true,"wgr","wgr","192.168.0.253","sa","");
       System.out.println("ok");
       ftpUpload.close();
      } catch (IOException e) {
       e.printStackTrace();
      }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wgr1001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值