package com.zhongzheng.controller; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.zhongzheng.common.config.RuoYiConfig; import com.zhongzheng.common.constant.Constants; import com.zhongzheng.common.core.controller.BaseController; import com.zhongzheng.common.core.domain.AjaxResult; import com.zhongzheng.common.exception.CustomException; import com.zhongzheng.common.utils.file.FileUploadUtils; import com.zhongzheng.common.utils.file.FileUtils; import com.zhongzheng.framework.config.ServerConfig; import com.zhongzheng.modules.alioss.bo.FileHandleBo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /** * 通用请求处理 * * @author zhongzheng */ @RestController public class CommonController extends BaseController { private static final Logger log = LoggerFactory.getLogger(CommonController.class); @Autowired private ServerConfig serverConfig; /** * 通用下载请求 * * @param fileName 文件名称 * @param delete 是否删除 */ @GetMapping("common/download") public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) { try { if (!FileUtils.checkAllowDownload(fileName)) { throw new Exception(StrUtil.format("文件名称({})非法,不允许下载。 ", fileName)); } String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); String filePath = RuoYiConfig.getDownloadPath() + fileName; response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, realFileName); FileUtils.writeBytes(filePath, response.getOutputStream()); /* if (delete) { FileUtils.deleteFile(filePath); }*/ FileUtils.deleteFile(filePath); } catch (Exception e) { log.error("下载文件失败", e); } } /** * 通用上传请求 */ @PostMapping("/common/upload") public AjaxResult uploadFile(MultipartFile file) throws Exception { try { // 上传文件路径 String filePath = RuoYiConfig.getUploadPath(); // 上传并返回新文件名称 String fileName = FileUploadUtils.upload(filePath, file); String url = serverConfig.getUrl() + fileName; AjaxResult ajax = AjaxResult.success(); ajax.put("fileName", fileName); ajax.put("url", url); return ajax; } catch (Exception e) { return AjaxResult.error(e.getMessage()); } } /** * 文件分片处理 */ @PostMapping("/common/decompression") public AjaxResult> uploadDecompression(MultipartFile file, String param) throws Exception { String zhiyuan = System.getProperty("user.dir"); FileHandleBo bo = JSONObject.parseObject(param, FileHandleBo.class); String path = zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign(); String[] split = bo.getName().split("\\."); String fileName = bo.getFileMd5() + "_" + bo.getIndex()+"."+ split[split.length - 1]; FileUploadUtils.uploadFragment(path, file, fileName); File file2 = new File(path); List fileNameList = Arrays.stream(file2.listFiles()).map(File::getName).collect(Collectors.toList()); return AjaxResult.success(fileNameList); } /** * 本地文件删除 */ @GetMapping("/common/del/file") public AjaxResult delFile() { String zhiyuan = System.getProperty("user.dir"); String toPath = zhiyuan + "/zhongzheng-admin/src/main/resources/zhiyuan"; File directory = new File(toPath); try { Files.walk(directory.toPath()) .filter(Files::isRegularFile) .map(Path::toFile) .forEach(File::delete); }catch (Exception e){ e.printStackTrace(); throw new CustomException(e.getMessage()); } return AjaxResult.success(); } /** * 文件分片处理 */ @PostMapping("/common/merge/file") public AjaxResult uploadDecompression(@RequestBody FileHandleBo bo) throws Exception { String zhiyuan = System.getProperty("user.dir"); String path = zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign(); //全部上传完成 合并文件返回文件名称 String toPath = zhiyuan + "/zhongzheng-admin/src/main/resources/zhiyuan"+bo.getTimeSign(); File file1 = new File(toPath); if (!file1.exists()) { file1.mkdirs(); } File ToFile = new File(toPath + "/" + bo.getName()); if (ToFile.createNewFile()) { FileUploadUtils.merge(bo.getFileMd5(), path, ToFile.getPath()); //校验文件MD5值 String content = FileUtils.md5HashCode(ToFile.getPath()); if (!bo.getFileMd5().equals(content)) { //文件不一致,删除文件 ToFile.delete(); //删除分片资源 FileUtils.deleteFilePackage(zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign()); throw new CustomException("文件上传失败!请重新上传"); } } //删除分片资源 FileUtils.deleteFilePackage(zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign()); return AjaxResult.success(); } @PostMapping("/common/delete/file") public AjaxResult deleteDecompression(@RequestBody FileHandleBo bo) throws Exception { //删除合并文件 String zhiyuan = System.getProperty("user.dir"); String toPath = zhiyuan + "/zhongzheng-admin/src/main/resources/zhiyuan"+bo.getTimeSign(); File file = new File(toPath); List files = Arrays.stream(file.listFiles()).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(files)){ files.stream().filter(item -> item.getName().equals(bo.getName())).forEach(x -> { x.delete(); }); } return AjaxResult.success(); } /** * 本地资源通用下载 */ @GetMapping("/common/download/resource") public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) throws Exception { try { if (!FileUtils.checkAllowDownload(resource)) { throw new Exception(StrUtil.format("资源文件({})非法,不允许下载。 ", resource)); } // 本地资源路径 String localPath = RuoYiConfig.getProfile(); // 数据库资源地址 String downloadPath = localPath + StrUtil.subAfter(resource, Constants.RESOURCE_PREFIX, false); // 下载名称 String downloadName = StrUtil.subAfter(downloadPath, "/", true); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, downloadName); FileUtils.writeBytes(downloadPath, response.getOutputStream()); } catch (Exception e) { log.error("下载文件失败", e); } } }