烦请各位高手帮看看代码写的对否?
下面的代码是想将本地电脑上生成的.xml文档上传至远程的服务器指定文件夹去,以前从未做过。对方告知他的远程服务器IP以及指定的文件夹,要求我把生成的test.xml文档上传过去。我生成的test.xml文档就存放在我的应用程序当前目录下面的xml文件夹中。不知道我写的文件路径以及文件名格式是否正确,代码是否有效,再就是想问一下是否只有相同网段的两台电脑才能上传文件呢?比如我的电脑IP是:192.168.1.100,是否就无法上传?谢谢指教!string uriString = "http://100.110.120.155/AVS_FilePort/Input";
string fileName = "test.xml";
if (uriString.EndsWith("/") == false) uriString = uriString + "/";
uriString = uriString + fileName;
string fileNamePath = Application.StartupPath.ToString() + @"\xml\" + fileName;
// 创建WebClient实例
WebClient myWebClient = new WebClient();
// 访问权限设置
myWebClient.Credentials = CredentialCache.DefaultCredentials;
try
{
myWebClient.UploadFile(uriString,"PUT",fileNamePath);
MessageBox.Show("上传成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} --------------------编程问答--------------------
没发现有问题!
自己不能调试? --------------------编程问答-------------------- 对方说服务器及文件夹不能上传非生产实际使用的数据文件,就是说用一些假数据来上传调试不允许,所以要求程序一定要调试正确后,直接上传真实的生产数据文件。 --------------------编程问答--------------------
还有这样的。。谁能保证不会出任何问题呢。。 --------------------编程问答-------------------- 那你们公司的这个系统就没有自己的测试环境?
没有的话,只能跟客户协商,说部署的时候可能会出现什么问题,然后进行部署。。
--------------------编程问答-------------------- 再问一下:
对方告诉我的远程服务器及文件夹为:\\100.110.120.155\AVS_FilePort\Input 这样对吗?
在程序中写:
string uriString = @"\\100.110.120.155\\AVS_FilePort\Input"; //前面要加上@
还是写:
string uriString = "http://100.110.120.155/AVS_FilePort/Input";
那后面的语句:
if (uriString.EndsWith("/") == false) uriString = uriString + "/";
会不会出问题?
--------------------编程问答-------------------- 有知道的吗? --------------------编程问答-------------------- 到底用http协议、ftp协议还是windows file share传输?
你的代码99%不可用。 --------------------编程问答--------------------
//要上传的文件,远程FTP的IP,远程目录,用户名,密码
public void Upload(string filename, string ftpServerIP, string dir, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + dir + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 根据uri创建FtpWebRequest对象
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
// MessageBox.Show("短信发送成功!!!");
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message, "Upload Error");
}
}
private void FTP_Click(object sender, EventArgs e)
{
Upload(Application.StartupPath + @"\xml\test.xml", "100.110.120.155", "/AVS_FilePort/Input/", "user", "password");
}
补充:.NET技术 , C#