使用Nginx实现文件上传,或文件断点上传

本文介绍如何使用Nginx实现文件上传功能,包括断点续传,并详细配置了Nginx与Spring MVC的整合方式。同时展示了如何利用jQuery简化前端操作及后端Java代码如何处理上传的数据。

使用Nginx实现文件上传,或文件断点上传

使用技术:nginx ,spring mvc, nginx upload module,jquery,jquery-ajax-form

前提:nginx编译时需要将nginx upload module 编译进去

ngxin 配置:

location ^~ /deltaManager/delta/upload/  {
            upload_store    /data01/nginx/file ;
            upload_pass /deltaManagerDeltaUpload; # 转发到 【location /deltaManagerDeltaUpload】
            add_header From localhost;
            proxy_redirect  off;
            proxy_set_header   Host   $host;
            proxy_set_header   Referer $http_referer;
            proxy_set_header   Cookie $http_cookie;
            proxy_set_header   X-Real-IP  $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            upload_max_file_size 3000m;
            upload_store_access user:rw group:rw all:rw;
            upload_set_form_field "fileName" $upload_file_name; # 文件原名
            upload_set_form_field "filename_content_type" $upload_content_type; # 文件类型
            upload_set_form_field "filePath" $upload_tmp_path; # 文件临时保存路径
            upload_aggregate_form_field "filename_md5" $upload_file_md5; # 文件md5
            upload_aggregate_form_field "fileSize" $upload_file_size; # 文件大小,filename_size为转发到后端(java)的参数。
            upload_pass_form_field "^.*$"; #转发之前表单的全部参数
            upload_limit_rate 128k;
            upload_pass_args on;
            upload_cleanup 400 404 499 500-505;
            track_uploads proxied 30s;
            chunked_transfer_encoding       off;
        }
        
location /deltaManagerDeltaUpload {
       proxy_pass http://127.0.0.1:8019/fota-car-manager/deltaManager/upload.do; # 后端接收上传信息的实际接口
       proxy_redirect  off;
       proxy_set_header   Host   $host;
       proxy_set_header   Referer $http_referer;
       proxy_set_header   Cookie $http_cookie;
       proxy_set_header   X-Real-IP  $remote_addr;
       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

    }
        

后端接收参数: 因为使用spring,所有接收参数比servlet 或者 struts 稍微简单。 直接在controller层中的方法中,使用参数接收 nginx 转发过来的参数(网上 servlet 解析参数,还需要通过解析流来获取数据,太麻烦) 参考代码:

    @RequestMapping(value = "/upload.do")
    @ResponseBody
    public Map<String, Object> upload(String projectId, String filePath, String fileSize, String fileName) {
        logger.info("nginx 中转上传文件");
        logger.info("deltaId:" + deltaId);
        logger.info("projectId:" + projectId);
        logger.info("filename_path:" + filePath);
        logger.info("filename_size:" + fileSize);
    return null;
    }
    
    

处理流程:

  1. 将 nginx 中临时保存的文件,复制到指定文件夹。
  2. 业务处理
  3. 将 nginx 中临时保存的文件删除

转载于:https://my.oschina.net/shichangcheng/blog/871413

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值