文件上传的几种不同语言和不同方法的总结。


第一种模式 : PHP 语言来处理

这个模式比较简单, 用的人也是最多的, 类似的还有用 .net 来实现, jsp来实现, 都是处理表单。只有语言的差别, 本质没有任何差别。

file.php 文件内容如下 :

<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>

测试命令 :

curl   -F   "action=file.php"   -F   "file=@xxx.c"   http://192.168.1.162/file.php

这样就可以把本地文件 xxx.c  通过表单的形式提交到服务器, file.php文件就会处理该表单。

第二种模式: lua 语言来处理

这种模式需要用  ngx_lua 模块的支持, 你可以直接下载  ngx_openresty  的源码安装包, 该项目由春哥负责。

春哥为了处理 文件上传, 还专门写了个lua的  upload.lua 模块。

网址为   https://github.com/agentzh/lua-resty-upload    大家可以下载, 里面只用到 upload.lua 文件即可, 把这个文件放到

/usr/local/openresty/lualib/resty/  这个目录即可(该目录是缺省安装的目录, ./configure  --prefix=/usr  可以改变目录)

下来写一个 savefile.lua 的文件来处理上传上来的文件, 文件内容如下 :

package.path = '/usr/local/share/lua/5.1/?.lua;/usr/local/openresty/lualib/resty/?.lua;'
package.cpath = '/usr/local/lib/lua/5.1/?.so;' local upload = require "upload" local chunk_size = 4096
local form = upload:new(chunk_size)
local file
local filelen=0
form:set_timeout(0) -- 1 sec
local filename function get_filename(res)
local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)')
if filename then
return filename[2]
end
end local osfilepath = "/usr/local/openresty/nginx/html/"
local i=0
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
if res[1] ~= "Content-Type" then
filename = get_filename(res[2])
if filename then
i=i+1
filepath = osfilepath .. filename
file = io.open(filepath,"w+")
if not file then
ngx.say("failed to open file ")
return
end
else
end
end
elseif typ == "body" then
if file then
filelen= filelen + tonumber(string.len(res))
file:write(res)
else
end
elseif typ == "part_end" then
if file then
file:close()
file = nil
ngx.say("file upload success")
end
elseif typ == "eof" then
break
else
end
end
if i==0 then
ngx.say("please upload at least one file!")
return
end

我把上面这个 savefile.lua 文件放到了  nginx/conf/lua/ 目录中

nginx.conf 配置文件中添加如下的配置 :

        location /uploadfile
{
content_by_lua_file 'conf/lua/savefile.lua';
}

用下面的上传命令进行测试成功

curl   -F   "action=uploadfile"   -F   "file=@abc.zip"   http://127.0.0.1/uploadfile

第三种模式: perl 语言来处理

编译 nginx 的时候 用  --with-http_perl_module 让其支持perl脚本

然后用perl来处理表单, 这种模式我还没做测试, 如果有那位弟兄试验过, 帮我补充一下。

下面的代码为 PERL 提交表单的代码:

use strict;
use warnings;
use WWW::Curl::Easy;
use WWW::Curl::Form; my $curl = WWW::Curl::Easy->new;
my $form = WWW::Curl::Form->new; $form->formaddfile("11game.exe", 'FILE1', "multipart/form-data");
# $form->formadd("FIELDNAME", "VALUE");
$curl->setopt(CURLOPT_HTTPPOST, $form); $curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, 'http://127.0.0.1/uploadfile'); # A filehandle, reference to a scalar or reference to a typeglob can be used here.
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body); # Starts the actual request
my $retcode = $curl->perform; # Looking at the results...
if ($retcode == 0)
{
print("Transfer went ok\n");
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# judge result and next action based on $response_code
print("Received response: \n$response_body\n");
}
else
{
# Error code, type of error, error message
print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}

服务器端的代码我不是用perl CGI, 而是嵌入nginx 的 perl脚本, 所以处理表单的代码还没有测试通过,等有时间了在研究一下。

第四种模式:用 http 的dav 模块的 PUT 方法

编译 nginx 的时候 用 --with-http_dav_module 参数让其支持 dav 模式

nginx.conf文件中配置如下 :

        location / {
client_body_temp_path /usr/local/openresty/nginx/html/tmp;
dav_methods PUT DELETE MKCOL COPY MOVE;
create_full_put_path on;
dav_access group:rw all:r;
root html;
#index index.html index.htm;
}

用下面的命令进行测试可以成功 :

curl  --request   PUT   --data-binary "@11game.exe"   --header "Content-Type: application/octet-stream"    http://127.0.0.1/game.exe

其中11game.exe为上传的本地文件。

只有第四种是 PUT 方法, 其他的三种都属于  POST 表单的方法。

让nginx支持文件上传的几种模式的更多相关文章

  1. 文件上传的三种模式-Java

    文件上传的三种方式-Java 前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方 ...

  2. Openresty + nginx-upload-module支持文件上传

    0. 说明 这种方式其实复杂,麻烦!建议通过这个方式搭建Openresty文件上传和下载服务器:http://www.cnblogs.com/lujiango/p/9056680.html 1. 包下 ...

  3. RPC基于http协议通过netty支持文件上传下载

    本人在中间件研发组(主要开发RPC),近期遇到一个需求:RPC基于http协议通过netty支持文件上传下载 经过一系列的资料查找学习,终于实现了该功能 通过netty实现文件上传下载,主要在编解码时 ...

  4. nginx实现文件上传和下载

    nginx实现文件上传和下载 发布时间:2020-06-05 16:45:27 来源:亿速云 阅读:156 作者:Leah 栏目:系统运维 这篇文章给大家分享的是nginx实现文件上传和下载的方法.小 ...

  5. java nio 写一个完整的http服务器 支持文件上传 chunk传输 gzip 压缩 使用过程 和servlet差不多

    java nio 写一个完整的http服务器  支持文件上传   chunk传输    gzip 压缩      也仿照着 netty处理了NIO的空轮询BUG        本项目并不复杂 代码不多 ...

  6. curl文件上传有两种方式,一种是post_fileds,一种是infile

    curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄! );curl_setopt($ch, CURLOPT_PO ...

  7. springmvc学习笔记--支持文件上传和阿里云OSS API简介

    前言: Web开发中图片上传的功能很常见, 本篇博客来讲述下springmvc如何实现图片上传的功能. 主要讲述依赖包引入, 配置项, 本地存储和云存储方案(阿里云的OSS服务). 铺垫: 文件上传是 ...

  8. 设置nginx中文件上传的大小限制度

    通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题: 用nginx来做webserver的时,上传大文件时需要特别注意client_max_body_s ...

  9. 让UpdatePanel支持文件上传(2):服务器端组件 .

    我们现在来关注服务器端的组件.目前的主要问题是,我们如何让页面(事实上是ScriptManager控件)认为它接收到的是一个异步的回送?ScriptManager控件会在HTTP请求的Header中查 ...

随机推荐

  1. swift 遍历

    最简单的一个遍历数组 for 随便起个名字 in 升级 上面的看不懂的话,这个应该会简单点 import UIKit let interestingNumbers = [ ,,,,,], ,,,,,] ...

  2. 11-17的学习总结(DOMfirstday)

    HTML: 超文本标记语言,专门定义网页内容的语言 XHTML: 严格的HTML标准 DHTML: 所有实现网页动态效果技术的统称 XML: 可扩展的标记语言,标签都是自定义的 XML语法和HTML语 ...

  3. POJ 2049 Finding Nemo bfs 建图很难。。

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6952   Accepted: 1584 Desc ...

  4. OGNL-action

    需要注意的是,action需要先被调用到,OGNL才能成功,因为action被执行才被压入值栈 package com.wolfgang.action; import com.opensymphony ...

  5. Phonegap 3.0 获取当前地址位置

    新版本的cordova 3.0 中,使用官方的示例可直接获取当前手机的地理位置,前提是手机开启了gps,或可联网. 获取到的是经纬度坐标值等信息,可通过google api 实现通过经纬度获取当前地理 ...

  6. QiQi and Symmerty

    http://sdu.acmclub.com/index.php?app=problem_title&id=961&problem_id=23772 题意:给出一个01串,问有多少个子 ...

  7. codeforces C. Painting Fence

    http://codeforces.com/contest/448/problem/C 题意:给你n宽度为1,高度为ai的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...

  8. python包管理-distutils,setuptools,pip,virtualenv等介绍

    python包管理-distutils,setuptools,pip,virtualenv等介绍 对于每个编程语言来说打包和发布开发包往往非常重要,而作为一个编程者能够快速容易的获得并应用这些由第三方 ...

  9. 测试WWW方案(反向代理,负载均衡,HTTP加速缓存)

    大约图如下: NGINX FRONT(80)--->VARNISH(8080)---->LNMP BACKEND 1(80) |--->LNMP BACKEND 2(80) 主要是前 ...

  10. I2C总线之(一)---概述

    概述: I²C 是Inter-Integrated Circuit的缩写,发音为"eye-squared cee" or "eye-two-cee" , 它是一 ...