Java上传下载压缩文件(zip/rar/7z)

上传与下载

package com.yc.server.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试类
 * @Author xx
 * @PackageName xxx
 * @Package com.xx.server.controller
 * @Date 2023/7/10 14:02
 */
@Slf4j
@RestController
@RequestMapping("/xx")
public class TestController {

    @RequestMapping("/test")
    public String test(){
        return "测试接口通过!!!";
    }


        @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String uploadFile(@RequestPart("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "请选择要上传的文件";
        }

        String fileName = file.getOriginalFilename();
        String extension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();

        if (!extension.equals(".zip") && !extension.equals(".rar") && !extension.equals(".7z")) {
            return "只允许上传zip、rar或7z文件";
        }

        try {
            InputStream inputStream = file.getInputStream();
            File destination = new File("xxx\\upload" + fileName);
            FileOutputStream outputStream = new FileOutputStream(destination);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();

            return "文件上传成功!";
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败,请重试";
        }
    }


    private static final Map<String, String> FILE_EXTENSION_CONTENT_TYPE_MAP = new HashMap<>();

    static {
        FILE_EXTENSION_CONTENT_TYPE_MAP.put(".zip", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FILE_EXTENSION_CONTENT_TYPE_MAP.put(".rar", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FILE_EXTENSION_CONTENT_TYPE_MAP.put(".7z", MediaType.APPLICATION_OCTET_STREAM_VALUE);
    }

    @GetMapping(value = "/download/{fileName}")
    public ResponseEntity<FileSystemResource> downloadFile(@PathVariable("fileName") String fileName) {
        String extension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();

        if (!FILE_EXTENSION_CONTENT_TYPE_MAP.containsKey(extension)) {
            return ResponseEntity.notFound().build();
        }

        File file = new File("xxxx\\" + fileName);
        FileSystemResource resource = new FileSystemResource(file);

        return ResponseEntity.ok()
                .header("Content-Disposition", "attachment; filename=" + fileName)
                .contentType(MediaType.parseMediaType(FILE_EXTENSION_CONTENT_TYPE_MAP.get(extension)))
                .body(resource);
    }
}

你可能感兴趣的:(java,开发语言)