本文整理汇总了C++中HttpServletRequest::getHttpMime方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpServletRequest::getHttpMime方法的具体用法?C++ HttpServletRequest::getHttpMime怎么用?C++ HttpServletRequest::getHttpMime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpServletRequest
的用法示例。
在下文中一共展示了HttpServletRequest::getHttpMime方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: doUpload
// POST 方式且满足:
// Content-Type: multipart/form-data; boundary=xxx
bool doUpload(HttpServletRequest& req, HttpServletResponse& res)
{
// 先获得 Content-Type 对应的 http_ctype 对象
http_mime* mime = req.getHttpMime();
if (mime == NULL)
{
logger_error("http_mime null");
return false;
}
// 获得数据体的长度
long long int len = req.getContentLength();
if (len <= 0)
{
logger_error("body empty");
return false;
}
// 获得输入流
istream& in = req.getInputStream();
char buf[8192];
int ret;
bool finish = false;
const char* filepath = "./var/mime_file";
ofstream out;
out.open_write(filepath);
// 设置原始文件存入路径
mime->set_saved_path(filepath);
size_t k;
// 读取 HTTP 客户端请求数据
while (len > 0)
{
k = (size_t) len > sizeof(buf)
? sizeof(buf) : (size_t) len;
ret = in.read(buf, k, false);
if (ret == -1)
{
logger_error("read POST data error");
return false;
}
out.write(buf, ret);
len -= ret;
// 将读得到的数据输入至解析器进行解析
if (!finish && mime->update(buf, ret) == true)
finish = true;
}
out.close();
if (len != 0 || finish == false)
logger_warn("not read all data from client");
param1_ = req.getParameter("name1");
param2_ = req.getParameter("name2");
param3_ = req.getParameter("name3");
string path;
// 遍历所有的 MIME 结点,找出其中为文件结点的部分进行转储
const std::list<http_mime_node*>& nodes = mime->get_nodes();
std::list<http_mime_node*>::const_iterator cit = nodes.begin();
for (; cit != nodes.end(); ++cit)
{
const char* name = (*cit)->get_name();
if (name == NULL)
continue;
http_mime_t mime_type = (*cit)->get_mime_type();
if (mime_type == HTTP_MIME_FILE)
{
const char* filename = (*cit)->get_filename();
if (filename == NULL)
{
logger("filename null");
continue;
}
if (strcmp(name, "file1") == 0)
file1_ = filename;
else if (strcmp(name, "file2") == 0)
file2_ = filename;
else if (strcmp(name, "file3") == 0)
file3_ = filename;
// 有的浏览器(如IE)上传文件时会带着文件路径,所以
// 需要先将路径去掉
filename = acl_safe_basename(filename);
#ifdef WIN32
path.format("var\\%s", filename);
#else
path.format("./var/%s", filename);
#endif
(void) (*cit)->save(path.c_str());
//.........这里部分代码省略.........