winform的ftp方式上传
public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
try
{
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
//FileStream fs = fileInf.OpenRead();
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
return 0;
}
catch (Exception ex)
{
reqFTP.Abort();
Logging.WriteError(ex.Message + ex.StackTrace);
return -2;
}
}
//ftp方式下载
public static int DownloadFtp(string filePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FtpWebRequest reqFTP;
try
{
//filePath = < <The full path where the file is to be created.>>,
//fileName = < <Name of the file to be created(Need not be the name of the file on FTP server).>>
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return 0;
}
catch (Exception ex)
{
Logging.WriteError(ex.Message + ex.StackTrace);
return -2;
}
}
转载于:https://www.cnblogs.com/wuzk2008/archive/2009/07/07/1518545.html
winform的ftp方式上传相关推荐
- springboot以FTP方式上传文件到远程服务器
此处远程服务器是ubuntu,关于ftpserver的配置请参考该文https://blog.csdn.net/sunxiaoju/article/details/85224602,在此处就不再赘述. ...
- java通过ftp方式上传_通过FTP以Java方式上传文件
我试图开发一个简单的java代码,它将一些内容从本地机器上传到服务器/另一台机器.我使用下面的代码 import sun.net.ftp.*; import java.io.*; public cla ...
- 项目实战|C#Socket通讯方式改造(一)--Socket实现Ftp的上传和下载
学更好的别人, 做更好的自己. --<微卡智享> 本文长度为1869字,预计阅读5分钟 前言 好久没写过C#的文章了,主要原因最近也很少动C#项目的代码,所以也没什么可写的,最近是一个老项 ...
- php vsftpd文件上传类,php ftp文件上传函数(基础版)
php ftp文件上传函数(基础版) 复制代码 代码如下: // 定义变量 $local_file = 'local.zip'; $server_file = 'server.zip'; // 连接F ...
- linux 关闭开机 ftp,解决linux ftp匿名上传、下载开机自启问题
如果在平时学习,工作中经常使用 ftp 服务器 ,可以设置成开机自启,在设置之前要先了解几个关于自启的命令: 1.chkconfig 命令 主要作用:用于检查,设置系统的各种服务.其中有几个重要参数, ...
- ftp服务器上传文件权限设置,ftp服务器 上传文件权限设置
ftp服务器 上传文件权限设置 内容精选 换一换 华为云对象存储服务帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用对 ...
- java上传ftp_java实现FTP文件上传与文件下载
本文实例为大家分享了两种java实现FTP文件上传下载的方式,供大家参考,具体内容如下 第一种方式: package com.cloudpower.util; import java.io.File; ...
- FTP的上传下载工具类
下载commons-net的最新包,并引入至项目中. 以下为相关例子代码: package com.ffcs.icity.common.util;import java.io.File; import ...
- 用脚本实现FTP的上传和下载
1. FTP的安装 检查 rpm -q vsftpd vsftpd -v 安装 yum -y install vsftpd ft 查看安装位置 [root@ftp ~]# whereis vsftpd ...
最新文章
- Service的基本组成
- webstorm设置注释颜色_简单5步了解相关矩阵的注释热图
- LeetCode 672. 灯泡开关 Ⅱ(枚举)
- 热门搜索怎么实现_三个步骤教你学会,搜索引擎霸屏技术!
- Windows平台下使用Dokan实现文件系统的开发
- mac 图形化安装mysql,mac安装mysql图形化工具?
- 在64位操作系统上使用FlashDevelop的Debug功能
- android 播放器 直播,通过android中的mediaplayer直播
- ubuntu 20.04安装谷歌拼音输入法
- 【毕业设计】python opencv 深度学习 指纹识别算法实现
- 2014年你不用担心的10件事
- ubuntu repo安装方法
- Python爬虫入门教程 93-100 帮粉丝写Python爬虫之【获取CSDN周榜所有大佬的收费专栏】
- 微信小程序把玩(三十)wx.request(object) API
- 阿里Java面试必问:java多线程实例
- Centos系统服务器挂载硬盘(ntfs格式和exfat格式)
- MySql 数据库的创建
- Function中的apply函数的应用
- RFID在工业自动化产线工艺中的应用
- c语言实验报告函数和宏定义,第六章 实验报告(函数与宏定义)