public static Sm savePic(MultipartFile multipartFile, String type) {
FileOutputStream out = null;
String path = null;
Sm sm = null;
File newFile ;
try {
byte[] bytes = multipartFile.getBytes();
if (isWindows()) {
path = Config.WINPATH + Config.WINLEVEL + type ;
newFile = new File(path);
if(!newFile.exists()){
newFile.mkdirs();
path = path+ Config.WINLEVEL + multipartFile.getOriginalFilename();
newFile = new File(path);
}else{
path = path+ Config.WINLEVEL + multipartFile.getOriginalFilename();
newFile = new File(path);
}
} else {
newFile = new File(path);
path = Config.LINUXPATH + Config.LINUXLEVEL + type ;
if(!newFile.exists()){
newFile.mkdirs();
path = path +Config.LINUXLEVEL + multipartFile.getOriginalFilename();
newFile = new File(path);
}else{
path = path +Config.LINUXLEVEL + multipartFile.getOriginalFilename();
newFile = new File(path);
}
}
if (!newFile.exists()) {
out = new FileOutputStream(path);
out.write(bytes);
out.flush();
out.close();
System.out.println("成功保存至本地");
sm = Sm.builder().code(Config.SUCCESS).data(LocalDateTime.now()).msg(Config.SUCCESSUPLOAD).path(path).build();
} else {
sm = Sm.builder().code(Config.FAILED).data(LocalDateTime.now()).msg("文件已存在").path(path).build();
}
} catch (IOException e) {
sm = Sm.builder().code(Config.FAILED).data(LocalDateTime.now()).msg(Config.UPLOADFAILED+e.getMessage()).path(path).build();
} finally {
if(null != out){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sm;
}