若依ruoyi 系统日志导出

业务需求说明:仅admin用户可导出若依系统产生的日志

前端代码

index.vue






common.js

import request from '@/utils/request'

// 系统日志下载
export function logDownload(params) {
  return request({
    url: '/common/logDownload?fileName=' + params,
    method: 'get',
    responseType: 'blob'
  })
}

后端代码

// 当前启动的环境
@Value("${spring.profiles.active}")
private String activeProfile;


/**
 * 日志下载方法
 * @param fileName
 * @param response
 * @param request
 */
@GetMapping("/common/logDownload")
public void logFileDownload(String fileName, HttpServletResponse response, HttpServletRequest request)
{
    try
    {
        if(!SecurityUtils.getLoginUser().getUser().isAdmin()){
            throw new BaseException("非管理员,取法下载该文件。");
        }
        String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
        String diskPath = "local".equals(activeProfile)? "D:/home/ruoyi/logs/": "/home/ruoyi/logs/";
        String filePath = diskPath + fileName;
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FileUtils.setAttachmentResponseHeader(response, realFileName);
        FileUtils.writeBytes(filePath, response.getOutputStream());
    }
    catch (Exception e)
    {
        log.error("下载文件失败", e);
    }
}

你可能感兴趣的:(vue.js,javascript)