这里前面说过了通过请求路径转到处理上传文件的处理中去,直接讲处理上传请求的内容。
这里把内容转到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;
}
}
}