vc++上傳檔案
//獲取檔案內容
bool file_con(char **buffer,LPCSTR file)
{
FILE *fp = fopen(file, "rb");
if(fp==NULL)
{
printf("檔案上傳失敗,請檢查檔案路徑.....\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;
}
//檔案上傳
std::string upload(LPCSTR lpszServer,LPCSTR lpszAddr,LPCSTR fileUrl)
{
char *file = NULL;
if(!file_con(&file,fileUrl))
{
return "0";
}
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
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);
return "0";
}
printf("ip address = %s, port = %d\n",inet_ntoa(server.sin_addr), ntohs(server.sin_port));
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/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko\r\n";
header += "Connection: Keep-Alive\r\n";
header += "Accept: text/html, application/xhtml+xml, */*\r\n";
header += "Referer: http://www.xxx.com/form/submit.php?name=uninstall\r\n";
header += "Accept-Language: zh-CN\r\n";
header += "Pragma: no-cache\r\n";
header += "Proxy-Connection: Keep-Alive\r\n";
header += "Content-Type: multipart/form-data;boundary=----------------------------64b23e4066e5d\r\n";
content += "------------------------------64b23e4066e5d\r\n";
content += "Content-Disposition: form-data; name=\"field#[file][]\"; filename=\"1.jpg\"\r\n";
content += "Content-Type: image/jpeg\r\n";
//post尾時間戳
std::string strContent("\r\n------------------------------64b23e4066e5d--\r\n");
char temp[64] = {0};
//注意下面這個引數Content-Length,這個引數值是:http請求頭長度+請求尾長度+檔案總長度
sprintf(temp, "Content-Length: %d\r\n\r\n", content.length()+l_file_len+strContent.length());
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.2);
}
::send(sock,strContent.c_str(),strContent.length(),0);
char szBuffer[10240] = {0};
while (true)
{
int nRet = ::recv(sock, szBuffer, sizeof(szBuffer), 0);
if (nRet == 0 || nRet == WSAECONNRESET)
{
printf("Connection Closed.\n");
break;
}
else if (nRet == SOCKET_ERROR)
{
printf("socket error\n");
break;
}
else
{
printf("recv() returned %d bytes\n", nRet);
printf("received: %s\n", szBuffer);
break;
}
}
::closesocket(sock);
delete [] file;
return szBuffer;
}
void main()
{
WORD wVersionRequested=MAKEWORD(2,2);
WSADATA wsaData;
if(WSAStartup(wVersionRequested,&wsaData))
{
cout<<"載入錯誤"<<endl;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wHighVersion)!=2)
{
cout<<"WinSock's 載入錯誤"<<endl;
}
upload("www.xxx.com","/form/submit.php?name=uninstall","C:\\Users\\Administrator\\Desktop\\1.jpg");
return ;
}
curl:上傳檔案
curl_global_init(CURL_GLOBAL_WIN32);
CURL*easy_handle = curl_easy_init();
// 使用multi-parts form post
curl_easy_setopt(easy_handle, CURLOPT_URL,"http://wvw.xxx.com/form/submit.php?name=uninstall");
curl_httppost*post = NULL; curl_httppost *last =NULL;
// 文字資料
//curl_formadd(&post,&last, CURLFORM_COPYNAME, "id",CURLFORM_COPYCONTENTS, "JGood",CURLFORM_END);
//curl_formadd(&post,&last, CURLFORM_COPYNAME, "forward",CURLFORM_COPYCONTENTS, "HangZhou", CURLFORM_END);
//curl_formadd(&post,&last, CURLFORM_COPYNAME, "field#[test]",CURLFORM_COPYCONTENTS, "te", CURLFORM_END);
//curl_formadd(&post,&last, CURLFORM_COPYNAME, "field#[file][]",CURLFORM_COPYCONTENTS, "te", CURLFORM_END);
// 文字檔案中的資料
curl_formadd(&post,&last, CURLFORM_COPYNAME, "field#[file][]",CURLFORM_FILE, "1.jpg", CURLFORM_CONTENTTYPE, "image/jpeg",CURLFORM_END);
curl_easy_setopt(easy_handle, CURLOPT_HTTPPOST,post);
curl_easy_perform(easy_handle);
curl_formfree(post);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
相關文章
- php檔案上傳之多檔案上傳PHP
- 單個檔案上傳和批量檔案上傳
- 檔案上傳
- SpringMVC 單檔案上傳與多檔案上傳SpringMVC
- Java大檔案上傳、分片上傳、多檔案上傳、斷點續傳、上傳檔案minio、分片上傳minio等解決方案Java斷點
- 檔案上傳之三基於flash的檔案上傳
- 前端大檔案上傳/分片上傳前端
- Flask——檔案上傳Flask
- PHP上傳檔案PHP
- JavaScript 檔案上傳JavaScript
- Git上傳檔案Git
- YII檔案上傳
- 檔案上傳概述
- beego上傳檔案Go
- 上傳檔案流程
- 上傳EXCLE檔案
- PHP 檔案上傳PHP
- 檔案上傳漏洞
- MVC檔案上傳 - 使用Request.Files上傳多個檔案MVC
- .NET Core 如何上傳檔案及處理大檔案上傳
- Django檔案上傳 -- 適用於單一小檔案上傳Django
- 上傳檔案的陷阱
- HTTP檔案上傳原理HTTP
- 上傳檔案專題
- PHP 分片上傳檔案PHP
- Java Web 檔案上傳JavaWeb
- WEB漏洞——檔案上傳Web
- Aliyun Oss 上傳檔案
- 使用fileinput上傳檔案
- PHP ftp上傳檔案PHPFTP
- 上傳檔案至GitHubGithub
- 檔案上傳測試
- Linux上傳檔案Linux
- laravel 多檔案上傳Laravel
- ajaxfileupload 檔案上傳
- Ajax 之檔案上傳
- SpringMvc檔案上傳SpringMVC
- PHP--檔案上傳PHP