poco 服务端接收来自客户端上传的文件

这里前面说过了通过请求路径转到处理上传文件的处理中去,直接讲处理上传请求的内容。

        这里把内容转到UploadFileHandler这个类里面。

void UploadFileRequestHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
	Application& app = Application::instance();
	//app.logger().information("Request from " + request.clientAddress().toString());

	UploadFileHandler partHandler;
	HTMLForm form(request, request.stream(), partHandler);
	Poco::JSON::Object object(Poco::JSON_PRESERVE_KEY_ORDER);
	object.set("method", "UploadFile");
	if (partHandler.Sucecss())
	{
		object.set("errcode", "0");
		object.set("errmsg", "null");
		response.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
	}
	else
	{
		object.set("errcode", "30");
		object.set("errmsg", partHandler.Erro());
		response.setStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
	}
	std::ostringstream ostr_stream;
	object.stringify(ostr_stream);
	std::ostream& out = response.send();
	out << ostr_stream.str();
}

        处理上传的类UploadFileHandler代码

class UploadFileHandler : public Poco::Net::PartHandler
{
public:
	UploadFileHandler();
	//处理下载文件
	void handlePart(const MessageHeader& header, std::istream& stream);

private:
	void createDirectoryIfNotExists(std::string dirPath);
public:

	int length() const;
	const std::string& name() const;
	const std::string& fileName() const;
	const std::string& contentType() const;
	const bool Sucecss() const;
	const std::string Erro() const;
private:
	int _length;
	std::string _type;
	std::string _name;
	std::string _fileName;
	std::string _path;
	bool _bSucecss;
	std::string _strErro;
};

#include "UploadFileHandler.h"
#include <Poco/FileStream.h>  
#include <iostream>  
#include <filesystem>  
using Poco::Net::NameValueCollection;
UploadFileHandler::UploadFileHandler() :
	_length(0),
	_bSucecss(false),
	_strErro(""),
	_path("")
{

}

void UploadFileHandler::handlePart(const MessageHeader& header, std::istream& stream)
{
	_type = header.get("Content-Type", "(unspecified)");
	if (header.has("Content-Disposition"))
	{
		std::string disp;
		NameValueCollection params;
		MessageHeader::splitParameters(header["Content-Disposition"], disp, params);
		_path = params.get("path", "(unnamed)");
		_fileName = params.get("filename", "(unnamed)");
		
	}
	if ("unnamed" == _path)
	{
		_bSucecss = false;
		return;
	}
	if ("unnamed" == _fileName)
	{
		_bSucecss = false;
		return;
	}
	try
	{
		// 打开文件进行写入  
		createDirectoryIfNotExists(_path);
		Poco::FileOutputStream fileStream("./"+_path +"/" + _fileName);
		if (!fileStream) 
		{
			throw Poco::FileNotFoundException("File could not be opened: "+ _path + _fileName);
			_bSucecss = false;
			return;
		}
		fileStream << stream.rdbuf();
		// 关闭文件输出流  
		fileStream.close();
		_bSucecss = true;
	}
	catch (const Poco::Exception& ex)
	{
		_strErro = ex.displayText();
		std::cout<<ex.displayText()<<std::endl;
		_bSucecss = false;
	}
	


}

void UploadFileHandler::createDirectoryIfNotExists(std::string dirPath)
{
	std::filesystem::path path(dirPath);

	// 检查目录是否存在  
	if (!std::filesystem::exists(path)) 
	{
		// 尝试创建目录  
		if (std::filesystem::create_directories(path)) 
		{
			std::cout << "Directory created: " << dirPath << std::endl;
		}
		else 
		{
			std::cerr << "Failed to create directory: " << dirPath << std::endl;
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雁南1830

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值