From ee5e1dbddd1b8cec48676c86892d53f45d6b7171 Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Tue, 2 Jul 2024 08:49:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B93.0=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=A1=8C=E9=9D=A2=E5=9B=BE=E6=A0=87=E4=B8=8D=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DownloadController.java | 28 +++ .../ApplicationLoginController.java | 6 +- .../user/controller/UserController.java | 3 +- .../zhiyun03/utils/config/MyWebMvcConfig.java | 17 ++ .../zhiyun/zhiyun03/utils/file/IFileUtil.java | 90 +++++++++ .../zhiyun03/utils/file/LocalFileUtil.java | 178 ++++++++++++++++++ src/main/resources/application.yml | 7 + 7 files changed, 325 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/zhiyun/zhiyun03/utils/file/IFileUtil.java create mode 100644 src/main/java/com/zhiyun/zhiyun03/utils/file/LocalFileUtil.java diff --git a/src/main/java/com/zhiyun/zhiyun03/download/controller/DownloadController.java b/src/main/java/com/zhiyun/zhiyun03/download/controller/DownloadController.java index 28973fa..94b4403 100644 --- a/src/main/java/com/zhiyun/zhiyun03/download/controller/DownloadController.java +++ b/src/main/java/com/zhiyun/zhiyun03/download/controller/DownloadController.java @@ -9,13 +9,17 @@ import com.zhiyun.zhiyun03.download.vo.DownloadVo; import com.zhiyun.zhiyun03.utils.annotation.NoLoginCheck; import com.zhiyun.zhiyun03.utils.common.JsonResult; import com.zhiyun.zhiyun03.utils.common.ResultCode; +import com.zhiyun.zhiyun03.utils.file.IFileUtil; import com.zhiyun.zhiyun03.utils.oss.AliyunOssUtil; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.util.List; @@ -31,6 +35,9 @@ public class DownloadController { @Resource private DownloadService downloadService; + @Value("${file.path}") + private String filePath; + @ApiOperation("查询下载中心") @GetMapping("/queryDownload") @@ -157,5 +164,26 @@ public class DownloadController { } + @Autowired + private IFileUtil fileUtil; + @ApiOperation("客户端下载") + @GetMapping ("/getCs") + @NoLoginCheck + public void downTzCs(HttpServletResponse response){ + + fileUtil.download(response,"天择智云.exe"); + + } + + @ApiOperation("谷歌浏览器下载") + @GetMapping ("/getChrome") + @NoLoginCheck + public void getChrome(HttpServletResponse response){ + + fileUtil.download(response,"谷歌浏览器.exe"); + + } + + } diff --git a/src/main/java/com/zhiyun/zhiyun03/user/controller/ApplicationLoginController.java b/src/main/java/com/zhiyun/zhiyun03/user/controller/ApplicationLoginController.java index 4325a2f..10c4f66 100644 --- a/src/main/java/com/zhiyun/zhiyun03/user/controller/ApplicationLoginController.java +++ b/src/main/java/com/zhiyun/zhiyun03/user/controller/ApplicationLoginController.java @@ -115,9 +115,9 @@ public class ApplicationLoginController { CoreUser user = objectMapperEncry.readValue(userInfoJsonEncry, CoreUser.class); - if ("供应链金融实训系统".equals(applicationName)){ - applicationName = "天择供应链金融实训系统V1.0"; - } + if ("供应链金融实训系统".equals(applicationName)){ + applicationName = "天择供应链金融实训系统V1.0"; + } if ("外汇模拟交易系统".equals(applicationName)){ applicationName = "天择外汇模拟交易实训系统"; diff --git a/src/main/java/com/zhiyun/zhiyun03/user/controller/UserController.java b/src/main/java/com/zhiyun/zhiyun03/user/controller/UserController.java index 795e187..5ee8fe0 100644 --- a/src/main/java/com/zhiyun/zhiyun03/user/controller/UserController.java +++ b/src/main/java/com/zhiyun/zhiyun03/user/controller/UserController.java @@ -235,7 +235,8 @@ public class UserController { select.lambda().eq(Application::getAppName, "智云数字金融综合平台"); Application applicationByMain = applicationMapper.selectOne(select); ApplicationIconVo applicationIconVo = new ApplicationIconVo(); - applicationIconVo.setAppIcon(applicationByMain.getAppIcon()); + applicationIconVo.setAppIcon("https://zhiyun03.oss-cn-beijing.aliyuncs.com/images/"+89+".png"); + applicationIconVo.setClientIcon("89"); applicationIconVo.setAppUrl(applicationByMain.getAppUrl()); applicationIconVo.setAppName(applicationByMain.getAppName()); list.add(applicationIconVo); diff --git a/src/main/java/com/zhiyun/zhiyun03/utils/config/MyWebMvcConfig.java b/src/main/java/com/zhiyun/zhiyun03/utils/config/MyWebMvcConfig.java index 0208c2b..f5d37b6 100644 --- a/src/main/java/com/zhiyun/zhiyun03/utils/config/MyWebMvcConfig.java +++ b/src/main/java/com/zhiyun/zhiyun03/utils/config/MyWebMvcConfig.java @@ -1,6 +1,10 @@ package com.zhiyun.zhiyun03.utils.config; import com.zhiyun.zhiyun03.filter.LoginIntercetor; +import com.zhiyun.zhiyun03.utils.file.IFileUtil; +import com.zhiyun.zhiyun03.utils.file.LocalFileUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @@ -9,6 +13,19 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; @Configuration public class MyWebMvcConfig extends WebMvcConfigurationSupport { + @Value("${file.path}") + private String filePath; + @Value("${file.type}") + private String fileType; + @Bean + public IFileUtil getFileUtil() { + if (fileType.equals("local")) { + return new LocalFileUtil(filePath); + } else { + throw new IllegalArgumentException("未知文件工具类注入类型"); + } + } + @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); diff --git a/src/main/java/com/zhiyun/zhiyun03/utils/file/IFileUtil.java b/src/main/java/com/zhiyun/zhiyun03/utils/file/IFileUtil.java new file mode 100644 index 0000000..786310d --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/utils/file/IFileUtil.java @@ -0,0 +1,90 @@ +package com.zhiyun.zhiyun03.utils.file; + + +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; + +/** + * 文件上传下载接口定义 + * + * @author 陈沅 + */ + +public interface IFileUtil { + + + + /** + * 从http请求上传文件 + * + * @param file form传参http文件对象 + * @return 返回文件在服务器上保存的相对路径 + */ + String upload(MultipartFile file); + + /** + * 根据文件流上传文件 + * + * @param objectName 指定文件名(带扩展名) + * @param fileIn 文件流 + * @return 返回文件在服务器上保存的相对路径 + */ + String upload(String objectName, InputStream fileIn); + + /** + * 删除服务器上的一个文件 + * + * @param path 文件在服务器上保存的相对路径 + * @return 返回true则表示删除成功 + */ + boolean remove(String path); + + /** + * 下载文件流,指定文件名 + * + * @param response + * @param fileName 指定下载文件名称 + * @param filePath 文件保存的相对路径 + */ + void download(HttpServletResponse response, String fileName, String filePath); + + /** + * 下载文件流,不指定文件名 + * + * @param response + * @param filePath 文件保存的相对路径 + */ + void download(HttpServletResponse response, String filePath); + + /** + * 相对路径 + * + * @param relativePath 文件保存的相对路径 + * @return 文件保存的绝对路径 + */ + String getFullPath(String relativePath); + + + /** + * 获取文件后缀名 + * + * @param fileName 文件名称 + * @return suffix + */ + String getSuffix(String fileName); + + + /** + * 获取文件存储至磁盘的相对路径 + * + * @param fileName 文件名 + * @param suffix 动态拼接的文件后缀,若该值为空则不拼接 + * @return 相对路径 + */ + String getDiskRelativePath(String fileName, String suffix); + +} diff --git a/src/main/java/com/zhiyun/zhiyun03/utils/file/LocalFileUtil.java b/src/main/java/com/zhiyun/zhiyun03/utils/file/LocalFileUtil.java new file mode 100644 index 0000000..299dd63 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/utils/file/LocalFileUtil.java @@ -0,0 +1,178 @@ +package com.zhiyun.zhiyun03.utils.file; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.IdUtil; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +public class LocalFileUtil implements IFileUtil{ + + private final static List excludeSp = Arrays.asList("bin", "sh"); + + private final String localPath; + + public LocalFileUtil(String localPath) { + this.localPath = localPath; + } + + @Override + public String upload(MultipartFile file) { + return upload(null, file); + } + + @Override + public String upload(String objectName, InputStream fileIn) { + Assert.notNull(fileIn, "文件不能为空"); + Assert.hasText(objectName, "文件名为空"); + Assert.isTrue(objectName.lastIndexOf(".") > 0, "文件名称需携带扩展后缀"); + FileOutputStream out = null; + try { + String relativePath = getDiskRelativePath(objectName, null); + File file = new File(getFullPath(relativePath)); + file.getParentFile().mkdirs(); + out = new FileOutputStream(file); + byte[] buff = new byte[4096]; + int len; + while ((len = fileIn.read(buff)) != -1) { + out.write(buff, 0, len); + } + out.flush(); + return relativePath; + } catch (IOException e) { + e.printStackTrace(); + throw new IllegalArgumentException("上传文件失败,IO错误"); + } finally { + try { + fileIn.close(); + if (out != null) out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public boolean remove(String path) { + if (!StringUtils.hasText(path)) return false; + File file = new File(getFullPath(path)); + if (!file.exists()) return false; + return file.delete(); + } + + @Override + public void download(HttpServletResponse response, String fileName, String relativePath) { + Assert.hasText(relativePath, "路径为空"); + System.out.println("------------------------------------" + getFullPath(relativePath)); + File file = new File(getFullPath(relativePath)); + Assert.isTrue(file.exists(), "附件不存在"); + String sp = relativePath.substring(relativePath.lastIndexOf(".") + 1); + Assert.isTrue(!excludeSp.contains(sp.toLowerCase()), "文件类型错误"); + if (!StringUtils.hasText(fileName)) { + fileName = file.getName().substring(0, file.getName().lastIndexOf(".")); + } + InputStream fis = null; + try { + response.setContentType("application/octet-stream"); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode((fileName + "." + sp), CharsetUtil.UTF_8)); + fis = new BufferedInputStream(Files.newInputStream(file.toPath())); + response.addHeader("Content-Length", String.valueOf(fis.available())); + byte[] buff = new byte[4096]; + int len; + while ((len = fis.read(buff)) != -1) { + response.getOutputStream().write(buff, 0, len); + } + } catch (Exception e) { + throw new IllegalArgumentException("下载文件失败: " + e.getMessage()); + } finally { + try { + if (fis != null) fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public void download(HttpServletResponse response, String relativePath) { + download(response, null, relativePath); + } + + @Override + public String getFullPath(String relativePath) { + if (!relativePath.startsWith("/")) { + relativePath = "/" + relativePath; + } + return localPath + relativePath; + } + + @Override + public String getSuffix(String fileName) { + Assert.hasText(fileName, "文件名不存在!"); + if (fileName.lastIndexOf(".") < 0) { + return null; + } + return fileName.substring(fileName.lastIndexOf(".") + 1); + } + + public String upload(String fileName, MultipartFile file) { + return upload(fileName, file, null); + } + + + @Override + public String getDiskRelativePath(String fileName, String suffix) { + Calendar c = Calendar.getInstance(); + Date d = new Date(); + c.setTime(d); + String year = Integer.toString(c.get(Calendar.YEAR)); + String month = Integer.toString(c.get(Calendar.MONTH) + 1); + String day = Integer.toString(c.get(Calendar.DATE)); + StringBuilder path = new StringBuilder(); + path.append("/").append(year) + .append("/").append(month) + .append("/").append(day) + .append("/").append(fileName); + if (StringUtils.hasText(suffix)) path.append(".").append(suffix); + return path.toString(); + } + + + private String upload(String fileName, MultipartFile file, String relativePath) { + Assert.isTrue(!file.isEmpty(), "文件不存在"); + String originalFilename = file.getOriginalFilename(); + if (fileName == null) fileName = IdUtil.simpleUUID(); + String sp = getSuffix(originalFilename); + Assert.notNull(sp, "文件类型错误"); + Assert.isTrue(!excludeSp.contains(sp.toLowerCase()), "文件类型错误"); + try { + String filePath; + if (relativePath == null) { + filePath = getDiskRelativePath(fileName, sp); + } else { + relativePath = relativePath.endsWith("/") ? relativePath : relativePath + "/"; + filePath = relativePath + fileName + "." + sp; + } + + File destFile = new File(getFullPath(filePath)); + destFile.getParentFile().mkdirs(); + file.transferTo(destFile); + return filePath; + } catch (FileNotFoundException e) { + throw new IllegalArgumentException("上传文件失败,FileNotFound错误"); + } catch (IOException e) { + throw new IllegalArgumentException("上传文件失败,IO错误"); + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e6bb026..1006189 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -59,3 +59,10 @@ pagehelper: swagger: enabled: false + + +# 文件存储 +file: + type: local + path: /usr/local/tianzeProject/zhiyun +# path: D:\home \ No newline at end of file