| | |
| | | package com.ruoyi.web.controller.project; |
| | | |
| | | import java.io.File; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.project.domain.ServiceDocumentfiles; |
| | | import com.ruoyi.project.service.IServiceDocumentfilesService; |
| | | import com.ruoyi.common.config.RuoYiConfig; |
| | | import com.ruoyi.common.utils.file.FileUploadUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.framework.config.ServerConfig; |
| | | import org.springframework.mock.web.MockMultipartFile; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | /** |
| | | * 捐献附件Controller |
| | |
| | | @Autowired |
| | | private IServiceDocumentfilesService serviceDocumentfilesService; |
| | | |
| | | @Autowired |
| | | private ServerConfig serverConfig; |
| | | |
| | | /** |
| | | * 政策文件上传(**不限文件类型**)。 |
| | | * |
| | | * <p>2026-10-09 业务要求:政策文件模块允许上传/下载任意文件类型。 |
| | | * |
| | | * <p>为什么不直接用 {@code /common/upload}:通用上传走 |
| | | * {@code FileUploadUtils.upload(baseDir, file)},会按 {@code MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION} |
| | | * 白名单校验扩展名,非白名单类型(如 dwg、7z、ofd、eml 等)会被拒绝。 |
| | | * 这里把 allowedExtension 显式传 {@code null} —— {@code assertAllowed} 的判断是 |
| | | * {@code allowedExtension != null && !isAllowedExtension(...)},传 null 即跳过扩展名校验, |
| | | * 同时**仍保留** 500MB 大小限制、文件名长度限制与存储路径安全处理。 |
| | | * |
| | | * <p>仅政策文件模块使用本接口;{@code /common/upload} 与其他模块的白名单保持不变。 |
| | | * |
| | | * @param file 上传文件 |
| | | * @return 与 {@code /common/upload} 一致的返回体(code/msg/fileName/url) |
| | | */ |
| | | @ApiOperation("政策文件上传(不限文件类型)") |
| | | @PostMapping("/upload") |
| | | public AjaxResult uploadFile(MultipartFile file) { |
| | | String originalFilename = file == null ? null : file.getOriginalFilename(); |
| | | try { |
| | | if (file == null || file.isEmpty()) { |
| | | return AjaxResult.error("上传文件为空,请重新选择文件"); |
| | | } |
| | | // 上传文件路径 |
| | | String filePath = RuoYiConfig.getUploadPath(); |
| | | |
| | | // 同名文件处理:加时间戳重命名,避免覆盖已上传文件(与通用上传保持一致) |
| | | String filename = originalFilename == null ? "" : originalFilename.trim(); |
| | | String pathFileName = FileUploadUtils.extractFilename(file); |
| | | File target = new File(filePath + "/" + pathFileName); |
| | | if (target.exists()) { |
| | | int i = filename.lastIndexOf("."); |
| | | if (i > -1) { |
| | | String startStr = filename.substring(0, i); |
| | | String endStr = filename.substring(i); |
| | | String newFilename = startStr + System.currentTimeMillis() + endStr; |
| | | file = new MockMultipartFile(newFilename, newFilename, file.getContentType(), file.getBytes()); |
| | | } |
| | | } |
| | | |
| | | // allowedExtension 传 null:不做扩展名校验,允许任意文件类型 |
| | | String relativePath = FileUploadUtils.upload(filePath, file, null); |
| | | |
| | | // 与通用上传一致的 url 处理(部分部署下 8032 需要带 /prod-api 前缀) |
| | | String url = serverConfig.getUrl(); |
| | | if (url != null && url.contains("8032")) { |
| | | url = url.replace("8032", "8032/prod-api"); |
| | | } |
| | | |
| | | log.info("政策文件上传成功:原始文件名={},存储路径={}", filename, relativePath); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("fileName", relativePath); |
| | | ajax.put("url", url + relativePath); |
| | | return ajax; |
| | | } catch (Exception e) { |
| | | // 返回 AjaxResult(HTTP 200 + code=500),前端 uploadSccess 的 else 分支会把原因弹出来 |
| | | log.error("政策文件上传失败:原始文件名={}", originalFilename, e); |
| | | return AjaxResult.error("上传失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 查询捐献附件列表 |
| | | */ |