详解如何使用Nginx搭建文件服务器及实现文件服务

前言

公司最近有做文件服务器的需求,并且使用到了Nginx做负载均衡服务器,顺水推舟,就想着顺便用作文件服务器算了,实际上它也非常适合。

Nginx是一种轻巧、高效的Web服务器,用作文件服务器非常合适。但是如果需要一些高级功能,如FTP远程访问、多用户管理,可能需要选择更为复杂的方案,例如Apache或FileZilla Server。

搭建步骤

步骤一:安装Nginx

1.1 首先需要安装Nginx,可以使用以下命令:

sudo apt-get update
sudo apt-get install nginx

1.2 安装完成后,启动Nginx服务:

sudo systemctl start nginx

步骤二:创建Nginx配置文件

2.1 创建一个新的Nginx配置文件:

sudo nano /etc/nginx/sites-available/myfileserver

2.2 编写配置文件

server {
    listen 80;
    server_name test.com;
        
    location / {
           #指向文件存放的位置
           root /path/to/file;
           autoindex on;
           autoindex_exact_size off;
           charset utf-8;
    }
}


这个配置文件表示监听在端口80上的请求,同时指向存储文件的目录并开启文件列表功能

2.3 关闭并保存文件

!wq

步骤三:开发文件服务

我们是用Java做的接口,使用Springboot框架+Maven

3.1 在Maven中添加坐标

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies> 

3.2 在application.properties中添加配置

#服务端口
server.port=8080 

#指定Nginx文件服务器地址和文件存放位置
fileserver.url=test.com 
fileserver.path=/path/to/file/

3.3 实现文件上传,下载,查看的服务

@Controller
public class FileController {

    @Value("${fileserver.path}")
    private String fileServerPath;

    @PostMapping("/upload")
    public String uploadFile(Model model, @RequestParam("file") MultipartFile file) {
        try {
            Path filePath = Paths.get(fileServerPath + "/" + file.getOriginalFilename());
            Files.write(filePath, file.getBytes());
            model.addAttribute("message", "File uploaded successfully");
        } catch (IOException e) {
            e.printStackTrace();
            model.addAttribute("message", "File upload failed");
        }

        return "uploadForm";
    }

    @GetMapping("/download/{fileName:.+}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable("fileName") String fileName{
        Path filePath = Paths.get(fileServerPath + "/" + fileName);
        HttpHeaders headers = new HttpHeaders();
        try {
            byte[] data = Files.readAllBytes(filePath);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", fileName);
            headers.setContentLength(data.length);
            return new ResponseEntity<byte[]>(data, headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
        }
    }

    @GetMapping("/")
    public String getFiles(Model model) {
        List<String> fileList = new ArrayList<>();
        File folder = new File(fileServerPath);
        File[] files = folder.listFiles();
        for(File file : files) {
            if (file.isFile()) {
                fileList.add(file.getName());
            }
        }
        model.addAttribute("fileList", fileList);
        return "fileList";
    }
}

至此,文件服务的搭建和文件服务的开发就完成了,能满足基本的文件服务需求

猜你喜欢

转载自blog.csdn.net/weixin_42559574/article/details/129943000
今日推荐