通过纯Nginx实现一个简单的文件上传功能

需求想要上传文件到服务器,内部使用访问也不多,但是上传接口没有,又不想额外配置一个文件服务器,通过查阅资料发现Nginx自带文件上传功能。

1. 检查当前nginx是否包含 dav 模块

bash 复制代码
nginx -V 2>&1 | findstr "dav"

输入该命令,检查,如果有的话,就可以使用了。

2. 配置上传接口

bash 复制代码
    location /eventFileUpload {
        # 设置上传文件大小限制为 100M
        client_max_body_size 100M;
    
        # 指定文件保存路径(注意使用正斜杠:我用的是windows服务器)
        alias E:/xxx/xxx/xxx;
    
        # 开启目录浏览,方便查看已上传的文件
        autoindex on;
    
        # 启用 WebDAV 功能
        dav_methods PUT DELETE MKCOL COPY MOVE;
    
        # 允许创建多级目录
        create_full_put_path on; 
     }

3. 调用上传

java 复制代码
 public static void main(String[] args) {
        try {
            // File path to upload
            String filePath = "D:\\1.txt";
            File file = new File(filePath);
            
            if (!file.exists()) {
                System.out.println("File does not exist: " + filePath);
                return;
            }
            
            // Upload URL 注意这里哦,eventFileUpload 为nginx 配置的访问url。后面为文件地址,要放进来的服务器的文件地址,要加到这里,因为body只传了文件流,没有指定上传到哪个路径,需要在这里指定哦
            String uploadUrl = "http://xxx/xxx/xxx/eventFileUpload/222/1.txt";
            
            // Create OkHttp client
            OkHttpClient client = new OkHttpClient();
            
            // Create request body with file content
            okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(
                file, 
                MediaType.parse("application/octet-stream")
            );
            
            // Build PUT request
            Request request = new Request.Builder()
                .url(uploadUrl)
                .put(requestBody)
                .build();
            
            // Execute request
            System.out.println("Uploading file: " + filePath);
            System.out.println("To URL: " + uploadUrl);
            
            try (Response response = client.newCall(request).execute()) {
                if (response.isSuccessful()) {
                    System.out.println("Upload successful!");
                    System.out.println("Response code: " + response.code());
                    if (response.body() != null) {
                        System.out.println("Response body: " + response.body().string());
                    }
                } else {
                    System.out.println("Upload failed!");
                    System.out.println("Response code: " + response.code());
                    System.out.println("Response message: " + response.message());
                    if (response.body() != null) {
                        System.out.println("Response body: " + response.body().string());
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("Error during file upload: " + e.getMessage());
            e.printStackTrace();
        }
    }
相关推荐
Madison-No725 分钟前
搭建项目测试环境
linux·运维·服务器·python
吴声子夜歌1 小时前
Shell编程实例——编写安全的shell脚本(二)
linux·运维·shell
悠悠121382 小时前
微服务通信别只看性能:REST 与 gRPC 的 6 个选型判断和一次 502 故障复盘
运维·服务器
zx_741484814 小时前
【Linux入门】Shell 函数、正则表达式与文本处理:cut 与 awk
linux·运维·正则表达式
Zenova EdgeOS4 小时前
Linux dmesg 工业边缘实战:内核日志过滤、持久化与故障定位
linux·运维·边缘计算·工业边缘·dmesg·内核日志
wdfk_prog5 小时前
ROS教程08:从 TransportTCP::connect() 追到 TCPROS Connection Header、序列化与 Socket 数据传输
运维·缓存·docker·容器·ros
做运维的阿瑞5 小时前
Python 标准库汇总:分类速览与常用模块清单
linux·运维·python
日常筹谋记6 小时前
自动化仓储安全防护工况评估:明治传感器AS-33C技术适配性分析
大数据·运维·创业创新·业界资讯
Shadow(⊙o⊙)7 小时前
Linux进阶知识1.0
linux·运维·服务器
szephyr7 小时前
HTTPS 证书申请与自动续期:acme.sh + Let‘s Encrypt 完整记录
nginx·https·acme.sh·证书续期