广告位 后台主题配置管理 |
广告位 后台主题配置管理 |
本篇文章给大家谈谈ftp服务器迁移,以及将数据从ftp服务器对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
需要你先在云主机里面架设下FTP.然后设置一个目录.并在本地电脑上安装下flashfxp上传工具.利用这个工具就可以把本地数据上传到云主机里面.如果你是上传简单的小文件.也可以在远程连接服务器的时候点选项.里面有个本地资源.详细信息.在里面勾选本地磁盘后再远程.这样即可把本地的磁盘映射到云主机系统中.直接复制粘贴文件即可
C# ftp上传ftp
/// summary
/// 上传文件
/// /summary /
// param name="fileinfo"需要上传的文件/param
/// param name="targetDir"目标路径/param
/// param name="hostname"ftp地址/param /
// param name="username"ftp用户名/param /
// param name="password"ftp密码/param
public static void UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password)
{ //1. check target
string target;
if (targetDir.Trim() == "")
{ return; }
target = Guid.NewGuid().ToString();
//使用临时文件名
string URI = "FTP://" + hostname + "/" + targetDir + "/" + target;
///WebClient webcl = new WebClient();
System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);
//设置FTP命令 设置所要执行的FTP命令,
// = System.Net.WebRequestMethods.;//假设此处为显示指定路径下的文件列表
= System.Net.WebRequestMethods.;
//指定文件传输的数据类型
= true;
= true; //告诉ftp文件大小
= fileinfo.Length;
//缓冲大小设置为2KB
const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead; //打开一个文件流 (System.IO.FileStream) 去读上传的文件
using (FileStream fs = fileinfo.OpenRead())
{
try { //把上传的文件写入流
using (Stream rs = )
{ do
{ //每次读文件流的2KB
dataRead = fs.Read(content, 0, BufferSize); rs.Write(content, 0, dataRead); }
while (!(dataRead BufferSize)); rs.Close(); } }
catch (Exception ex) { } finally { fs.Close(); } }
ftp = null; //设置FTP命令
ftp = GetRequest(URI, username, password);
= System.Net.WebRequestMethods.; //改名
= fileinfo.Name; try { ; }
catch (Exception ex)
{
ftp = GetRequest(URI, username, password); = System.Net.WebRequestMethods.; //删除
; throw ex; } finally
{
//fileinfo.Delete(); } // 可以记录一个日志 "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );
ftp = null;
#region
/***** *FtpWebResponse * ****/ //FtpWebResponse ftpWebResponse = (FtpWebResponse);
#endregion
}
/// summary
/// 下载文件
/// /summary
/// param name="localDir"下载至本地路径/param
/// param name="FtpDir"ftp目标文件路径/param
/// param name="FtpFile"从ftp要下载的文件名/param
/// param name="hostname"ftp地址即IP/param
/// param name="username"ftp用户名/param
/// param name="password"ftp密码/param
public static void DownloadFile(string localDir, string FtpDir, string FtpFile, string hostname, string username, string password)
{
string URI = "FTP://" + hostname + "/" + FtpDir + "/" + FtpFile;
string tmpname = Guid.NewGuid().ToString();
string localfile = localDir + @"\" + tmpname;
System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);
= System.Net.WebRequestMethods.;
= true;
= false;
using (FtpWebResponse response = (FtpWebResponse))
{
using (Stream responseStream = response.GetResponseStream())
{
//loop to read write to file
using (FileStream fs = new FileStream(localfile, FileMode.CreateNew))
{
try
{
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
} while (!(read == 0));
responseStream.Close();
fs.Flush();
fs.Close();
}
catch (Exception)
{
//catch error and delete file only partially downloaded
fs.Close();
//delete target file as it's incomplete
File.Delete(localfile);
throw;
}
}
responseStream.Close();
}
response.Close();
}
try
{
File.Delete(localDir + @"\" + FtpFile);
File.Move(localfile, localDir + @"\" + FtpFile);
ftp = null;
ftp = GetRequest(URI, username, password);
= System.Net.WebRequestMethods.;
;
}
catch (Exception ex)
{
File.Delete(localfile);
throw ex;
}
// 记录日志 "从" + URI.ToString() + "下载到" + localDir + @"\" + FtpFile + "成功." );
ftp = null;
}
/// summary
/// 搜索远程文件
/// /summary
/// param name="targetDir"/param
/// param name="hostname"/param
/// param name="username"/param
/// param name="password"/param
/// param name="SearchPattern"/param
/// returns/returns
public static Liststring ListDirectory(string targetDir, string hostname, string username, string password, string SearchPattern)
{
Liststring result = new Liststring();
try
{
string URI = "FTP://" + hostname + "/" + targetDir + "/" + SearchPattern;
System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);
= System.Net.WebRequestMethods.;
= true;
= true;
string str = GetStringResponse(ftp);
str = str.Replace("\r\n", "\r").TrimEnd('\r');
str = str.Replace("\n", "\r");
if (str != string.Empty)
result.AddRange(str.Split('\r'));
return result;
}
catch { }
return null;
}
private static string GetStringResponse(FtpWebRequest ftp)
{
//Get the result, streaming to a string
string result = "";
using (FtpWebResponse response = (FtpWebResponse))
{
long size = response.ContentLength;
using (Stream datastream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(datastream, System.Text.Encoding.Default))
{
result = sr.ReadToEnd();
sr.Close();
}
datastream.Close();
}
response.Close();
}
return result;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
/// summary
/// 向Ftp服务器上传文件并创建和本地相同的目录结构
/// 遍历目录和子目录的文件
/// /summary
/// param name="file"/param
private void GetFileSystemInfos(FileSystemInfo file)
{
string getDirecName=file.Name;
if (!ftpIsExistsFile(getDirecName, "192.168.0.172", "Anonymous", "") file.Name.Equals(FileName))
{
MakeDir(getDirecName, "192.168.0.172", "Anonymous", "");
}
if (!file.Exists) return;
DirectoryInfo dire = file as DirectoryInfo;
if (dire == null) return;
FileSystemInfo[] files = dire.GetFileSystemInfos();
for (int i = 0; i files.Length; i++)
{
FileInfo fi = files[i] as FileInfo;
if (fi != null)
{
DirectoryInfo DirecObj=fi.Directory;
string DireObjName = DirecObj.Name;
if (FileName.Equals(DireObjName))
{
UploadFile(fi, DireObjName, "192.168.0.172", "Anonymous", "");
}
else
{
Match m = Regex.Match(files[i].FullName, FileName + "+.*" + DireObjName);
//UploadFile(fi, FileName+"/"+DireObjName, "192.168.0.172", "Anonymous", "");
UploadFile(fi, m.ToString(), "192.168.0.172", "Anonymous", "");
}
}
else
{
string[] ArrayStr = files[i].FullName.Split('\\');
string finame=files[i].Name;
Match m=Regex.Match(files[i].FullName,FileName+"+.*"+finame);
//MakeDir(ArrayStr[ArrayStr.Length - 2].ToString() + "/" + finame, "192.168.0.172", "Anonymous", "");
MakeDir(m.ToString(), "192.168.0.172", "Anonymous", "");
GetFileSystemInfos(files[i]);
}
}
}
/// summary
/// 判断ftp服务器上该目录是否存在
/// /summary
/// param name="dirName"/param
/// param name="ftpHostIP"/param
/// param name="username"/param
/// param name="password"/param
/// returns/returns
private bool ftpIsExistsFile(string dirName, string ftpHostIP, string username, string password)
{
bool flag = true;
try
{
string uri = "ftp://" + ftpHostIP + "/" + dirName;
System.Net.FtpWebRequest ftp = GetRequest(uri, username, password);
= WebRequestMethods.;
FtpWebResponse response = (FtpWebResponse);
response.Close();
}
catch (Exception )
{
flag = false;
}
return flag;
}
只能重新上传了。你是想迁移ftp服务器吗?用爱米云共享网盘的,迁移服务器非常方便,功能完全可以代替ftp,比ftp还好用
你有FTPClient就比较好办,假如你的两台FTP服务器分别为fs1和fs2
在本地开发代码思路如下:
通过FTPClient连接上fs1,然后下载(可以循环批量下载)到本地服务器,保存到一个临时目录。
下载完成后,FTPClient断开与fs1的连接,记得必须logout。
本地服务器通过FileInputStream将刚下载到临时目录的文件读进来,得到一个ListFile集合。
通过FTPClient连接上fs2,循环ListFile集合,将文件上传至fs2的特定目录,然后清空临时目录,上传完毕后,断开fs2的连接,同样必须logout。
回答1:可以GHOST,但是驱动必须全部卸载掉 否则会产生硬件冲突 蓝屏,软件可能会出现不能用的情况。
回答2:即使设置成一样,只要你FTP空间里的文件没有备份过来,文件就不会存在了。
回答3:传输在FTP的文件储存在FTP服务器上,也就是你原来的那台服务器上,具体情况请参照FTP服务器架设软件的设定!
ftp服务器迁移的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于将数据从ftp服务器、ftp服务器迁移的信息别忘了在本站进行查找喔。
广告位 后台主题配置管理 |
广告位 后台主题配置管理 |