import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.demo.common.reply.Result; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; /** * 文件 **/ @RestController @RequestMapping("api/file") @Tag(name = "文件") public class FileController { @GetMapping("download-image") @Operation(summary = "下载") @Parameter(name = "filename", description = "文件名称", in = ParameterIn.QUERY) @ApiOperationSupport(order = 5) public ResponseEntity download(String filename) { Resource image = new ClassPathResource("static/" + filename); if (!image.exists()) { return ResponseEntity.ok(Result.error(404, "文件不存在")); } String fileName = URLEncoder.encode("网站图标.png", StandardCharsets.UTF_8); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName); headers.add(HttpHeaders.CONTENT_TYPE, "image/png"); return ResponseEntity.ok().headers(headers).body(image); } }