上传、回显、下载
package com.gennlife.settlement.rest;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RestController
@RequestMapping("/p")
public class TGyjhController {
private String uploadPath = "config/image/";
@PostMapping("/uploadOne")
public Object uploadOne(@RequestParam(value ="file") MultipartFile multipartFile) throws IOException {
String currentDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String yyyyMMdd = uploadPath + currentDate + File.separator;
if (!FileUtil.exist(yyyyMMdd)) {
FileUtil.mkdir(yyyyMMdd);
}
String fileName = UUID.randomUUID().toString() + "@" + multipartFile.getOriginalFilename();
String suffix = Objects.requireNonNull(multipartFile.getOriginalFilename()).substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
File file1 = FileUtil.writeBytes(multipartFile.getBytes(), yyyyMMdd + fileName);
List<Map<String, String>> pathList = new ArrayList<>();
if (file1.length() > 0) {
Map<String, String> map = new HashMap<>();
map.put("fileName", fileName);
map.put("suffix", suffix);
map.put("path", yyyyMMdd);
pathList.add(map);
}
return pathList;
}
@PostMapping("/uploadMany")
public Object uploadMany(@RequestParam(value ="file")MultipartFile[] file) throws IOException {
String currentDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String yyyyMMdd = uploadPath + currentDate + File.separator;
if (!FileUtil.exist(yyyyMMdd)) {
FileUtil.mkdir(yyyyMMdd);
}
List<Map<String, String>> pathList = new ArrayList<>();
if (file.length > 0) {
for (MultipartFile multipartFile : file) {
String fileName = UUID.randomUUID().toString() + "@" + multipartFile.getOriginalFilename();
String suffix = Objects.requireNonNull(multipartFile.getOriginalFilename()).substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
File file1 = FileUtil.writeBytes(multipartFile.getBytes(), yyyyMMdd + fileName);
if (file1.length() > 0) {
Map<String, String> map = new HashMap<>();
map.put("fileName", fileName);
map.put("suffix", suffix);
map.put("path", currentDate);
pathList.add(map);
}
}
}
return pathList;
}
@PostMapping("showImg")
public Object showImg(HttpServletResponse response, @RequestBody Map<String, Object> map) {
if (map.isEmpty()) {
return "文件不能为空";
}
boolean suffix = checkPic(map.get("suffix").toString());
if (!suffix) {
return "不是图片";
}
try {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(FileUtil.readBytes(map.get("path").toString() + map.get("fileName").toString()));
IoUtil.close(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@PostMapping("download")
public Object download(HttpServletResponse response, @RequestBody Map<String, Object> map) throws IOException {
if (map.isEmpty()) {
return "文件不能为空";
}
String fileUrl = uploadPath + map.get("path").toString() + File.separator + map.get("fileName").toString();
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(map.get("fileName").toString().getBytes("gb2312"), "ISO8859-1"));
outputStream.write(FileUtil.readBytes(fileUrl));
IoUtil.close(outputStream);
return null;
}
private static boolean checkPic(String suffix) {
if (suffix.isEmpty()) {
return false;
}
String reg = "(.JPEG|.jpeg|.JPG|.jpg|.PNG|.png|.GIF|.gif|.BMP|.bmp)$";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher("." + suffix);
return matcher.find();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2020-07-03 IDEA插件-Git Commit Template
2020-07-03 IDEA插件-Translation