Example #1
0
func Ftp(pathlist []info) {
	//配置FTP的地址.
	con, err := ftp.Dial("127.0.0.1:21")
	if err != nil {
		fmt.Println("连接FTP错误:", err)
		return
	}
	//下面是配置FTP的账户.
	if err := con.Login("root", "123456"); err != nil {
		fmt.Println("登录错误:", err)
	}
	defer con.Logout()

	for _, v := range pathlist {
		File, err := os.Open(v.path)
		if err != nil {
			fmt.Printf("打开文件:%s错误 %s\n", v.path, err)
			continue
		}
		defer File.Close()
		err = con.Stor(v.path, File)
		if err != nil {
			fmt.Println("上传文件出错:", err)
			continue
		}
		fmt.Println(v.path, "上传成功")
	}
}
Example #2
0
// connect - opens a connection to the provided ftp host and then authenticates with the host with the username, password attributes
func (f *FtpWriter) connect(killChan chan error) {
	conn, err := ftp.Dial(f.host)
	if err != nil {
		util.KillPipelineIfErr(err, killChan)
	}

	lerr := conn.Login(f.username, f.password)
	if lerr != nil {
		util.KillPipelineIfErr(lerr, killChan)
	}

	r, w := io.Pipe()

	f.conn = conn
	go f.conn.Stor(f.path, r)
	f.fileWriter = w
	f.authenticated = true
}
func NewFtpDownloader(host string, src string, dest string) FtpDownloader {
	os.MkdirAll(dest, 0777)

	conn, err := ftp.Dial(host + ":21")
	if err != nil {
		panic(err)
	}

	err = conn.Login("anonymous", "")
	if err != nil {
		panic(err)
	}

	err = conn.ChangeDir(src)
	if err != nil {
		panic(err)
	}

	return FtpDownloader{host: host, conn: conn, dest: dest}
}