zoukankan      html  css  js  c++  java
  • Openresty配置文件上传下载

    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/

  • 相关阅读:
    Linux的上的MongoDB的安装与卸载
    MongoDB常用操作
    scrapy 爬网站 显示 Filtered offsite request to 错误.
    在linux系统下把多个终端合并在一个窗口
    安装python爬虫scrapy踩过的那些坑和编程外的思考
    大规模爬虫流程总结
    Python的35种“黑魔法”级别技巧!
    2019/2/13 Python今日收获
    2019/2/12 Python今日收获
    2019/1/22 Python今日收获
  • 原文地址:https://www.cnblogs.com/lujiango/p/9056680.html
Copyright © 2011-2022 走看看