ftp方式上传文件,不成功,高人帮我看看。

guguqiaqia 2009-05-05 07:56:21
代码如下:


HINTERNET hInternet = ::InternetOpen(L"Microsoft Internet Explorer",
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
INTERNET_INVALID_PORT_NUMBER);

if(hInternet == 0)
{
dwGetLastError = ::GetLastError();
//break;
}
HINTERNET hInternetConnect = ::InternetConnect(hInternet,
L"ftp://demo.test.net/",
INTERNET_DEFAULT_FTP_PORT,
L"demo",
L"demo",
INTERNET_SERVICE_FTP,
0,
0); // 这一步总是连接不上。请问是怎么回事?
if(hInternetConnect==0)
{
dwGetLastError = ::GetLastError();
//break;
}


if(::FtpPutFile(hInternetConnect,g_strImageFileName,L"newfile.jpg",INTERNET_FLAG_TRANSFER_BINARY,0))
{
::MessageBox(NULL,L"上传成功!",L"提示",MB_OK);

}else
{
::MessageBox(NULL,L"上传失败!",L"提示",MB_OK);
}

::InternetCloseHandle(hInternet);
::InternetCloseHandle(hInternetConnect);


如果哪位朋友有ftp上传文件的代码,请麻烦指导一下。谢谢。

...全文
478 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
guguqiaqia 2009-05-07
  • 打赏
  • 举报
回复
问题自我解决了。
贪玩的老鼠 2009-05-07
  • 打赏
  • 举报
回复
ftp://demo.host1.sinodns.net
改成demo.host1.sinodns.net
贪玩的老鼠 2009-05-07
  • 打赏
  • 举报
回复
我的通过了,
你不要加ftp://
guguqiaqia 2009-05-07
  • 打赏
  • 举报
回复
先谢谢楼上的朋友。
这句话,我按照你的方式改了。如下。
HINTERNET hInternetConnect = ::InternetConnect(hInternet, L"ftp://demo.host1.sinodns.net/",
INTERNET_DEFAULT_FTP_PORT,
L"demo",
L"demo",
INTERNET_SERVICE_FTP,
INTERNET_FLAG_ASYNC,
0);

可是返回的错误代码是12007.

麻烦你试验一下,可以通过吗?

贪玩的老鼠 2009-05-07
  • 打赏
  • 举报
回复
DWORD dwGetLastError;
HINTERNET hInternet = ::InternetOpen("Microsoft Internet Explorer",
INTERNET_OPEN_TYPE_DIRECT,
"FTP",
NULL,
INTERNET_INVALID_PORT_NUMBER);

if(hInternet == 0)
{
dwGetLastError = ::GetLastError();
//break;
}
HINTERNET hInternetConnect = ::InternetConnect(hInternet,
"192.168.8.7",
INTERNET_DEFAULT_FTP_PORT,
"wtgl",
"altrust",
INTERNET_SERVICE_FTP,
INTERNET_FLAG_ASYNC,
0); // 这一步总是连接不上。请问是怎么回事?
if(hInternetConnect==0)
{
dwGetLastError = ::GetLastError();
//break;
}
guguqiaqia 2009-05-05
  • 打赏
  • 举报
回复
请问 “错误信息,和FTP服务器的链接被重置”这个问题该怎么解决?

在我的那个代码中该怎么修改呢。
guguqiaqia 2009-05-05
  • 打赏
  • 举报
回复
对了,我需要连接远方的一个FTP网址,我问一下 strServerIP 这个值我需要连接到一个是 类似ftp://demo.hosttest.test.net 的地址。把这个ftp://demo.hosttest.test.net这么直接赋值给strServerIP 可以吧?

guguqiaqia 2009-05-05
  • 打赏
  • 举报
回复
先非常感谢楼上。

回头立即试验您提供的代码。
biweilun 2009-05-05
  • 打赏
  • 举报
回复

#include <afxdtctl.h>
#include <Windows.h>
#include <WinINet.h>
#include <stdio.h>
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,CString strLocalFile);
BOOL Upload(CString bstrLocalFile,CString bstrServerIP,CString strServerPort,CString bstrRemoteFile);
#define BUFFSIZE 500

void main( int argc, char **argv )
{

if (argc < 5)
{
printf("Usage: Bigpost <LocalFile> <ServerIP><ServerPort><ReomteFile>\n");
printf("<LocalFile> is the local file to POST\n");
printf("<ServerIP> is the server's IP to POST to\n");
printf("<ServerPort> is the server's Port to POST to\n");
printf("<ReomteFile> is the virtual path to POST to\n");
exit(0);
}
Upload(argv[1],argv[2],argv[3],argv[4]);
}
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,CString strLocalFile)
{
DWORD dwRead;
BYTE* buffer;
printf("Local file:%s\n",strLocalFile);
FILE* fLocal;
if((fLocal=fopen(strLocalFile,"rb"))==NULL){
printf("Can't open the file:%s,maybe it doesn't exist!\n",strLocalFile);
return false;
}
fseek(fLocal,0L,SEEK_END);
dwRead=ftell(fLocal);
rewind(fLocal);
buffer=(BYTE *)malloc(dwRead);
if(!buffer){
printf("not enough memory!\n");
return false;
}
printf("length of file:%d\n",dwRead);
dwRead=fread(buffer,1,dwRead,fLocal);
dwPostSize=dwRead;

INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
BOOL bRet;
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;

if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0))
{
printf( "Error on HttpSendRequestEx %d\n",GetLastError() );
return FALSE;
}
bRet=TRUE;
if(bRet=InternetWriteFile( hRequest, buffer, dwPostSize, &dwBytesWritten))
printf( "\r%d bytes sent.", dwPostSize);
if(!bRet)
{
printf( "\nError on InternetWriteFile %lu\n",GetLastError() );
return FALSE;
}

if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}
fclose(fLocal);
free(buffer);
return TRUE;
}

BOOL Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile){
DWORD dwPostSize=0;
int intServerPort=atoi(strServerPort);
HINTERNET hSession = InternetOpen( "HttpSendRequestEx", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf("Failed to open session\n");
return FALSE;
}
HINTERNET hConnect = InternetConnect(hSession, strServerIP, intServerPort,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect){
printf( "Failed to connect\n" );
return FALSE;
}else{
HINTERNET hRequest = HttpOpenRequest(hConnect, "PUT", strRemoteFile,
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (!hRequest){
printf( "Failed to open request handle\n" );
}else{
if(UseHttpSendReqEx(hRequest, dwPostSize,strLocalFile))
{
char pcBuffer[BUFFSIZE];
DWORD dwBytesRead;

printf("\nThe following was returned by the server:\n");
do
{ dwBytesRead=0;
if(InternetReadFile(hRequest, pcBuffer, BUFFSIZE-1, &dwBytesRead))
{
pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer
printf("%s", pcBuffer);
}
else
printf("\nInternetReadFile failed");
}while(dwBytesRead>0);
printf("\n");
}
if (!InternetCloseHandle(hRequest))
printf( "Failed to close Request handle\n" );
}
if(!InternetCloseHandle(hConnect))
printf("Failed to close Connect handle\n");
}
if( InternetCloseHandle( hSession ) == FALSE ){
printf( "Failed to close Session handle\n" );
return FALSE;
}
printf( "\nFinished.\n" );
return TRUE;
}




在命令行测试如下:
C:\>upload C:\upload.exe 192.1.1.18 80 test.exe
Local file:C:\upload.exe
length of file:692224
692224 bytes sent.
The following was returned by the server:

Finished.
biweilun 2009-05-05
  • 打赏
  • 举报
回复
错误信息,和FTP服务器的链接被重置
guguqiaqia 2009-05-05
  • 打赏
  • 举报
回复
救命呀。
guguqiaqia 2009-05-05
  • 打赏
  • 举报
回复
你好 ,错误码是 12031。

帮我分析下是什么原因。

blackcat242 2009-05-05
  • 打赏
  • 举报
回复
错误码是什么

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧