OpenResty文件上传用例

OpenResty文件上传,支持java和前端上传

安装OpenResty

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
-- relname是相对路径文件名,创建不存在路径
function file_exists(relname)
        local path = saveRootPath .. relname
      --  local abspath=string.match(path, "(.+)/[^/]*%.%w+$")
        local abspath
            if  string.match(path, "(.+)/[^/]*%.%w+$") ~= nil then
                 abspath=string.match(path, "(.+)/[^/]*%.%w+$")
            else
                abspath= path
            end
        testfile,errs = io.open(abspath,"rb")
        if not testfile then
                ngx.log(ngx.ERR,errs)
                local md="mkdir -p "
                local mdpath=md .. abspath
                ngx.log(ngx.ERR,"not exist! start mkdir "..abspath)
                os.execute(mdpath)
         else
                testfile:close()
        end
end



-- 保存的文件对象
local fileToSave
--文件是否成功保存
local ret_save = false
local ret_str
local ret_key
local filepathname
while true do
    local typ, res, err = form:read()
    local ret_name_flag=true
    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]
        -- ret_key=value
        if key == "Content-Disposition" then
            -- 解析出本次上传的文件名
            -- form-data; name="testFileName"; filename="testfile.txt"
            -- form-data; name=
            local kvlist = string.split(value, ';')
            for _, kv in ipairs(kvlist) do
                local seg = string.trim(kv)
                if seg:find("name") == 1 then
                   local kvfile2 = string.split(seg, "=")
                    local filename = string.sub(kvfile2[2], 2, -2)
                   if string.match(filename, "(.+)/[^/]*%.%w+$") ~=  nil then
                       file_exists(filename)    -- 判断子文件夹是否存在,不存在创建
                       filepathname = filename
                     ret_key=filepathname
                    elseif filename:match("(.+)/[^/]*/$") ~= nil  then
                       file_exists(filename)    -- 判断子文件夹是否存在,不存在创建
                       filepathname = filename
                       ret_key=filepathname
                   elseif  filename:match("(.+)/[^/]*%w+$") ~= nil   then
                       file_exists(filename .. "/")    -- 判断子文件夹是否存在,不存在创建
                       filepathname = filename .. "/"
                       ret_key=filepathname .. "/"
                   else
                     ngx.log(ngx.INFO, "get file path!!")
                   end

                 end
                if seg:find("filename")  then
                    local kvfile = string.split(seg, "=")
                    local filename
                     if filepathname ~= nil then
                         if string.match(filepathname, "(.+)/[^/]*%.%w+$") then
                            filename = filepathname
                         else
                            filename = filepathname .. string.sub(kvfile[2], 2, -2)
                            filepathname = filename
                         end
                      else
                        filename=string.sub(kvfile[2], 2, -2)
                        filepathname = filename
                      end
                   -- ret_key=filepathname
                     file_exists(filename)    -- 判断子文件夹是否存在,不存在创建
                     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("",filepathname)
end

 

测试用例:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小努蛋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值