long l_file_len; //获取文件内容 bool file_con(char **buffer, LPCSTR file) { FILE *fp = fopen(file, "rb"); if (fp == NULL) { std::wcout << "请检查文件路径.....\n"; 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; } //文件上传 /** lpszServer:服务器地址 lpszAddr:服务器接受文件地址 fileUrl:文件存放目录 filename:文件名称带扩展名的 */ std::string upload(LPCSTR lpszServer, LPCSTR lpszAddr, LPCSTR fileUrl,string filename) { char *file = NULL; if (!file_con(&file, fileUrl)) { return "0"; } WORD wVersionRequested; WSAData wsaData; int err; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ printf("WSAStartup() called failed!\n"); Sleep(5); return 0; } else { printf("WSAStartup() called successful!\n"); } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup(); Sleep(5); return "0"; } SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == INVALID_SOCKET) { printf("connect() called failed! The error code is: %d\n", WSAGetLastError()); Sleep(5); return "0"; } SOCKADDR_IN server; server.sin_family = AF_INET; server.sin_port = htons(80); struct hostent *host_addr = gethostbyname(lpszServer); if (host_addr == NULL) { return "host_addr == NULL"; } server.sin_addr.s_addr = *((int *)*host_addr->h_addr_list); if (::connect(sock, (SOCKADDR *)&server, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { ::closesocket(sock); Sleep(5); return "0"; } //printf("ip address = %s, port = %d\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port)); CTime currTime; currTime = CTime::GetCurrentTime(); CString dateformat; dateformat.Format(_T("%u"), currTime.GetTime()); std::string dateCurrent = dateformat.GetBuffer(0); std::string header(""); std::string content(""); //----------------------post头开始-------------------------------- header += "post "; header += lpszAddr; header += " HTTP/1.1\r\n"; header += "Host: "; header += lpszServer; header += "\r\n"; header += "User-Agent: Mozilla/4.0\r\n"; header += "Connection: Keep-Alive\r\n"; header += "Accept: */*\r\n"; header += "Pragma: no-cache\r\n"; header += "Content-Type: multipart/form-data; charset=\"gb2312\"; boundary=----------------------------"+ dateCurrent +"\r\n"; content += "------------------------------"+ dateCurrent +"\r\n"; content += "Content-Disposition: form-data; name=\"file\"; filename=\""+ filename +"\"\r\n"; content += "Content-Type: aapplication/octet-stream\r\n\r\n"; //post尾时间戳 std::string strContent("\r\n------------------------------"+ dateCurrent +"\r\n"); char temp[64] = { 0 }; //注意下面这个参数Content-Length,这个参数值是:http请求头长度+请求尾长度+文件总长度 sprintf(temp, "Content-Length: %d\r\n\r\n", content.length() + l_file_len); header += temp; std::string str_http_request; str_http_request.append(header).append(content); //----------------------post头结束----------------------------------- //发送post头 send(sock, str_http_request.c_str(), str_http_request.length(), 0); char fBuff[4096]; int nPacketBufferSize = 4096; // 每个数据包存放文件的buffer大小 int nStart;//记录post初始位置 int nSize;//记录剩余文件大小 // 就分块传送 for (int i = 0; i < l_file_len; i += nPacketBufferSize) { nStart = i; if (i + nPacketBufferSize + 1 > l_file_len) { nSize = l_file_len - i; } else { nSize = nPacketBufferSize; } memcpy(fBuff, file + nStart, nSize); ::send(sock, fBuff, nSize, 0); Sleep(0.5); } //::send(sock, strContent.c_str(), strContent.length(), 0); char szBuffer[1024] = { 0 }; while (true) { int nRet = ::recv(sock, szBuffer, sizeof(szBuffer), 0); if (nRet == 0 || nRet == WSAECONNRESET) { std::wcout << "Connection Closed.\n"; break; } else if (nRet == SOCKET_ERROR) { std::wcout << "socket error\n"; break; } else { //char Information_nRet[256]; //char Information_szBuffer[256]; //sprintf(Information_nRet, "recv() returned %d bytes\n", nRet); //std::wcout << Information_nRet; //sprintf(Information_szBuffer,"received: %s\n", szBuffer); //std::wcout << Information_szBuffer; break; } } ::closesocket(sock); delete[] file; return "0"; //return szBuffer; } int main(){ char temp_path[300]; string res_notice = upload("域名", "/interface/.", temp_path, '123.txt'); }
|
---|