POCO:如何在c ++中使用poco将图像上传到webser

时间:2012-12-20 08:39:30

标签: c++ image upload poco-libraries

我正在尝试将图像上传到远程Web服务器。我使用过HTMLForm和FilePartSource。我能够成功将图像上传到本地服务器(即loclhost),但是当我尝试将其上传到远程服务器时,从远程Web服务器收到的响应是“411 Length Required”。 我试图设置request.setContentLength(sizeofimagefile)但仍然是同样的问题。 任何人都可以指导我什么是问题或。 这是我的代码。

    HTMLForm htmlform;
htmlform.set("aaaaaa", "bbbbbbb");
htmlform.set("cccccc", "ddddddd");
htmlform.setEncoding(HTMLForm::ENCODING_MULTIPART);

PartSource * pFileSrc = new FilePartSource("filename", "application/octet-stream");

std::istream& mystream = pFileSrc->stream();
mystream.seekg(0, std::ios::end);
int uiLength = mystream.tellg();

    htmlform.addPart("file", pFileSrc);

URI uri("yyy");

    HTTPClientSession session(uri.getHost(), uri.getPort());        
HTTPRequest post_req(Poco::Net::HTTPRequest::HTTP_POST,"xxx",HTTPMessage::HTTP_1_1);        
post_req.setKeepAlive(true);
htmlform.prepareSubmit(post_req);


std::ostream& oustr = session.sendRequest(post_req);
htmlform.write(oustr);

HTTPResponse res;
std::istream& rs = session.receiveResponse(res);

std::cerr << rs.rdbuf(); 

提前致谢

2 个答案:

答案 0 :(得分:4)

std::ostream& oustr = session.sendRequest(post_req);
htmlform.write(oustr);

您的代码无法将表单数据分配到请求对象中。因此,当您调用session.sendRequest时,会向服务器发送一个空请求。要将HTMLForm正确转换为HTTPRequest,您必须这样写 -

  

htmlform.write(session.sendRequest(post_req));

对我有用的图片上传代码是 -

    HTTPRequest request(HTTPRequest::HTTP_POST, "/fileupload/upload_file.php",    HTTPMessage::HTTP_1_1);
    HTMLForm form;
    form.setEncoding(HTMLForm::ENCODING_MULTIPART);
    form.set("entry1", "value1");
    form.set("entry2", "value2");
    form.addPart("file", new FilePartSource("/home/abc/Pictures/sample.png"));
    form.prepareSubmit(request);

    HTTPClientSession *httpSession = new HTTPClientSession("localhost");
    httpSession->setTimeout(Poco::Timespan(20, 0));
    form.write(httpSession->sendRequest(request));        

    Poco::Net::HTTPResponse res;
    std::istream &is = httpSession->receiveResponse(res);
    Poco::StreamCopier::copyStream(is, std::cout);

相应的上传服务器使用标准PHP代码上传HTML表单文件。

答案 1 :(得分:0)

如果您可以将文件上传到本地服务器但不能上传到远程服务器,首先应检查您的远程Apache / PHP是否有上传限制。尝试远程服务器中的phpinfo()。

http://www.cyberciti.biz/faq/linux-unix-apache-increase-php-upload-limit/

如果没有,你应该修改你的代码......

来自Poco文档,网址: http://www.appinf.com/docs/poco/Poco.Net.HTMLForm.html

  

HTMLForm:

     

HTMLForm(const HTTPRequest&amp; request,std :: istream&amp;   requestBody);

     

根据给定的HTTP请求创建HTMLForm。 上传的文件是   默默地放弃

使用这个构造函数:

  

HtmlForm控件:

     

HTMLForm(const HTTPRequest&amp; request,std :: istream&amp; requestBody,PartHandler&amp; handler);

     

根据给定的HTTP请求创建HTMLForm。 上传的文件将传递到给定的PartHandler

在您的示例中,您正在应用什么构造函数?

另一方面,

  

addPart:

     

void addPart(       const std :: string&amp;名称,       PartSource * pSource);将部件/附件(文件上载)添加到表单。表单取得PartSource的所有权并删除它   什么时候不再需要它。 该部分仅在发送时才会发送   表单的编码集是“multipart / form-data”

尝试将“multipart / form-data”与addPart和HTMLForm的第二个构造函数一起使用。

如果它不起作用,请尝试使用Wireshark之​​类的网络嗅探器来检查您发送的内容。

检查您的请求的Content-Length标头是否具有sizeof(您的图片)+ sizeof(“aaaaaa”和“cccccc”参数)。或尝试使用GET方法发送您的表单POST。

让我知道它是否有效。

此致