diff --git a/len-core/src/main/java/com/len/util/UploadUtil.java b/len-core/src/main/java/com/len/util/UploadUtil.java
new file mode 100644
index 0000000..e483c8c
--- /dev/null
+++ b/len-core/src/main/java/com/len/util/UploadUtil.java
@@ -0,0 +1,105 @@
+package com.len.util;
+
+import com.len.exception.MyException;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.UUID;
+
+/**
+ * Created by meng on 2018/5/8.
+ * 文件上传工具类
+ */
+@Getter
+@Setter
+@ConfigurationProperties
+@Component
+public class UploadUtil {
+
+ /**
+ * 上传请求资源
+ */
+ private MultipartFile multipartFile = null;
+
+ /**
+ * 按照当日创建文件夹
+ */
+ @Value("${lenosp.isDayType}")
+ private boolean isDayType;
+ /**
+ * 自定义文件路径
+ */
+ @Value("${lenosp.uploadPath}")
+ private String uploadPath;
+
+ @Value("${lenosp.imagePath}")
+ private String imagePath;
+
+ public static final String IMAGE_SUFFIX = "bmp,jpg,png,gif,jpeg";
+
+ private File currentFile;
+
+ public UploadUtil(MultipartFile multipartFile) {
+ this.multipartFile = multipartFile;
+ }
+
+ public UploadUtil() {
+ }
+
+ public String upload() {
+ if (isNull()) {
+ throw new MyException("上传数据/地址获取异常");
+ }
+
+ String fileName = fileNameStyle();
+ try {
+ FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), currentFile);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return fileName;
+ }
+
+ /**
+ * 格式化文件名 默认采用UUID
+ *
+ * @return
+ */
+ public String fileNameStyle() {
+ String curr = multipartFile.getOriginalFilename();
+ int suffixLen = curr.lastIndexOf(".");
+ if (suffixLen == -1) {
+ throw new MyException("文件获取异常");
+ }
+ String suffix = curr.substring(suffixLen, curr.length());
+ int index = Arrays.binarySearch(IMAGE_SUFFIX.split(","),
+ suffix.replace(".", ""));
+
+ curr = UUID.randomUUID() + suffix;
+ String filename=curr;
+ //image 情况
+ curr = StringUtils.isEmpty(imagePath) || index == -1 ?
+ uploadPath + "\\" + curr : imagePath + "\\" + curr;
+ currentFile = new File(curr);
+
+ return filename;
+ }
+
+ private boolean isNull() {
+ if (null != multipartFile) {
+ return false;
+ }
+ return true;
+ }
+
+
+}
diff --git a/len-sys/src/main/java/com/len/controller/UserController.java b/len-sys/src/main/java/com/len/controller/UserController.java
index d674ee5..24e8e25 100644
--- a/len-sys/src/main/java/com/len/controller/UserController.java
+++ b/len-sys/src/main/java/com/len/controller/UserController.java
@@ -9,10 +9,7 @@ import com.len.entity.SysUser;
import com.len.exception.MyException;
import com.len.service.RoleUserService;
import com.len.service.SysUserService;
-import com.len.util.BeanUtil;
-import com.len.util.Checkbox;
-import com.len.util.JsonUtil;
-import com.len.util.Md5Util;
+import com.len.util.*;
import io.swagger.annotations.ApiOperation;
import java.io.*;
@@ -233,8 +230,9 @@ public class UserController extends BaseController {
return j;
}
- @Value("${imagePath}")
- private String imagePath;
+ @Autowired
+ UploadUtil uploadUtil;
+
/**
* 头像上传 目前首先相对路径
*/
@@ -242,18 +240,10 @@ public class UserController extends BaseController {
@ResponseBody
public JsonUtil imgUpload(HttpServletRequest req, @RequestParam("file") MultipartFile file,
ModelMap model) {
+ uploadUtil.setMultipartFile(file);
+ String fileName=uploadUtil.upload();
JsonUtil j = new JsonUtil();
- String imageName=file.getOriginalFilename();
- imageName= UUID.randomUUID()+imageName.substring(imageName.indexOf("."),imageName.length());
- File file2 = new File(imagePath+"\\"+imageName);
- try {
- FileUtils.copyInputStreamToFile(file.getInputStream(), file2);
- } catch (IOException e) {
- j.setFlag(false);
- j.setMsg("上传失败");
- e.printStackTrace();
- }
- j.setMsg(imageName);
+ j.setMsg(fileName);
return j;
}
diff --git a/len-web/src/main/java/com/len/config/WebMvcConfig.java b/len-web/src/main/java/com/len/config/WebMvcConfig.java
index f5f9cd5..a09d041 100644
--- a/len-web/src/main/java/com/len/config/WebMvcConfig.java
+++ b/len-web/src/main/java/com/len/config/WebMvcConfig.java
@@ -25,9 +25,9 @@ import java.util.Locale;
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
- @Value("${imagePath}")
+ @Value("${lenosp.imagePath}")
private String imagePath;
- @Value("${filePath}")
+ @Value("${lenosp.uploadPath}")
private String filePath;
@Bean
diff --git a/len-web/src/main/resources/application-dev.yml b/len-web/src/main/resources/application-dev.yml
index c1bdeef..97d5c6e 100644
--- a/len-web/src/main/resources/application-dev.yml
+++ b/len-web/src/main/resources/application-dev.yml
@@ -1,12 +1,12 @@
server:
- port: 8081
+ port: 8082
spring:
datasource:
url: jdbc:mysql://localhost:3306/lenos?useUnicode=true&characterEncoding=UTF-8
username: root
# password: 123456
# password: 1234
- password: root
+ password: 123456
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
@@ -32,6 +32,7 @@ spring:
prefer-ip: true
url: http://localhost:8082
+
mybatis:
type-aliases-package: com.len.entity
mapper-locations: classpath*:mapper/*.xml
@@ -55,6 +56,8 @@ logging:
com.len.mapper: debug
config: classpath:log4j2.yml
-filePath: ./file/
+lenosp:
+ uploadPath: ./file/
+ isDayType: false
-imagePath: ./image/
\ No newline at end of file
+ imagePath: ./image/
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 79e810c..81abb49 100644
--- a/pom.xml
+++ b/pom.xml
@@ -34,6 +34,12 @@
spring-boot-properties-migrator
-->
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+ true
+
+
org.springframework.boot
spring-boot-starter-web