package com.whyc.service;
|
|
import com.whyc.dto.Response;
|
import com.whyc.dto.paramter.UserWorkAlarmParam;
|
import com.whyc.mapper.UserWorkMapper;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.system.ApplicationHome;
|
import org.springframework.core.env.Environment;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import java.io.*;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
@Service
|
public class UserWorkService {
|
@Resource
|
private UserWorkMapper mapper;
|
@Autowired
|
private Environment environment;
|
|
|
|
public Response uploadAlarmFile(MultipartFile[] file, UserWorkAlarmParam param) {
|
String fileDirName = "";
|
int configType = Integer.parseInt(environment.getProperty("configFile.type"));
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
File jarFile = applicationHome.getDir();
|
//测试版
|
if(configType==1){
|
fileDirName = jarFile.getParentFile().toString();
|
}else{
|
//打包版
|
fileDirName = jarFile.toString();
|
}
|
String root=fileDirName+"/stationsrc/alarm"+ param.getStationId() + "/" + param.getAfterOrBefore() + "/";
|
List<String> filePathList = new ArrayList<>();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
boolean isSuccess = false;
|
try {
|
for (int i = 0; i < file.length && param != null; i++) {
|
String fileFileName = file[i].getOriginalFilename();
|
fileFileName= fileFileName.replace(".","_"+sdf.format(new Date())+".");
|
String filePath = root + fileFileName;
|
createFilefolderIFNotExist(filePath);
|
file[i].transferTo(new File(filePath));
|
isSuccess = true;
|
filePathList.add(filePath);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
if (isSuccess) {
|
return new Response().set(1,filePathList,"上传成功");
|
} else {
|
return new Response().set(0,false,"上传失败");
|
}
|
|
}
|
//将文件复制到指定的目录
|
public void copyFile(File oldFile, String newPath){
|
File file = new File(newPath);
|
FileInputStream in = null;
|
FileOutputStream out = null;
|
try {
|
in = new FileInputStream(oldFile);
|
out = new FileOutputStream(file);;
|
|
byte[] buffer=new byte[2097152];
|
int readByte = 0;
|
while((readByte = in.read(buffer)) != -1){
|
out.write(buffer, 0, readByte);
|
}
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
if(in != null){
|
try {
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if(out != null){
|
try {
|
out.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
public void createFilefolderIFNotExist(String filePath){
|
File f = new File(filePath);
|
if(!f.exists()){
|
if(!f.getParentFile().exists()){
|
f.getParentFile().mkdirs();
|
}
|
}
|
}
|
|
|
}
|