封装文件上传

This commit is contained in:
zxm 2018-05-08 23:40:37 +08:00
parent 9cbdbf51e7
commit dd39ee4339
5 changed files with 127 additions and 23 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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

View File

@ -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/
imagePath: ./image/

View File

@ -34,6 +34,12 @@
<artifactId>spring-boot-properties-migrator</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>