diff --git a/src/main/java/com/hxtec/polaris/PolarisApplication.java b/src/main/java/com/hxtec/polaris/PolarisApplication.java index 545b088985d244f9d5aac62bf4fc705daec21dad..d49865f87a50260a859234ee9d1231c06313cbd5 100644 --- a/src/main/java/com/hxtec/polaris/PolarisApplication.java +++ b/src/main/java/com/hxtec/polaris/PolarisApplication.java @@ -1,6 +1,7 @@ package com.hxtec.polaris; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @@ -16,6 +17,7 @@ import tk.mybatis.spring.annotation.MapperScan; @EnableCaching @EnableJpaAuditing @EnableScheduling +@EnableAutoConfiguration public class PolarisApplication { public static void main(String[] args) { diff --git a/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java b/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java index 453861b1b87a8fecf2b812fdac6b4c77e9f4bdad..84f1711052777fce55b13622d4c9921fd0519842 100644 --- a/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java +++ b/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java @@ -57,8 +57,10 @@ public final class GlobalVar { //延时收货时间 默认为3天 public static final int DELAY_TIME = 3; + //文件映射mapping + public static final String FILE_MAPPING_PATH = "document"; //上传文件存储路径 public static final String UPLOAD_BASE_PATH = System.getProperty("user.home")+File.separator + "Polarisdoc"+File.separator; // public static final String CATEGORY_IMG_PATH = File.separator +"upload" + File.separator + "category"; - public static final String CATEGORY_IMG_PATH = UPLOAD_BASE_PATH +"upload" + File.separator + "category"; + public static final String CATEGORY_IMG_PATH = "upload" + File.separator + "category"; } diff --git a/src/main/java/com/hxtec/polaris/commons/tasks/InitTask.java b/src/main/java/com/hxtec/polaris/commons/tasks/InitTask.java index 47ca13df1dfa68ac630bb5e6f2ed8b29941ebdc8..45e9e71eda43175e215f1758b7e8848dd1c0c25a 100644 --- a/src/main/java/com/hxtec/polaris/commons/tasks/InitTask.java +++ b/src/main/java/com/hxtec/polaris/commons/tasks/InitTask.java @@ -25,13 +25,13 @@ public class InitTask implements CommandLineRunner { @Override public void run(String... args){ - File file = new File(GlobalVar.CATEGORY_IMG_PATH); + File file = new File(GlobalVar.UPLOAD_BASE_PATH); if (!file.exists()) { try{ file.mkdirs(); - logger.info("当前发布项目下,没有对应目录!已创建"+GlobalVar.CATEGORY_IMG_PATH); + logger.info("当前发布项目下,没有对应目录!已创建"+GlobalVar.UPLOAD_BASE_PATH); }catch (Exception e){ - logger.info("当前发布项目下,没有对应目录!目录创建失败!目录名称"+GlobalVar.CATEGORY_IMG_PATH); + logger.info("当前发布项目下,没有对应目录!目录创建失败!目录名称"+GlobalVar.UPLOAD_BASE_PATH); } } } diff --git a/src/main/java/com/hxtec/polaris/config/ResourceConfig.java b/src/main/java/com/hxtec/polaris/config/ResourceConfig.java index 66cf98b6beecfd7c7e287605beee5689d1716190..ab1a4aaca4de3387a2c952688095fc2fcd833954 100644 --- a/src/main/java/com/hxtec/polaris/config/ResourceConfig.java +++ b/src/main/java/com/hxtec/polaris/config/ResourceConfig.java @@ -11,9 +11,8 @@ public class ResourceConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - /** 图片传路径 */ -// registry.addResourceHandler("/profile/**").addResourceLocations("file:" + GlobalVar.CATEGORY_IMG_PATH); - registry.addResourceHandler("/profile/**").addResourceLocations("file:C:\\Users\\Justsafe\\Polaris_doc\\upload\\category"); - registry.addResourceHandler("/profile/**").addResourceLocations("file:C:/Users/Justsafe/Polaris_doc/upload/category"); + //配置文件资源映射路径 + registry.addResourceHandler("/"+GlobalVar.FILE_MAPPING_PATH+"/**").addResourceLocations("file:" + GlobalVar.UPLOAD_BASE_PATH); + super.addResourceHandlers(registry); } } diff --git a/src/main/java/com/hxtec/polaris/service/impl/FileServiceImpl.java b/src/main/java/com/hxtec/polaris/service/impl/FileServiceImpl.java index 151424818f3537936081dda7b8d5226980dfbc63..01e8e33627142a69492e78542706f371ab54dc48 100644 --- a/src/main/java/com/hxtec/polaris/service/impl/FileServiceImpl.java +++ b/src/main/java/com/hxtec/polaris/service/impl/FileServiceImpl.java @@ -44,32 +44,31 @@ public class FileServiceImpl implements FileService{ /** * 保存文件到指定目录,并返回路径 - * @param img + * @param img 前端传递过来的图片 * @param request - * @param categoryImgPath - * @return + * @param savePath 图片存储地址 为相对路径 严格遵守 upload/category 格式 + * @return String 返回图片的地址 http://127.0.0.1:9102/polaris/document/upload/category/test.jpg */ - private String saveImg(MultipartFile img, HttpServletRequest request, String categoryImgPath) throws IOException { + private String saveImg(MultipartFile img, HttpServletRequest request, String savePath) throws IOException { String path = "error"; if(!img.isEmpty()) { //原本的文件名 //现在的文件名是时间戳加原文件名,出现图片相同时,读取不出来的bug String nowName = System.currentTimeMillis() + img.getOriginalFilename(); + //如果文件名太长,给图片重命名 + if (img.getOriginalFilename().length()>20){ + nowName = "图片-" + System.currentTimeMillis(); + } //将文件保存在当前工程下的一个upload文件 -// String realPath = request.getSession().getServletContext().getRealPath(categoryImgPath); //将文件的输入流输出到一个新的文件 - FileUtils.copyInputStreamToFile(img.getInputStream(), new File(categoryImgPath, nowName)); + FileUtils.copyInputStreamToFile(img.getInputStream(), new File(GlobalVar.UPLOAD_BASE_PATH+savePath, nowName)); //getScheme 当前链接使用的协议 getServerName 服务器地址 getServerPort 端口号 getContextPath 应用名称 -// path = request.getScheme() -// +"://" + request.getServerName() -// + ":" + request.getServerPort() -// + request.getContextPath() -// + categoryImgPath -// + "/" + nowName; path = request.getScheme() - +"://" + request.getServerName() + + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + + "/" + GlobalVar.FILE_MAPPING_PATH + + "/" + savePath + "/" + nowName; } return path;