通过纯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();
        }
    }
相关推荐
Lvan的前端笔记6 小时前
docker:每个前端项目一个 Nginx 容器还是只有一个Nginx容器
前端·nginx·docker
姜鱼问生6 小时前
Nginx 缓存命中率监控:从 X-Cache-Status 到实时统计
运维·nginx·缓存
Apipi*6 小时前
30天速通Linux 第六章信号及信号处理
linux·运维·信号处理
运维行者_8 小时前
网络性能监控怎么做?从自动发现到根因分析的4个环节
运维·服务器·网络·人工智能·支持向量机
此时不提桶,更待何时8 小时前
06-14-A-Kafka集群运维与迁移实战详解
运维·kafka
IT研究所10 小时前
AI-ITR平台如何减少客户问题反复升级?
大数据·运维·人工智能·低代码·自然语言处理·安全架构·企微
AIgorithmGEEK10 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil20810 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)10 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter62510 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器