lua文件上传直连

lua文件上传直连,第1张

lua文件上传直连:

nginx配置

注意:

文件存储路径

指定上传逻辑代码路径

在nginx上添加一个server

#上传文件服务

server

{

listen 19999

set $store_dir "/data/vue/fffoa/"# 文件存储路径

location /upfile {

content_by_lua_file conf/lua/upload.lua# 实现文件上传的逻辑

}

location /download {

autoindex on

autoindex_localtime on

alias /data/vue/fffoa/

index index.html

}

access_log logs/uploadfile_access.log main

error_log logs/uploadfile_error.log crit

}

登录后复制

实现上传逻辑代码 conf/lua/upload.lua

注意:我使用的是openresty,可以直接引用resty.upload等lua库,如果你是nginx,还需要找到upload.lua、cjson库

-- 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-dataname="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

登录后复制

测试上传文件

在postman里面写上 地址:19999/upload/

配置好文件,选发送,上传成功显示 save file ok

查看服务器内容

CSDN_码404:使用openresty的lua-resty-upload实现文件上传

nginx

lua

点赞文章给优秀博主打call~

后端的责任。

前端上传文件实时显示进度条和上传速度的工作原理就是后端的责任,在Django中实现需要重载上传文件的函数,在上传时文件是老桥被分成数个MB的chunk处理的,每次都会调用这个上传函数。也就是说,每处理一个chunk就更新uploadedsize,然后浏览器端通过AJAX获取这个值和文件大小

最后用JavaScript渲染到页面上。

前端只能说会用框架和插件干活。前段时间用的百度的webuploader,demo就带进度码前条的。js代码不多可以看一下,猜测是监听事件。上传是前端和通信协议做的事,后端是写入。在比较传统流和和spring自带的transferto的耗时统称上传时间是不对的,应为写入时间。

项目框架采用spring+hibernate+springMVC如果上传文件不想使用flash那么你可以采用html5截图前段模块是bootstarp框架不废话直接来代码spring-mvc配置文件。

nginx话lua可以拿到链接的套接口,读取套接口就可以知道当侍模猛前上传了多少了。可以看下openresty的lualib/resty/upload.lua。

介绍

在使用markdown格式的过程中,经常需要上传图片,但是常液镇常很复杂,image,在csdn上也很麻烦,在我有阿里闹液粗云的情况下,用nginx实现我的图片服务器.

安装 OpenResty

OpenResty,以前用过,所以就按照文档快速安装.

apt-get install libpcre3-dev libssl-dev perl make build-essential curl

./configure

make

make install

默认目录 :/usr/local/openresty/

添加配置文件

cd /usr/local/openresty/

mkdir conf/

vi nginx.conf

配置文件具体内容

worker_processes 1

error_log logs/error.log

events {

worker_connections 1024

}

http {

server {

listen 8080

location ~ .*.(gif|jpg|jpeg|png)$ {

expires 24h

root /home/images/#指定图片存放路径

access_log /home/nginx/logs/images.log#图片 日志路径

proxy_store on

proxy_store_access user:rw group:rw all:rw

proxy_temp_path /home/images/#代理临时路径

proxy_redirect off

}

启动

./openresty -c ../conf/nginx.conf

./openresty -s stop

netstat -antp

x

image

通过 SecureCRT 7.0拖拽上传文件

image

通过 get -r * 同步文件到本地

image

访问埋烂即可

https://blog.csdn.net/better_mouse/java/article/details/84256664


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12301224.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存