利用Socket提交文件到web server

简介:

    前一段时间开发一个WebDAV的客户端,主要是接受发送文件。因为只需要上传下载文件,所以不想使用curl这样重量级的库,所以,基于以上原因自己编写封装一个类,利用socket完成对文件的上传和下载,还可以使用内核函数sendfile实现文件上传时的“零拷贝”。在本例中,同样也给出使用curl方式对文件进行上传操作的方法。针对WebDav的开发还有一个专门的库neon,同样这里也给出使用neon库完成文件上传操作的方法。

    在上传过程中会把大于2M的文件,按2M发送,以防止,当上传文件过大时并且数量过多时,不能及时对其他非文件处理操作请求做出处理。

具体项目说明如下

    USocket类(USocket.hUSocket.cc),创建到远程Web server的本地socket连接,它需要远程的hostport参数,这俩个参数可以从UUri类中获得。

    UUri(UUri.hUUri.cc),传递给该类一个URL,比如http://www.exuetang.net:8080/test/test.jpg,该类会把此URL分解为scheme,host,port,path等信息

    Putfile类(Putfile.hPutfile.cc,上传文件操作类,通过putfile方法,需要webPath和本地文件的path,返回操作状态,以及文件大小。

调用方法

#include 
using namespace::std;
 
#include "Putfile.h"

#define FILENAME "/tmp/file/1.8m.JPG"
 
using namespace::ucs;
 
/* 上传文件 */
void putfile(const string& webPath, const string& filename)
{
    Putfile client;
    int fileSize;
    // 上传文件
    UState state = client.putfile(webPath,filename,fileSize);
    if(state == U_OK)
        cout << filename << " -- OK, file size  " << fileSize << endl;
    else
        cout << filename << " -- fail" << state << endl;
}

int
main ( int argc, char *argv[] )
{
 
    if(argc != 3)
    {
        cout << "./Client method /n";
        cout << "   ./Client webPath fileName/n";
        return 0;
    }else{
        string strWeb = argv[1];
        string strFile = argv[2];
        putfile(strWeb,strFile);
    }
    return EXIT_SUCCESS;
}
执行make命令,然后

./Client http://www.xxx.com/test.dat test.dat

输出如下的log

Putfile.cc/_sectsendfile[348]: end recv 3784
Putfile.cc/_sectsendfile[351]: send file size: 114688
Putfile.cc/putfile[137]: put file OK
Putfile.cc/putfile[140]: send size 114688| file size 160650
USocket.cc/~USocket[33]: USocket::~USocket()
UUri.cc/~UUri[28]: UUri::~UUri
/tmp/example/abc.jpg -- OK, file size  160650
Putfile.cc/~Putfile[52]: Putfile::~Putfile()

如果不使用sendfile,使用neon库或者curl库,需要修改Makefile文件,增加-lneon或者-lcurl库,同时修改Macros.h文件。如果不想显示日志,注释掉Logging.h文件中的

#define DEBUG_MODE

即可, 具体操作参考源代码。
点击下载

目录
相关文章
|
2月前
|
数据可视化 Shell Linux
shell+crontab+gitlab实现ecs服务器文件的web展示
本文通过把ecs服务器上的文件定时上传至gitlab,实现文件的页面可视化和修改历史。技术点:shell、crontab、gitlab。
51 3
|
3月前
|
IDE Linux 开发工具
如何在Linux运行RStudio Server并实现Web浏览器远程访问
如何在Linux运行RStudio Server并实现Web浏览器远程访问
74 0
|
2月前
|
应用服务中间件 nginx
【报错】Failed to start A high performance web server and a reverse proxy server.
【报错】Failed to start A high performance web server and a reverse proxy server.
|
3月前
|
Linux
【web server】基于升序链表的定时器
【web server】基于升序链表的定时器
|
1月前
|
弹性计算 算法 应用服务中间件
倚天使用|Nginx性能高27%,性价比1.5倍,基于阿里云倚天ECS的Web server实践
倚天710构建的ECS产品,基于云原生独立物理核、大cache,结合CIPU新架构,倚天ECS在Nginx场景下,具备强大的性能优势。相对典型x86,Http长连接场景性能收益27%,开启gzip压缩时性能收益达到74%。 同时阿里云G8y实例售价比G7实例低23%,是Web Server最佳选择。
|
1月前
|
Windows
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
57 2
|
1月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
128 1
|
2月前
|
存储 数据库 Python
Django教程第6章 | web开发实战-文件上传(导入文件、上传图片)
web应用实战:导入文件解析到DB,上传图片【2月更文挑战第25天】
56 0
Django教程第6章 | web开发实战-文件上传(导入文件、上传图片)
|
3月前
|
XML C++ 数据格式
C++使用gSoap写Web Server和Web Client
C++使用gSoap写Web Server和Web Client
36 1
|
3月前
【web server】HTTP协议如何解析or封装
【web server】HTTP协议如何解析or封装