¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.project.common; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.file.FileUploadUtils; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | import com.ruoyi.framework.config.RuoYiConfig; |
| | | import com.ruoyi.framework.config.ServerConfig; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | |
| | | /** |
| | | * éç¨è¯·æ±å¤ç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @RestController |
| | | public class CommonController |
| | | { |
| | | 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.isValidFilename(fileName)) |
| | | { |
| | | throw new Exception(StringUtils.format("æä»¶åç§°({})éæ³ï¼ä¸å
许ä¸è½½ã ", fileName)); |
| | | } |
| | | String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); |
| | | String filePath = RuoYiConfig.getDownloadPath() + fileName; |
| | | |
| | | response.setCharacterEncoding("utf-8"); |
| | | response.setContentType("multipart/form-data"); |
| | | response.setHeader("Content-Disposition", |
| | | "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName)); |
| | | FileUtils.writeBytes(filePath, response.getOutputStream()); |
| | | if (delete) |
| | | { |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¬å°èµæºéç¨ä¸è½½ |
| | | */ |
| | | @GetMapping("/common/download/resource") |
| | | public void resourceDownload(String name, HttpServletRequest request, HttpServletResponse response) throws Exception |
| | | { |
| | | // æ¬å°èµæºè·¯å¾ |
| | | String localPath = RuoYiConfig.getProfile(); |
| | | // æ°æ®åºèµæºå°å |
| | | String downloadPath = localPath + StringUtils.substringAfter(name, Constants.RESOURCE_PREFIX); |
| | | // ä¸è½½åç§° |
| | | String downloadName = StringUtils.substringAfterLast(downloadPath, "/"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | response.setContentType("multipart/form-data"); |
| | | response.setHeader("Content-Disposition", |
| | | "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName)); |
| | | FileUtils.writeBytes(downloadPath, response.getOutputStream()); |
| | | } |
| | | } |