1. 下载包安装Openresty

openresty-1.13.6.1下载地址 https://openresty.org/download/openresty-1.13.6.1.tar.gz

安装请自行百度。

2. 配置

2.1 nginx.conf

user root;
worker_processes 20; error_log logs/error.log notice; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream;
server {
listen 8082;
server_name localhost;
# 最大允许上传的文件大小
client_max_body_size 200m; location / {
root html;
index index.html index.htm;
}
set $store_dir "/sdf/slb/openresty/nginx/html/download/"; # 文件存储路径
# 文件上传接口:http://xxx:8082/upfile
location /upfile {
content_by_lua_file conf/lua/upload.lua; # 实现文件上传的逻辑
}
# 文件下载入口: http://xxx:8082/download
location /download {
autoindex on;
autoindex_localtime on;
root html;
index index.html;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

  

2.2 upload.lua(文件位于conf/lua/upload.lua)

-- upload.lua
--==========================================
-- 文件上传
--==========================================
local upload = require "resty.upload"
local cjson = require "cjson"
local chunk_size = 4096
local form, err = upload:new(chunk_size)
if not form then
ngx.log(ngx.ERR, "failed to new upload: ", err)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
form:set_timeout(1000)
-- 字符串 split 分割
string.split = function(s, p)
local rt= {}
string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
return rt
end
-- 支持字符串前后 trim
string.trim = function(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- 文件保存的根路径
local saveRootPath = ngx.var.store_dir
-- 保存的文件对象
local fileToSave
--文件是否成功保存
local ret_save = false
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "header" then
-- 开始读取 http header
-- 解析出本次上传的文件名
local key = res[1]
local value = res[2]
if key == "Content-Disposition" then
-- 解析出本次上传的文件名
-- form-data; name="testFileName"; filename="testfile.txt"
local kvlist = string.split(value, ';')
for _, kv in ipairs(kvlist) do
local seg = string.trim(kv)
if seg:find("filename") then
local kvfile = string.split(seg, "=")
local filename = string.sub(kvfile[2], 2, -2)
if filename then
fileToSave = io.open(saveRootPath .. filename, "w+")
if not fileToSave then
ngx.say("failed to open file ", filename)
return
end
break
end
end
end
end
elseif typ == "body" then
-- 开始读取 http body
if fileToSave then
fileToSave:write(res)
end
elseif typ == "part_end" then
-- 文件写结束,关闭文件
if fileToSave then
fileToSave:close()
fileToSave = nil
end ret_save = true
elseif typ == "eof" then
-- 文件读取结束
break
else
ngx.log(ngx.INFO, "do other things")
end
end
if ret_save then
ngx.say("save file ok")
end

  

3. 测试

3.1 启动openresty

sbin/nginx

3.2 上传文件

通过地址http://192.168.23.65:8082/upfile上传文件。

3.3 下载文件

通过http://192.168.23.65:8082/download下载文件。

x. 参考资料

http://www.codexiu.cn/nginx/blog/11024/

Openresty配置文件上传下载的更多相关文章

  1. EasyNVR摄像机网页Chrome无插件视频播放功能二次开发之通道配置文件上传下载示例代码

    背景需求 熟悉EasyNVR产品的朋友们都知道,产品设计初期根据整个直播流程层级,我们将EasyNVR无插件直播系统划分为:硬件层.能力层.应用层,连接硬件与应用之间的桥梁,同时屏蔽各种厂家硬件的不同 ...

  2. springboot三种配置文件上传下载大小的配置

    配置文件为application.yml格式: spring: http: multipart: enabled: true max-file-size: 30MB max-request-size: ...

  3. win7下利用ftp实现华为路由器的配置文件上传和下载

    win7下利用ftp实现华为路由器的配置文件上传和下载 1.  Win7下ftp的安装和配置 (1)开始—>控制面板—>程序—>程序和功能—>打开或关闭Windows功能 (2 ...

  4. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  5. 基于Spring Mvc实现的Excel文件上传下载

    最近工作遇到一个需求,需要下载excel模板,编辑后上传解析存储到数据库.因此为了更好的理解公司框架,我就自己先用spring mvc实现了一个样例. 基础框架 之前曾经介绍过一个最简单的spring ...

  6. 用Canvas+Javascript FileAPI 实现一个跨平台的图片剪切、滤镜处理、上传下载工具

    直接上代码,其中上传功能需要自己配置允许跨域的文件服务器地址~ 或者将html文件贴到您的站点下同源上传也OK. 支持: 不同尺寸图片获取. 原图缩小放大. 原图移动. 选择框大小改变. 下载选中的区 ...

  7. 艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输)(一)

    艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输) 该系统基于开源的networkComms通讯框架,此通讯框架以前是收费的,目前已经免费并开元,作者是英国的,开发时间5年多,框架很稳定. 项 ...

  8. Webwork 学习之路【07】文件上传下载

    Web上传和下载应该是很普遍的一个需求,无论是小型网站还是大并发访问的交易网站.WebWork 当然也提供了很友好的拦截器来实现对文件的上传,让我们可以专注与业务逻辑的设计和实现,在实现上传和下载时顺 ...

  9. .NET两种常见上传下载文件方法

    1.FTP模式 代码如下: (1)浏览 /// <summary> /// 浏览文件 /// </summary> /// <param name="tbCon ...

随机推荐

  1. javascript实现原生ajax

    自从javascript有了各种框架之后,比如jquery,使用ajax已经变的相当简单了.但有时候为了追求简洁,可能项目中不需要加载jquery这种庞大的js插件.但又要使用到ajax这种功能该如何 ...

  2. GDB的一些技巧

    查看栈信息 bt info stack 查看源程序 list ctrl + x + a 分屏调试,上半部分显示代码,下半部分显示调试信息. 查看内存 p  xxxptr@n  查看xxxptr 指针内 ...

  3. [转]CodeSmith 基础教程

    本文转自:http://www.cnblogs.com/sorex/archive/2009/12/24/1631533.html 〇.            前言 最近两天自己写了个简单的ORM框架 ...

  4. github之无命令可视化界面操作——GitHub DeskTop

    Git是Linuxs之父Lunus用C语言写的一个非常好用的分布式版本控制系统. GitHub可以给我们提供免费的代码仓库,并用Git可以在上面提交代码并进行版本控制.使用Git一般要安装Git ,并 ...

  5. iOS: 在Xcode7系列中给类名自动添加前缀

    添加前缀原因: 我个人还是很喜欢 Class Prefix 的,因为: 1. 使用它几乎不增加什么成本, 2. 不使用它可能会造成安全隐患, 3. 它能够以直接的方式编码一些信息以供未来的回溯, 4. ...

  6. Windows系统下将目录挂载为一个磁盘并分配盘符

    Windows系统下subst可以临时将目录分配一个盘符. 将路径与驱动器号关联. SUBST [drive1: [drive2:]path]SUBST drive1: /D drive1: 指定要分 ...

  7. java匿名内部类之RocketMQ中的应用

    匿名内部类在spring中没怎么见用,在RocketMQ中有大量的应用. 确实可以提高开发效率.这可能代表两种写代码的态度吧. 匿名内部类简单来说就是直接在函数中实现接口方法,不需要声明一个接口实现类 ...

  8. Rails零散知识

    1. 邮箱验证VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i validates :email, pres ...

  9. 【菜鸟也疯狂UML系列】——浅析UML四种关系

    在UML中.关系是很重要的.它抽象出对象之间的联系,让对象构成某个联系起来的结构.以下将简要分析一下UML中的四种关系:关联.依赖,泛化,实现. 一.举例罗列 1.关联(Association) 关联 ...

  10. Ubuntu 下安装adobe reader

    ctrl+alt+t打开终端 wget ftp://ftp.adobe.com/pub/adobe/reader/unix/9.x/9.5.5/enu/AdbeRdr9.5.5-1_i386linux ...