文件下载

http://localhost/dev-api/common/download/resource?name=/profile/avatar/2020/05/27/fd7a23f212cda2805b83cf3f90c4af36.jpeg
/**
 * 本地资源通用下载
 *
 * @param name     文件路径
 * @param request  请求
 * @param response 响应
 * @throws Exception
 */
@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());
}
posted @ 2020-05-27 22:12  xl4ng  阅读(342)  评论(0编辑  收藏  举报