目前正着急,做一个程序。需要把一个文件传输到网站的一个目录下。打算用ftp方式或其它方式,请问有没有做过的朋友。告诉我一下方法就可以了。
解决方案 »
- win32是什么意思 ,和windows操作系统是什么关系
- 自定义消息和图片框
- 关于画弧算法的问题,请进!~
- 在CRecordset的MFC编程中,用this->Add()函数时,有没有必要调用this->open()和this->close()函数?
- 如何改变系统处理WM_MOUSEMOVE的时间
- 怎样在2000下屏蔽alt+ctrl+del
- 谁有定时执行的类,例如每星期,每天等的固定时间?
- 关于在状态栏上显示Tooltip的问题:
- IOCP + KeepLive无法检测非优雅断开
- 加 CALLBACK 与不加CALLBACK 的函数在调用以及返回上有什么区别吗?
- 原来我什么奖章都没有.....
- 关于多文件的问题
BOOL CFTPDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
//打开FTP连接
m_pInetSession=new CInternetSession(AfxGetAppName(),
1,PRE_CONFIG_INTERNET_ACCESS);
UpdateData();
try
{
m_pFtpConnection=m_pInetSession->GetFtpConnection(m_FtpAddr,"home","111");
}
catch (CInternetException *pEx)
{
TCHAR szError[1024];
if (pEx->GetErrorMessage(szError,1024))
{
AfxMessageBox(szError);
}
else
{
AfxMessageBox("Exception");
}
pEx->Delete();
m_pFtpConnection=NULL;
return false;
}
//初始化FTP服务器上的文件列表
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
void CFTPDlg::OnBnClickedAddfile()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog fldlg(true);
fldlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
if(IDOK==fldlg.DoModal())
{
m_filepath=fldlg.GetPathName();
m_fileName=fldlg.GetFileName();
//AfxMessageBox(m_filepath);
UpdateData(false);
}
}
//extern CInternetException m
void CFTPDlg::OnBnClickedUp()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
/*CFTPApp *app;
app=(CFTPApp*)AfxGetApp();
*/
if (m_filepath=="")
{
AfxMessageBox("请输入文件路径!");
return;
}
if(0==m_pFtpConnection->PutFile(m_filepath,m_fileName))
{
AfxMessageBox("上传文件失败!");
return;
}
else
{
m_filepath="";
UpdateData(false);
AfxMessageBox("上传文件成功!");
}
}
DWORD WINAPI CFTPDlg::DownFile(LPVOID lpParameter)
{
//delete lpParameter;
CFtpConnection *m_pFtpConnection=((strThread*)lpParameter)->m_pFtpConnection;
CString str=((strThread*)lpParameter)->path;
delete lpParameter;
if(m_pFtpConnection==NULL)
{
AfxMessageBox("请先与FTP服务器建立连接");
return 0;
}
//CString str= m_ftpFile.GetItemText(m_ftpFile.GetSelectedItem());
// str="\\"+str;
/*CFtpFileFind find(m_pFtpConnection);
if(!find.FindFile(str))
{
AfxMessageBox("此文件不存在!");
return;
}*/
if (str=="")
{
AfxMessageBox("请选择文件!");
return 0;
}
int x = -1;
CString strName;
strName=str;
strName=strName.TrimLeft("/");
x=strName.Find("/");
while (-1!=x)
{
strName=strName.Right(strName.GetLength()-x-1);
x=strName.Find("x");
}
CFileDialog fl(true,NULL,strName);
CString strSave;
fl.m_ofn.lpstrFilter="All Files(*.*)\0*.*\0\0";
//fl.m_ofn.lpstrDefExt=strName;
if(IDOK==fl.DoModal())
{
strSave=fl.GetPathName();
}
else
return 0;
if (0==m_pFtpConnection->GetFile(str,strSave,false))
{
//GetLastError();
AfxMessageBox("下载失败");
return 0;
}
else
{
AfxMessageBox("下在完成!\n文件的存放位置为:\n" + strSave);
}
return 1;
}
void CFTPDlg::OnBnClickedDown()
{
//启动一个下载线程
strThread *pa=new strThread;
pa->m_pFtpConnection=m_pFtpConnection;
pa->path=m_ftpFile.GetItemText(m_ftpFile.GetSelectedItem());
HANDLE hThread;
hThread=CreateThread(NULL,0,DownFile,(LPVOID)pa,0,NULL);
CloseHandle(hThread);
// TODO: 在此添加控件通知处理程序代码
}
BOOL CFTPDlg::DestroyWindow()
{
// TODO: 在此添加专用代码和/或调用基类
if (m_pFtpConnection!=NULL)
{
m_pFtpConnection->Close();
delete m_pFtpConnection;
}
delete m_pInetSession;
return CDialog::DestroyWindow();
}
void CFTPDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
if(m_pFtpConnection==NULL)
{
AfxMessageBox("请先与FTP服务器建立连接");
return;
}
m_ftpFile.DeleteAllItems();
UpdateData(false);
findflie("");
}
void CFTPDlg::findflie(CString filepath)
{
static CFtpFileFind finder(m_pFtpConnection);
// start looping
CString str;
str=filepath+"/";
BOOL bWorking = finder.FindFile(str);
while (bWorking)
{
//AfxMessageBox(finder.GetFilePath());
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
if (finder.IsDirectory())
{
if (!finder.IsDots())
{
findflie(finder.GetFilePath());
}
}
else
{
//AfxMessageBox(finder.GetFilePath());
CString strPath;
CString strTitle;
//m_ftpFile.DeleteAllItems();
strPath=finder.GetFilePath();
strTitle=finder.GetFileName();
/*getDirtory(strPath.TrimLeft("/"));
SetDir();*/
//strPath=strPath.TrimLeft("/");
m_ftpFile.InsertItem(strPath,NULL,NULL);
}
}
//finder.Close();
}
void CFTPDlg::getDirtory(CString filename)
{
strArray.RemoveAll();
CString temp;
int xPos=-1;
if (""==filename)
{
AfxMessageBox("文件位置不能为空!");
return ;
}
xPos=filename.Find("/");
while(-1!=xPos)
{
temp=filename.Left(xPos);
strArray.Add(temp);
temp.Empty();
filename=filename.Right(filename.GetLength()-xPos-1);
xPos=-1;
xPos=filename.Find("/");
}
strArray.Add(filename);
}
void CFTPDlg::SetDir(void)
{
//m_ftpFile.
/*HTREEITEM hItem,hTemp;
hItem=m_ftpFile.InsertItem(strArray.GetAt(0),NULL,NULL);
if(m_ftpFile.GetItem(hTemp))
{
if (hTemp==hItem)
{
hTemp=hItem;
m_ftpFile.DeleteItem(hItem)
}
}
for(int i=0;i<m_ftpFile.GetCount();i++)
{
m_ftpFile.GetNextItem()
}
if (pmyTreeCtrl->ItemHasChildren(hmyItem))
{
HTREEITEM hNextItem;
HTREEITEM hChildItem = pmyTreeCtrl->GetChildItem(hmyItem);
while (hChildItem != NULL)
{
hNextItem = pmyTreeCtrl->GetNextItem(hChildItem, TVGN_NEXT);
pmyTreeCtrl->DeleteItem(hChildItem);
hChildItem = hNextItem;
}
}*/
}
void CFTPDlg::OnBnClickedButton2()
{
//打开FTP连接
m_pInetSession=new CInternetSession(AfxGetAppName(),
1,PRE_CONFIG_INTERNET_ACCESS);
UpdateData();
try
{
m_pFtpConnection=m_pInetSession->GetFtpConnection(m_FtpAddr,"home\\weiliang","!dingdong0103");
}
catch (CInternetException *pEx)
{
TCHAR szError[1024];
if (pEx->GetErrorMessage(szError,1024))
{
AfxMessageBox(szError);
}
else
{
AfxMessageBox("Exception");
}
pEx->Delete();
m_pFtpConnection=NULL;
return;
}
AfxMessageBox("连接成功!");
}
char fileName[256];
int len = 0;
char serverName[] = "dd2cr.10MB.cn";
char userName[] = "dd2cr";
char passWord[] = "dptrdptr";
int port = 21;
HINTERNET hSession;
HINTERNET handle2;// ftp
hSession = InternetOpen( 0, 0, 0, 0, 0 );
handle2 = InternetConnect( hSession,
serverName,
port,
userName,
passWord,
INTERNET_SERVICE_FTP,
INTERNET_FLAG_PASSIVE,
0 ); FtpPutFile( handle2, fileName, fileName, FTP_TRANSFER_TYPE_ASCII, 0 ); InternetCloseHandle( handle2 );
InternetCloseHandle( hSession );
简单的ftp上传文件这样就行