java ftp 0kb_ftp上传类 上传的文件都是0kb 请高手指点 代码如下

using System;

using System.Configuration;

using System.IO;

using FtpSupport;

using Microsoft.Win32;

using System.Web;

public class FtpClass

{

private string FtpIP=ConfigurationSettings.AppSettings["FtpIP"];

private string FtpUserName=ConfigurationSettings.AppSettings["FtpUserName"];

private string FtpPassord=ConfigurationSettings.AppSettings["FtpPassWord"];

private FtpConnection ftp;

private FtpConnection FtpConn()

{

ftp=new FtpConnection();

ftp.Connect(this.FtpIP,this.FtpUserName,this.FtpPassord);

return ftp;

}

private string GetFileExtName(string filename,bool withdot)

{

string [] arrs=filename.Split('.');

int i=arrs.Length;

return withdot?"."+arrs[i-1].ToString():arrs[i-1].ToString();

}

private string GetFileContentType(string filedownloadname)

{

string DEFAULT_CONTENT_TYPE = "application/unknown";

RegistryKey regkey,fileextkey;

string FileContentType;

try

{

regkey=Registry.ClassesRoot;

fileextkey=regkey.OpenSubKey(this.GetFileExtName(filedownloadname,false));

FileContentType=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();

}

catch

{

FileContentType=DEFAULT_CONTENT_TYPE;

}

return FileContentType;

}

public string FtpUpload(HttpPostedFile file,string dir,string fileName)

{

string FileDownloadName=fileName;

FtpConnection ftp=this.FtpConn();

if(ftp.DirectoryExist(dir))

ftp.SetCurrentDirectory(dir);

else

{

ftp.CreateDirectory(dir);

ftp.SetCurrentDirectory(dir);

}

ftp.PutStream(file.InputStream,FileDownloadName);

ftp.Close();

return FileDownloadName;

}

public void FileDel(string filedownloadname,string dir)

{

FtpConnection ftp=this.FtpConn();

if(ftp.DirectoryExist(dir))

{

ftp.SetCurrentDirectory(dir);

if(ftp.FileExist(filedownloadname))

{

ftp.DeleteFile(filedownloadname);

}

}

ftp.Close();

}

public void FtpDownload(HttpContext context,string filedownloadname,string dir)

{

context.Response.Clear();

context.Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode(filedownloadname, System.Text.Encoding.UTF8));

context.Response.ContentType=this.GetFileContentType(filedownloadname);

FtpConnection ftp=this.FtpConn();

ftp.SetCurrentDirectory(dir);

if(ftp.FileExist(filedownloadname))

{

FtpStream ftpfs=ftp.OpenFile(filedownloadname,GenericRights.Read);

byte [] buffer=new byte[10240];

int n=ftpfs.Read(buffer,0,buffer.Length);

while(n>0)

{

context.Response.BinaryWrite(buffer);

n=ftpfs.Read(buffer,0,buffer.Length);

}

context.Response.End();

ftpfs.Close();

}

else

{

context.Response.Write("");

}

ftp.Close();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值