package wwfww.warehouse.api;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import wwfww.warehouse.base.XCoreResult;
import wwfww.warehouse.base.XCoreResultGenerator;
import wwfww.warehouse.util.IdentityIdGenerator;
import wwfww.warehouse.util.UUIDGeneratorUtil;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.*;
/**
* @版权:版权所有 (c) 2017
* @author:cwj
* @version Revision 1.0.0
* @email:cwjie@betterda.com
* @see:
* @创建日期:2019年11月25日 @功能说明: @begin
* @修改记录:
* @修改后版本 修改人 修改内容
* @2019年5月23日 cwj 创建
* @end
*/
@Slf4j
@Api(tags = "上传模块")
@RestController
@RequestMapping(value = "api/upload")
@MultipartConfig
public class UploadPicturesController {
/**
* @param
* @return
* @author:liuyu
* @email:lyu@betterda.com
* @创建日期:2020年3月5日
* @功能说明:上传图片(可以给app用)
*/
@ApiOperation(value = "上传图片", notes = "上传图片")
@PostMapping(value = "uploadImage")
public XCoreResult<String> uploadImage(HttpServletRequest request) {
try {
Part part = request.getPart("file");
String suffix = getFileSuffix(part);
String path = writeTo(part,suffix);
return XCoreResultGenerator.success(path);
} catch (Exception e) {
log.error(e.getMessage(), e);
return XCoreResultGenerator.error(e.getMessage());
}
}
private String getFileSuffix(Part part) {
String head = part.getHeader("Content-Disposition");
String fileName = head.substring(head.indexOf("filename=\"")+10, head.lastIndexOf("\""));
System.out.println(fileName);
return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
}
private String writeTo(Part part, String suffix)throws IOException {
File newfile = new File("D:\\image");
if (!newfile.exists()) {
newfile.mkdirs();
}
String newurl = "/"+ UUIDGeneratorUtil.buildMallOrderSn()+".jpg";
InputStream in = part.getInputStream();
OutputStream out = new FileOutputStream(newfile+newurl);
byte[] b = new byte[1024];
int length = -1;
while((length = in.read(b))!=-1){
out.write(b, 0, length);
}
in.close();
out.close();
return "upload"+newurl;
}
/**
* @param
* @return
* @创建日期:2020年3月5日
* @功能说明:上传文件,可以是图片可以是tex等等文件
*/
@ApiOperation(value = "上传文件(1M)")
@PostMapping(value = "upload")
public XCoreResult<String> upload(@ApiParam(value = "文件", required = true) MultipartFile file) {
try {
String fileName = file.getOriginalFilename();
String suffix = StringUtils.substringAfterLast(fileName, ".");
String uuid = String.valueOf(String.valueOf(IdentityIdGenerator.getFlowIdInstance().nextId()));
String newFileName = uuid + "." + suffix;
//保存文件的路径
File newfile = new File("D:\\image\\" + uuid + "\\");
//判断是否存在文件夹,不存在则创建,exists()用来判断文件夹是否存在,mkdirs()是创建文件夹
if (!newfile.exists()) {
newfile.mkdirs();
}
//保存文件
file.transferTo(new File(newfile + "\\" + newFileName));
return XCoreResultGenerator.success("成功", newFileName);
} catch (Exception e) {
log.error(e.getMessage(), e);
return XCoreResultGenerator.failure("失败");
}
}
}
upload方法默认文件大小是1M,要改变上传大小的话,修改yml配置文件
spring:
servlet:
multipart:
enabled: true
#设置单个文件上传大小限制
max-file-size: 10MB
#设置文件上传的总容量
max-request-size: 100MB
如果写配置不好用的话,也可以编写配置bean
@Configuration
public class UploadConfig {
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大
factory.setMaxFileSize(DataSize.parse("100MB"));
// 设置总上传数据总大小
factory.setMaxRequestSize(DataSize.parse("100MB"));
return factory.createMultipartConfig();
}
}