📄 hzupload.cpp
字号:
// hzupload.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "hzupload.h"
#include <fstream>
#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
ifstream infile ("sys.ini");
string b;
getline(infile,b);
infile.close();
int size = b.size();
for (int ix = 0; ix<size; ++ix)
{
if ( b[ix]=='\\' )
{
b[ix] = '/';
}
}
b.erase(0,1);
b.erase(size-2,1);
CString lps1(b.c_str());
ifstream infile1("ServerPath.ini");
string server;
getline(infile1,server);
CString lps(server.c_str());
if (UploadFile1(lps,lps1))
{
cout<<"ok";
}
else
{
}
infile.close();
infile1.close();
}
return nRetCode;
}
//strURL 负责接收上传操作的页面的URL
//strLocalFileName 待上传的本地文
bool UploadFile1(CString fserver,CString filePath)
{
// TODO: Add extra validation here
CInternetSession session;
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString retStr;
//AfxMessageBox("进入UploadFile");
try
{
CString strServerName;
CString strObject;
INTERNET_PORT nPort=8082;
DWORD dwServiceType;
UINT BUFSIZE=262144;
AfxParseURL(fserver, dwServiceType, strServerName, strObject,nPort);
pServer = session.GetHttpConnection(strServerName, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT| INTERNET_FLAG_NO_AUTO_REDIRECT| INTERNET_FLAG_MAKE_PERSISTENT);
pFile->SetWriteBufferSize(BUFSIZE+1);
pFile->SetReadBufferSize(1024);
CString boundary ="----#@!---GeniuZ---!@#-----c22AaRQh12t6Va";
CString header = "Content-type:multipart/form-data; boundary="+ boundary ;
pFile->AddRequestHeaders(header);
UINT fsize=0;
char* p=GenerateRequestBody(filePath,boundary,&fsize);
// AfxMessageBox(p);
pFile->SendRequestEx(fsize);
for(UINT i=0;i*BUFSIZE<fsize;i++)
{
UINT gsize=BUFSIZE;
if((i+1)*BUFSIZE>fsize)
{
gsize=fsize-i*BUFSIZE;
}
pFile->Write(p+i*BUFSIZE,gsize);
}
delete p;
pFile->Flush();
pFile->EndRequest();
TCHAR sz[1024];
while (pFile->ReadString(sz, 1023)) retStr += sz;
pFile->Close();
pServer->Close();
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
AfxMessageBox(szErr);//报告出错信息
pEx->Delete();
return 0;
}
if (pFile != NULL) delete pFile;
if (pServer != NULL) delete pServer;
session.Close();
retStr.TrimLeft();
retStr.TrimRight();
if(retStr.GetLength()>8)
{
if(retStr.Find("success!")==0)
// return retStr.Right(retStr.GetLength()-8);
retStr.Right(retStr.GetLength()-8);
}
retStr="";
//AfxMessageBox("走出UploadFile");
// return retStr;
return 1;
}
char* GenerateRequestBody(CString filename, CString boundary,UINT* dwFileSize)
{
CString requestBody;
// For each file...
CString field_name = "upload_file";
CString contentDisposition = "Content-Disposition: form-data; name=\"" + field_name + "\"; filename=\"" + filename + "\"";
CString contentType = "Content-Type: application/octet-stream";
requestBody+="--";
requestBody+=boundary;
requestBody+= "\r\n";
requestBody+=contentDisposition + "\r\n";
requestBody+=contentType + "\r\n";
requestBody+="\r\n";
HANDLE hFile = CreateFile(filename ,GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
// assert(hFile != INVALID_HANDLE_VALUE);
*dwFileSize = GetFileSize(hFile, NULL);
char *p = (char *)malloc(*dwFileSize+1000);
strcpy(p,(LPCTSTR)(LPCTSTR)requestBody);
DWORD dwBytesRead;
ReadFile(hFile, p+requestBody.GetLength(), *dwFileSize, &dwBytesRead, NULL);
//CString fileContents;
//fileContents=p, dwFileSize);
int inx=requestBody.GetLength();
requestBody= "\r\n";
requestBody+="--" + boundary + "--\r\n";
strcpy(p+*dwFileSize+inx,(LPCTSTR)(LPCTSTR)requestBody);
*dwFileSize=*dwFileSize+inx+requestBody.GetLength();
CloseHandle(hFile);
return p;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -