cocos Httpclient 上传文件到服务器
来源:互联网 发布:商家如何退出农村淘宝 编辑:程序博客网 时间:2024/05/18 17:55
废话不多说、直接贴代码!
#include "Test01060Http.h"
#include <Shlwapi.h>
#include "stdio.h"
Test01060Http::Test01060Http()
{
}
Test01060Http::~Test01060Http()
{
}
bool Test01060Http::init()
{
Layer::init();
UploadPictures();
return true;
}
#define MYDATA "this is extra data from client"
void Test01060Http::UploadPictures()
{
network::HttpClient* http = network::HttpClient::getInstance();
network::HttpRequest* req = new network::HttpRequest;
req->setRequestType(network::HttpRequest::Type::POST);
req->setUrl("http:///subassembly/uploadPicterController/upload.html");
req->setResponseCallback(CC_CALLBACK_2(Test01060Http::onHttpRequestCompleted, this));
std::string pathKey = FileUtils::sharedFileUtils()->fullPathForFilename("b2de9c82d158ccbf0881c1d01dd8bc3eb135411e.jpg");
CCLOG("path = %s",pathKey.c_str());
//auto sifile = PathFindFileName(pathKey);
Data imgdata = FileUtils::getInstance()->getDataFromFile(pathKey);
ssize_t pos = B.find_last_of("/");
std::string cach_filename;
if (pos !=std::string::npos)
{
cach_filename = B.substr(pos + 1);
}
else
{
cach_filename = B;
}
unsigned char * pBuffer = FileUtils::sharedFileUtils()->getFileData(pathKey.c_str(),"rb", &buff);
const char* fileBinary = (const char*)pBuffer;
CCLOG("fileBinary %s = ", fileBinary);
//得到图片 文件流大小
std::string strBin = std::string(fileBinary, buff);
//设置 http headers 参数 和 数据流
std::string boundary = "----------------WebKitFormBou3123ndaryEm5WNw6hGiQUBpng";
std::string bound = boundary;
std::vector<std::string> headers;
headers.push_back("Content-Type:multipart/form-data; boundary = " + bound);
std::string strdata = strBin;
std::string str = "\r\n--" + boundary + "\r\n";
str = str + "Content-Disposition: form-data; name=\"file\"; filename=\"paoku1.png\"\r\n";
str = str + "Content-Type: application/octet-stream\r\n\r\n";
str = str + strBin;
str = str + "\r\n--" + boundary + "--\r\n";
//设置 RequesData
req->setRequestData(str.data(), str.size());
};
void Test01060Http::onHttpRequestCompleted(network::HttpClient* client, network::HttpResponse* response)
{
if (!response->isSucceed())
{
CCLOG("error");
CCLOG("error buffer: %s", response->getErrorBuffer());
CCLOG("error code: %d", (int)response->getResponseCode());
return;
}
CCLOG("login success");
};
#include "Test01060Http.h"
#include <Shlwapi.h>
#include "stdio.h"
Test01060Http::Test01060Http()
{
}
Test01060Http::~Test01060Http()
{
}
bool Test01060Http::init()
{
Layer::init();
UploadPictures();
return true;
}
#define MYDATA "this is extra data from client"
void Test01060Http::UploadPictures()
{
network::HttpClient* http = network::HttpClient::getInstance();
network::HttpRequest* req = new network::HttpRequest;
req->setRequestType(network::HttpRequest::Type::POST);
req->setUrl("http:///subassembly/uploadPicterController/upload.html");
req->setResponseCallback(CC_CALLBACK_2(Test01060Http::onHttpRequestCompleted, this));
std::string pathKey = FileUtils::sharedFileUtils()->fullPathForFilename("b2de9c82d158ccbf0881c1d01dd8bc3eb135411e.jpg");
CCLOG("path = %s",pathKey.c_str());
//auto sifile = PathFindFileName(pathKey);
Data imgdata = FileUtils::getInstance()->getDataFromFile(pathKey);
LPTSTR a = (LPTSTR)pathKey.c_str();
/ / 拿到最后一个斜杠最后文件名实例!
std::string B = "C:\\Windows\\System32/notepad.exe";ssize_t pos = B.find_last_of("/");
std::string cach_filename;
if (pos !=std::string::npos)
{
cach_filename = B.substr(pos + 1);
}
else
{
cach_filename = B;
}
CCLOG("B = %s", cach_filename.c_str());
//拿到图片数据
long buff = 0;unsigned char * pBuffer = FileUtils::sharedFileUtils()->getFileData(pathKey.c_str(),"rb", &buff);
const char* fileBinary = (const char*)pBuffer;
CCLOG("fileBinary %s = ", fileBinary);
//得到图片 文件流大小
std::string strBin = std::string(fileBinary, buff);
//设置 http headers 参数 和 数据流
std::string boundary = "----------------WebKitFormBou3123ndaryEm5WNw6hGiQUBpng";
std::string bound = boundary;
std::vector<std::string> headers;
headers.push_back("Content-Type:multipart/form-data; boundary = " + bound);
std::string strdata = strBin;
std::string str = "\r\n--" + boundary + "\r\n";
str = str + "Content-Disposition: form-data; name=\"file\"; filename=\"paoku1.png\"\r\n";
str = str + "Content-Type: application/octet-stream\r\n\r\n";
str = str + strBin;
str = str + "\r\n--" + boundary + "--\r\n";
// 设置设置Header
req->setHeaders(headers);//设置 RequesData
req->setRequestData(str.data(), str.size());
CCLOG("str data = %s \n str .size = %d \n", str.data(), str.size());
//发送
http->send(req);
//关闭
req->release();};
void Test01060Http::onHttpRequestCompleted(network::HttpClient* client, network::HttpResponse* response)
{
if (!response->isSucceed())
{
CCLOG("error");
CCLOG("error buffer: %s", response->getErrorBuffer());
CCLOG("error code: %d", (int)response->getResponseCode());
return;
}
CCLOG("login success");
};
0 0
- cocos Httpclient 上传文件到服务器
- 使用httpclient上传文件到web服务器
- 通过httpclient把文件上传到服务器-------服务器处理部分
- httpclient 从服务器A 上传文件到服务器B
- (Android)使用httpclient上传文件到服务器
- java上传文件到远程服务器(二)---HttpClient方式
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 基于HttpClient访问服务器以及上传文件
- HDU 4770Lights Against Dudely(2013 Asia Hangzhou Regional Contest A)
- 今天实现了类似淘宝上划操作显示详情页的demo
- 使用opencv训练问题及解决办法
- OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)的用法
- vix --- 免路径输入的vim
- cocos Httpclient 上传文件到服务器
- 如何保持守护进程被杀死后能够再次启动
- Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project Th
- Flume学习04 — Sink
- 黑马程序员 Java基础——Collection接口
- 什么是汇编语言(一) 汇编底层原理,指令字节码
- debian 中定时任务cron使用
- 理解字符集和编码
- FragmentPagerAdapter 和 FragmentStatePagerAdapter 区别