通过纯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();
        }
    }
相关推荐
Android系统攻城狮9 分钟前
Linux FFmpeg深度解析之avcodec_alloc_context3调用流程与实战(十四)
linux·运维·ffmpeg·车载音视频·音视频进阶·ffmpeg音视频进阶
老李IT笔记11 分钟前
抹除之后机器上的受管应用可以留下:这一步省掉的是哪段时间
运维·服务器
雾时之林41 分钟前
Nginx反向代理与proxy_cache缓存搭建CDN服务器
服务器·nginx·缓存
李纲明1 小时前
WordPress 定时发布不准点或备份/邮件从不跑:先分清 WP-Cron 触发与 DISABLE_WP_CRON,再查回环与系统 Cron
运维·性能优化·wordpress·定时任务·devdiy·wp-cron
Decay__1 小时前
01_MySQL 体系架构全解 —— 连接层、SQL 层、存储引擎层与 InnoDB 深入
运维·数据库·sql·mysql·架构
Linux_cy1 小时前
nginx 502 排查手册:九种原因分类、定位命令与检查顺序
linux·运维·服务器·网络·数据库·nginx·ssh
椒颜皮皮虾྅1 小时前
模型部署不再卡在环境上:DeploySharp 四大推理引擎与 Windows/Linux 离线交付实战
linux·运维·开发语言·人工智能·windows·计算机视觉·cuda
前端 贾公子2 小时前
第 6 章:MCP 服务器介绍与接入
运维·服务器·网络
-梅2 小时前
GitHub Actions 自动化:5 个真实运维提效案例
运维·自动化·github
syntaxsage2 小时前
Dive:Docker 镜像层可视化分析工具
运维·其他·docker·容器