local upload = require "resty.upload"local uuid = require "resty.uuid";local chunk_size = 4096local form = upload:new(chunk_size)local file--获取文件名function get_filename(res) local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)') if filename then return filename[2] end end --获取文件扩展名function getExtension(str)return str:match(".+%.(%w+)$")endwhile true dolocal typ, res, err = form:read() if not typ then ngx.say("{error:'error', msg:'"..tostring(err).."',imgurl:''}")returnendif typ == "header" thenif res[1] ~= "Content-Type" thenlocal file_id = uuid.new()local filen_ame = get_filename(res[2])local extension = getExtension(filen_ame)local dir = string.sub(file_id,0,2) local file_name = "/usr/local/tomcat7/webapps/dsideal_yy/html/down/Material/"..dir.."/"..file_id.."."..extension if file_name thenfile = io.open(file_name, "w+")if not file thenngx.say("{error:'error', msg:'failed to open file',imgurl:''}")returnendendendelseif typ == "body" thenif file thenfile:write(res) endelseif typ == "part_end" thenfile:close()file = nilelseif typ == "eof" thenbreakelse-- do nothingendendngx.say("{error:'success', msg:'上传成功!',imgurl:''}")