通用请求处理
| |
| |
| |
| |
| |
| @Controller |
| @RequestMapping("/common") |
| |
| public class CommonController |
| { |
| private static final Logger log = LoggerFactory.getLogger(CommonController.class); |
| |
| @Autowired |
| private ServerConfig serverConfig; |
| |
| private static final String FILE_DELIMETER = ","; |
| |
| |
| |
| |
| |
| |
| |
| @GetMapping("/download") |
| public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) |
| { |
| try |
| { |
| if (!FileUtils.checkAllowDownload(fileName)) |
| { |
| throw new Exception(StringUtils.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); |
| } |
| } |
| catch (Exception e) |
| { |
| log.error("下载文件失败", e); |
| } |
| } |
| |
| |
| |
| |
| @PostMapping("/upload") |
| @ResponseBody |
| 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("url", url); |
| ajax.put("fileName", fileName); |
| ajax.put("newFileName", FileUtils.getName(fileName)); |
| ajax.put("originalFilename", file.getOriginalFilename()); |
| return ajax; |
| } |
| catch (Exception e) |
| { |
| return AjaxResult.error(e.getMessage()); |
| } |
| } |
| |
| |
| |
| |
| @PostMapping("/uploads") |
| @ResponseBody |
| public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception |
| { |
| try |
| { |
| |
| String filePath = RuoYiConfig.getUploadPath(); |
| List<String> urls = new ArrayList<String>(); |
| List<String> fileNames = new ArrayList<String>(); |
| List<String> newFileNames = new ArrayList<String>(); |
| List<String> originalFilenames = new ArrayList<String>(); |
| for (MultipartFile file : files) |
| { |
| |
| String fileName = FileUploadUtils.upload(filePath, file); |
| String url = serverConfig.getUrl() + fileName; |
| urls.add(url); |
| fileNames.add(fileName); |
| newFileNames.add(FileUtils.getName(fileName)); |
| originalFilenames.add(file.getOriginalFilename()); |
| } |
| AjaxResult ajax = AjaxResult.success(); |
| ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER)); |
| ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| return ajax; |
| } |
| catch (Exception e) |
| { |
| return AjaxResult.error(e.getMessage()); |
| } |
| } |
| |
| |
| |
| |
| @GetMapping("/download/resource") |
| public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) |
| throws Exception |
| { |
| try |
| { |
| if (!FileUtils.checkAllowDownload(resource)) |
| { |
| throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource)); |
| } |
| |
| String localPath = RuoYiConfig.getProfile(); |
| |
| String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX); |
| |
| String downloadName = StringUtils.substringAfterLast(downloadPath, "/"); |
| response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); |
| FileUtils.setAttachmentResponseHeader(response, downloadName); |
| FileUtils.writeBytes(downloadPath, response.getOutputStream()); |
| } |
| catch (Exception e) |
| { |
| log.error("下载文件失败", e); |
| } |
| } |
| } |
| |
| |