<span style="font-family: Arial, Helvetica, sans-serif;">#pragma once</span>
#include "stdio.h"
#include "afxwin.h"
#include "afxinet.h"
#include "WinSock2.h"
#include "iostream"
using namespace std;
#pragma comment(lib,"ws2_32.lib")
class CUpload
{
public:
CUpload(void);
~CUpload(void);
private:
long l_file_len;
/编码转换/
char * m_lpUTF8; //UTF8编码缓存指针
char * m_lpMultiByte; //Multil多字节编码指针
wchar_t * m_wpWideChar; //Widechar编码缓存指针
int m_nUTF8Len;
int m_nMultiByteLen;
int m_nWideCharLen;
public:
bool Send_File(LPCTSTR lpszServer,LPCTSTR lpszAddr,int Server_Port,LPCTSTR File_Name);
bool UploadFile(LPCTSTR lpszServer,LPCTSTR lpszAddr,LPCTSTR fileUrl,int Server_Port);
bool Read_FileInfo(char **buffer,LPCSTR file);
CHttpConnection* m_pConnection;
CString MakeRequestHeaders(CString& strBoundary);
CString MakePreFileData(CString& strBoundary, CString& strFileName, int iRecordID);
CString MakePostFileData(CString& strBoundary);
void WideCharToUTF8(LPCWSTR wpWideData, int WideLen);
};
//.cpp
#include "StdAfx.h"
#include "Upload.h"
#include <iostream>
#include <io.h>
#include <string>
using namespace std;
CUpload::CUpload(void)
{
m_lpUTF8=NULL;
}
CUpload::~CUpload(void)
{
}
bool CUpload::Send_File(LPCTSTR lpszServer,LPCTSTR lpszAddr,int Server_Port,LPCTSTR File_Name)
{
WORD wVersionRequested=MAKEWORD(2,2);
WSADATA wsaData;
if(WSAStartup(wVersionRequested,&wsaData))
{
return false;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wHighVersion)!=2)
{
return false;
}
UploadFile(lpszServer,lpszAddr,File_Name,Server_Port);
}
//获取文件内容
bool CUpload::Read_FileInfo(char **buffer,LPCSTR file)
{
FILE *fp = fopen(file, "rb");
if(fp==NULL)
{
return false;
}
fseek(fp, 0, SEEK_END);
l_file_len = ftell(fp);
rewind(fp);
*buffer = new char[l_file_len + 1];
memset(*buffer, 0, l_file_len + 1);
fseek(fp, 0, SEEK_SET);
fread(*buffer, sizeof(char), l_file_len, fp);
fclose(fp);
return true;
}
/*string*/ CString CUpload::MakeRequestHeaders(CString &strBoundary)//包头
{
CString strFormat;
CString strData;
strFormat = _T("Content-Type:multipart/form-data;boundary=%s\r\n");//二进制文件传送Content-Type类型为: multipart/form-data
string data_0="Content-Type:multipart/form-data;boundary=-----------------------------7b4a6d158c9";
string data_1="\r\n";
strData.Format(strFormat, strBoundary);
return strData;
/*string data_info=data_0+data_1;
return data_info;*/
}
/*string*/ CString CUpload::MakePreFileData(CString &strBoundary, CString &strFileName, int iRecordID)
{
/**/
//Content-Type:
//JPG image/pjpeg
//PNG image/x-png
//BMP image/bmp
//TIF image/tiff
//GIF image/gif
CString strFormat;
CString strData;
strFormat += _T("--%s");
strFormat += _T("\r\n");
strFormat += _T("Content-Disposition: form-data; name=\"para1\"");//传给网络上的参数,根据网站抓包查看到底是需要哪些
strFormat += _T("\r\n\r\n");
strFormat += _T("my name is xiaoxiong");
strFormat += _T("\r\n");
strFormat += _T("--%s");
strFormat += _T("\r\n");
strFormat += _T("Content-Disposition: form-data; name=\"para2\"");
strFormat += _T("\r\n\r\n");
strFormat += _T("sdfasd");//国软件研究所
strFormat += _T("\r\n");
strFormat += _T("--%s");
strFormat += _T("\r\n");
strFormat += _T("Content-Disposition: form-data; name=\"trackdata\"; filename=\"%s\"");//文件地址信息
strFormat += _T("\r\n");
strFormat += _T("Content-Type: image/pjpeg");
strFormat += _T("\r\n\r\n");
strData.Format(strFormat, strBoundary, strBoundary, strBoundary, strFileName);//
int szie=strData.GetLength();
return strData;
}
CString CUpload::MakePostFileData(CString &strBoundary)//发送请求包
{
CString strFormat;
CString strData;
strFormat = _T("\r\n");
strFormat += _T("--%s");
strFormat += _T("\r\n");
strFormat += _T("Content-Disposition: form-data; name=\"submitted\"");
strFormat += _T("\r\n\r\n");
strFormat += _T("hello");
strFormat += _T("\r\n");
strFormat += _T("--%s--");
strFormat += _T("\r\n");
strData.Format(strFormat, strBoundary, strBoundary);
return strData;
}
//文件上传
bool CUpload::UploadFile(LPCTSTR lpszServer,LPCTSTR lpszAddr,LPCTSTR fileUrl,int Server_Port)
{
CString _mFilePath;
_mFilePath = fileUrl;//要传的本地文件地址
int startp = _mFilePath.ReverseFind('\\');
int namelen = _mFilePath.GetLength()-startp-1;
CString pcmname = _mFilePath;//.Mid(startp+1,namelen);
CString defServerName =lpszServer;//服务器名
CString defObjectName =lpszAddr;//保存的地址
// USES_CONVERSION;
CInternetSession Session;
CHttpConnection *pHttpConnection = NULL;
INTERNET_PORT nPort = Server_Port/*8076*/;
CFile fTrack;
CHttpFile* pHTTP;
CString strHTTPBoundary;
CString strPreFileData;
CString strPostFileData;
DWORD dwTotalRequestLength;
DWORD dwChunkLength;
DWORD dwReadLength;
DWORD dwResponseLength;
TCHAR szError[MAX_PATH];
void* pBuffer;
LPSTR szResponse;
CString strResponse;
bool bSuccess = true;
CString strDebugMessage;
if (FALSE == fTrack.Open(_mFilePath, CFile::modeRead | CFile::shareDenyWrite))//读出文件
{
return FALSE;
}
int iRecordID = 1;
strHTTPBoundary = _T("---------------------------7b4a6d158c9");//定义边界值
strPreFileData = MakePreFileData(strHTTPBoundary, pcmname, iRecordID);
strPostFileData = MakePostFileData(strHTTPBoundary);
int size_file= fTrack.GetLength();
int PostFileDta_Size=strPostFileData.GetLength() ;
int PreFileData= strPreFileData.GetLength();
dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();//计算整个包的总长度
dwChunkLength = 64 * 1024;
pBuffer = malloc(dwChunkLength);
if (NULL == pBuffer)
{
return FALSE;
}
try
{
pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);
CString str=MakeRequestHeaders(strHTTPBoundary);
pHTTP->AddRequestHeaders(str);//发送包头请求
pHTTP->SendRequestEx(dwTotalRequestLength/*, HSR_SYNC | HSR_INITIATE*/);
USES_CONVERSION;
#ifdef _UNICODE
// WideCharToUTF8((LPCWSTR)(LPCTSTR)strPreFileData,strPreFileData.GetLength());
pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());
//pHTTP->Write(m_lpUTF8,m_nUTF8Len);
#else
pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());//写入服务器所需信息
#endif
dwReadLength = -1;
size_t length=-1;
char *file_info;
char *info=new char[dwChunkLength];
while (0 != dwReadLength)
{
strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());
TRACE(strDebugMessage);
dwReadLength = fTrack.Read(pBuffer, dwChunkLength);//文件内容
if (0!=dwReadLength)
{
pHTTP->Write(pBuffer,dwReadLength);//写入服务器本地文件,用二进制进行传送
}
}
#ifdef _UNICODE
pHTTP->Write(W2A(strPostFileData.GetBuffer()), strPostFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
#endif
pHTTP->EndRequest(HSR_SYNC);
dwResponseLength = pHTTP->GetLength();
while (0 != dwResponseLength)
{
szResponse = (LPSTR)malloc(dwResponseLength + 1);
szResponse[dwResponseLength] = '\0';
pHTTP->Read(szResponse, dwResponseLength);
strResponse += szResponse;
free(szResponse);
dwResponseLength = pHTTP->GetLength();
}
}
catch (CException* e)
{
e->GetErrorMessage(szError, MAX_PATH);
e->Delete();
bSuccess = false;
}
pHTTP->Close();
delete pHTTP;
fTrack.Close();
if (NULL != pBuffer)
{
free((void*)pBuffer);
}
return bSuccess;
///
}
//将宽字符转为UTF-8
void CUpload::WideCharToUTF8(LPCWSTR wpWideData, int WideLen)
{
//如果原先的数据没有删除
if (m_lpUTF8)
{
delete [] m_lpUTF8;
m_lpUTF8 = NULL;
}
m_nUTF8Len = WideCharToMultiByte(CP_UTF8,0,wpWideData,WideLen,NULL,0,NULL,NULL);
m_lpUTF8 = new char[m_nUTF8Len + 1];
m_lpUTF8[m_nUTF8Len] = 0;
WideCharToMultiByte(CP_UTF8,0,wpWideData,WideLen,m_lpUTF8,m_nUTF8Len,NULL,NULL);
}
<pre name="code" class="cpp">//例如上传test.zip到指定的网址
str_ServerUrl="www.test.com";
str_ServerAddr="/Upload.aspx";
Server_Port=8076;
zip_name=L"test.zip";
m_upload.Send_File(str_ServerUrl,str_ServerAddr,Server_Port,zip_name);