|  | @@ -1,58 +0,0 @@
 | 
	
		
			
				|  |  | -package com.supervision.web.noticeManage.controller;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | -import org.springframework.http.ResponseEntity;
 | 
	
		
			
				|  |  | -import org.springframework.util.StringUtils;
 | 
	
		
			
				|  |  | -import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  | -import org.springframework.web.multipart.MultipartFile;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -import java.io.*;
 | 
	
		
			
				|  |  | -import java.nio.file.*;
 | 
	
		
			
				|  |  | -import java.time.LocalDate;
 | 
	
		
			
				|  |  | -import java.time.format.DateTimeFormatter;
 | 
	
		
			
				|  |  | -import java.util.*;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -@RestController
 | 
	
		
			
				|  |  | -@RequestMapping("/notice")
 | 
	
		
			
				|  |  | -public class FileController {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    @Value("${file.upload-dir}")
 | 
	
		
			
				|  |  | -    private String uploadDir;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    @Value("${file.url-prefix}")
 | 
	
		
			
				|  |  | -    private String urlPrefix;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    @PostMapping("/upload")
 | 
	
		
			
				|  |  | -    public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file) {
 | 
	
		
			
				|  |  | -        if (file == null || file.isEmpty()) {
 | 
	
		
			
				|  |  | -            return ResponseEntity.badRequest().body(Collections.singletonMap("message", "文件为空"));
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            // 按日期分目录: yyyy/MM/dd
 | 
	
		
			
				|  |  | -            String sub = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
 | 
	
		
			
				|  |  | -            Path dir = Paths.get(uploadDir, sub);
 | 
	
		
			
				|  |  | -            if (!Files.exists(dir)) Files.createDirectories(dir);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            String original = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
 | 
	
		
			
				|  |  | -            String ext = "";
 | 
	
		
			
				|  |  | -            int idx = original.lastIndexOf('.');
 | 
	
		
			
				|  |  | -            if (idx >= 0) ext = original.substring(idx);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            String filename = UUID.randomUUID().toString().replace("-", "") + ext;
 | 
	
		
			
				|  |  | -            Path target = dir.resolve(filename);
 | 
	
		
			
				|  |  | -            try (InputStream in = file.getInputStream()) {
 | 
	
		
			
				|  |  | -                Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            // 返回相对 URL:/files/yyyy/MM/dd/filename.ext
 | 
	
		
			
				|  |  | -            String fileUrl = urlPrefix + "/" + sub + "/" + filename;
 | 
	
		
			
				|  |  | -            Map<String, Object> body = new HashMap<>();
 | 
	
		
			
				|  |  | -            body.put("url", fileUrl);
 | 
	
		
			
				|  |  | -            body.put("contentType", file.getContentType());
 | 
	
		
			
				|  |  | -            return ResponseEntity.ok(body);
 | 
	
		
			
				|  |  | -        } catch (IOException e) {
 | 
	
		
			
				|  |  | -            e.printStackTrace();
 | 
	
		
			
				|  |  | -            return ResponseEntity.status(500).body(Collections.singletonMap("message", "上传失败"));
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -}
 |