文件上传修改,路径修改

master
anthonywj 2 years ago
parent 4f7b490d88
commit 164c76858c

@ -16,9 +16,6 @@ import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.idc.service.FileService; import com.glxp.api.idc.service.FileService;
/** /**
* *
*/ */
@ -31,30 +28,28 @@ public class FileController {
@RequestMapping(value = "/spssync/file/upload") @RequestMapping(value = "/spssync/file/upload")
@ResponseBody @ResponseBody
public BaseResponse upload(HttpServletRequest request, @RequestBody Map<String, Object> params) { public BaseResponse upload(HttpServletRequest request, @RequestBody Map<String, Object> params) {
return fileService.fileUpload(request,params); return fileService.fileUpload(request, params);
} }
@RequestMapping(value = "/spssync/file/download") @RequestMapping(value = "/spssync/file/download")
@ResponseBody @ResponseBody
public BaseResponse download(HttpServletRequest request, @RequestBody Map<String, Object> params) { public BaseResponse download(HttpServletRequest request, @RequestBody Map<String, Object> params) {
return fileService.fileDownload(request,params); return fileService.fileDownload(request, params);
} }
@RequestMapping(value = "/spssync/file/downloadFile") @RequestMapping(value = "/spssync/file/downloadFile")
@ResponseBody @ResponseBody
public BaseResponse downloadFile(HttpServletRequest request, @RequestBody Map<String, Object> params) { public BaseResponse downloadFile(HttpServletRequest request, @RequestBody Map<String, Object> params) {
return fileService.downloadFile(request,params); return fileService.downloadFile(request, params);
} }
@RequestMapping(value = "/spssync/file/uploadFile") @RequestMapping(value = "/spssync/file/uploadFile")
public BaseResponse uploadFile(HttpServletRequest request, public BaseResponse uploadFile(HttpServletRequest request,
@RequestParam(value = "content" , required = false) String content, @RequestParam(value = "content", required = false) String content,
@RequestParam(value = "files", required = false) MultipartFile[] files) { @RequestParam(value = "files", required = false) MultipartFile[] files) {
// //
return fileService.receiveFile(request, content, files); return fileService.receiveFile(request, content, files);
} }
} }

@ -38,7 +38,7 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
/*文件服务*/ /*文件服务*/
@Service @Service
public class FileServiceImpl implements FileService { public class FileServiceImpl implements FileService {
private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class); private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);
@ -48,152 +48,155 @@ public class FileServiceImpl implements FileService {
private String apiKey; private String apiKey;
@Value("${API_SECRET}") @Value("${API_SECRET}")
private String apiSecret; private String apiSecret;
@Resource @Resource
private DbDao dbDao; private DbDao dbDao;
@Resource @Resource
private IdcService idcService; private IdcService idcService;
private String imagePath = "register/file/image2/"; private String imagePath = "register/file/image2/";
String pdfPath = "pdf/template/";
@Override @Override
public BaseResponse upload(List<String> list) { public BaseResponse upload(List<String> list) {
return fileToUpload(list); return fileToUpload(list);
} }
@Override @Override
public BaseResponse download(List<String> list) { public BaseResponse download(List<String> list) {
return fileToDownload(list); return fileToDownload(list);
} }
@Override @Override
public BaseResponse fileUpload(HttpServletRequest request,Map<String,Object> params) { public BaseResponse fileUpload(HttpServletRequest request, Map<String, Object> params) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
String[] strs = params.get("fileName").toString().split(","); String[] strs = params.get("fileName").toString().split(",");
for(String str:strs) { for (String str : strs) {
list.add(str); list.add(str);
} }
return fileToUpload(list); return fileToUpload(list);
} }
private BaseResponse fileToUpload(List<String> list) { private BaseResponse fileToUpload(List<String> list) {
String host=""; String host = "";
try { try {
Map<String, Object> map = dbDao.get("select * from sync_data_set limit 1"); Map<String, Object> map = dbDao.get("select * from sync_data_set limit 1");
if(map!=null&&map.get("syncIp")!=null) if (map != null && map.get("syncIp") != null)
host = map.get("syncIp").toString(); host = map.get("syncIp").toString();
} catch (Exception ex) { } catch (Exception ex) {
} }
if(StringUtils.isEmpty(host)) { if (StringUtils.isEmpty(host)) {
try { try {
Map<String,Object> config =dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'"); Map<String, Object> config = dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'");
if(config!=null&&config.get("paramValue")!=null) if (config != null && config.get("paramValue") != null)
host = config.get("paramValue").toString(); host = config.get("paramValue").toString();
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
if(StringUtils.isEmpty(host)) if (StringUtils.isEmpty(host))
return ResultVOUtils.error(9999,"上传地址未配置,请至同步设置中进行设置"); return ResultVOUtils.error(9999, "上传地址未配置,请至同步设置中进行设置");
if(list!=null) { if (list != null) {
String filePathSlash = filePath.substring(filePath.length() - 1).equals("/") ? "" : "/";
ArrayList<String> files = new ArrayList<>(); ArrayList<String> files = new ArrayList<>();
boolean isExists = true; boolean isExists = true;
List<String> noExitsList = new ArrayList<>(); List<String> noExitsList = new ArrayList<>();
for(String strs:list) { for (String strs : list) {
String[] str = strs.split(","); if (!StringUtils.isEmpty(strs) && FileUtils.isFileExist(strs)) {
for(String s:str) { files.add(strs);
if (!StringUtils.isEmpty(s) && FileUtils.isFileExist(filePath + filePathSlash + imagePath + s)) { } else {
files.add(filePath + filePathSlash + imagePath + s); isExists = false;
} else { noExitsList.add(strs);
isExists = false;
noExitsList.add(s);
}
} }
} }
if(!isExists) if (!isExists)
return ResultVOUtils.error(9999,"以下文件不存在:\n"+JSON.toJSONString(noExitsList)); return ResultVOUtils.error(9999, "以下文件不存在:\n" + JSON.toJSONString(noExitsList));
String result = relayFile(files,host); String result = relayFile(files, host);
if (IDCUtils.isJson(result)) { if (IDCUtils.isJson(result)) {
BaseResponse baseResponse = JSON.parseObject(result,BaseResponse.class); BaseResponse baseResponse = JSON.parseObject(result, BaseResponse.class);
return baseResponse; return baseResponse;
} }
} }
return ResultVOUtils.error(9999,"失败"); return ResultVOUtils.error(9999, "失败");
} }
@Override @Override
public BaseResponse fileDownload(HttpServletRequest request,Map<String,Object> params) { public BaseResponse fileDownload(HttpServletRequest request, Map<String, Object> params) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
String[] strs = params.get("fileName").toString().split(","); String[] strs = params.get("fileName").toString().split(",");
for(String str:strs) { for (String str : strs) {
list.add(str); list.add(str);
} }
return fileToDownload(list); return fileToDownload(list);
} }
private BaseResponse fileToDownload(List<String> list) { private BaseResponse fileToDownload(List<String> list) {
Map<String,Object> params = new HashMap<String,Object>(); Map<String, Object> params = new HashMap<String, Object>();
String fileName = ""; String fileName = "";
for(String str:list) { for (String str : list) {
fileName +=fileName.length()>0 ? ","+str : str; fileName += fileName.length() > 0 ? "," + str : str;
} }
params.put("fileName", fileName); params.put("fileName", fileName);
Map<String, Object> map = dbDao.get("select * from sync_data_set limit 1"); Map<String, Object> map = dbDao.get("select * from sync_data_set limit 1");
if(map!=null&&map.get("syncIp")!=null) { if (map != null && map.get("syncIp") != null) {
String result = IDCUtils.post(map.get("syncIp").toString()+"/spssync/file/downloadFile", params); String result = IDCUtils.post(map.get("syncIp").toString() + "/spssync/file/downloadFile", params);
boolean success = false; boolean success = false;
if(IDCUtils.isJson(result)) { if (IDCUtils.isJson(result)) {
JSONObject object = JSON.parseObject(result); JSONObject object = JSON.parseObject(result);
if(object.getInteger("code")==20000) { if (object.getInteger("code") == 20000) {
String[] files = params.get("fileName").toString().split(","); String[] files = params.get("fileName").toString().split(",");
success = true; success = true;
for(String str:files) { for (String str : files) {
if(!idcService.signleDownloadFile(map.get("syncIp").toString(), str)) if (!idcService.signleDownloadFile(map.get("syncIp").toString(), str))
success = false; success = false;
} }
} }
} }
if(!success) if (!success)
return ResultVOUtils.error(9999,"失败"); return ResultVOUtils.error(9999, "失败");
} else { } else {
return ResultVOUtils.error(9999,"中继服务地址未配置,请至同步设置中进行配置"); return ResultVOUtils.error(9999, "中继服务地址未配置,请至同步设置中进行配置");
} }
return ResultVOUtils.success(null); return ResultVOUtils.success(null);
} }
@Override @Override
public BaseResponse receiveFile(HttpServletRequest request,String content,MultipartFile[] files) { public BaseResponse receiveFile(HttpServletRequest request, String content, MultipartFile[] files) {
boolean isRelay = false; boolean isRelay = false;
String filePathSlash = filePath.substring(filePath.length() - 1).equals("/") ? "" : "/"; String filePathSlash = filePath.substring(filePath.length() - 1).equals("/") ? "" : "/";
String host = ""; String host = "";
try { try {
Map<String,Object> config =dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'"); Map<String, Object> config = dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'");
if(config!=null&&config.get("paramValue")!=null) { if (config != null && config.get("paramValue") != null) {
isRelay = true; isRelay = true;
host = config.get("paramValue").toString(); host = config.get("paramValue").toString();
} }
} catch (Exception ex) { } catch (Exception ex) {
} }
ArrayList<String> saveFiles = new ArrayList<>(); ArrayList<String> saveFiles = new ArrayList<>();
Date startTime = new Date(); Date startTime = new Date();
if (files != null) { if (files != null) {
if(!FileUtils.makeDirectory(filePath + filePathSlash + imagePath)) if (!FileUtils.makeDirectory(filePath + filePathSlash + imagePath))
IDCUtils.createDirectory(filePath + filePathSlash + imagePath); IDCUtils.createDirectory(filePath + filePathSlash + imagePath);
try { try {
for (MultipartFile file : files) { for (MultipartFile file : files) {
String imageName = filePath + filePathSlash + imagePath + file.getOriginalFilename(); String flodPath;
if (file.getName().contains(".jrxml") || file.getName().contains(".jasper")) {
flodPath = pdfPath;
} else {
flodPath = imagePath;
}
String imageName = filePath + filePathSlash + flodPath + file.getOriginalFilename();
saveFiles.add(imageName); saveFiles.add(imageName);
IDCUtils.writeFile(file.getBytes(), filePath + filePathSlash + imagePath, file.getOriginalFilename()); IDCUtils.writeFile(file.getBytes(), filePath + filePathSlash + flodPath, file.getOriginalFilename());
} }
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -204,24 +207,24 @@ public class FileServiceImpl implements FileService {
} }
if(isRelay) { if (isRelay) {
String result = relayFile(saveFiles,host); String result = relayFile(saveFiles, host);
logger.info(result); logger.info(result);
if (IDCUtils.isJson(result)&&!result.contains("<html")) { if (IDCUtils.isJson(result) && !result.contains("<html")) {
BaseResponse baseResponse = JSON.parseObject(result,BaseResponse.class); BaseResponse baseResponse = JSON.parseObject(result, BaseResponse.class);
return baseResponse; return baseResponse;
} else { } else {
return ResultVOUtils.error(9999,"上传失败"); return ResultVOUtils.error(9999, "上传失败");
} }
} }
return ResultVOUtils.success(null); return ResultVOUtils.success(null);
} }
/*转发图片*/ /*转发图片*/
private String relayFile(ArrayList<String> files, String ip) { private String relayFile(ArrayList<String> files, String ip) {
String host = ip; String host = ip;
String result = ""; String result = "";
if (!StringUtils.isEmpty(host)) { if (!StringUtils.isEmpty(host)) {
host += "/spssync/file/uploadFile"; host += "/spssync/file/uploadFile";
OkHttpClient client = new OkHttpClient().newBuilder() OkHttpClient client = new OkHttpClient().newBuilder()
@ -244,7 +247,7 @@ public class FileServiceImpl implements FileService {
} }
} }
} }
RequestBody body = builder.build(); RequestBody body = builder.build();
Request req = new Request.Builder() Request req = new Request.Builder()
@ -270,34 +273,34 @@ public class FileServiceImpl implements FileService {
} }
return result; return result;
} }
@Override @Override
public BaseResponse downloadFile(HttpServletRequest request,Map<String,Object> params) { public BaseResponse downloadFile(HttpServletRequest request, Map<String, Object> params) {
String host = ""; String host = "";
try { try {
Map<String,Object> config =dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'"); Map<String, Object> config = dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'");
if(config!=null&&config.get("paramValue")!=null) if (config != null && config.get("paramValue") != null)
host = config.get("paramValue").toString(); host = config.get("paramValue").toString();
} catch (Exception ex) { } catch (Exception ex) {
} }
if(!StringUtils.isEmpty(host)) { if (!StringUtils.isEmpty(host)) {
String result = IDCUtils.post(host+"/spssync/file/downloadFile", params); String result = IDCUtils.post(host + "/spssync/file/downloadFile", params);
JSONObject object = JSON.parseObject(result); JSONObject object = JSON.parseObject(result);
boolean success = false; boolean success = false;
if(object.getInteger("code")==20000) { if (object.getInteger("code") == 20000) {
String[] files = params.get("fileName").toString().split(","); String[] files = params.get("fileName").toString().split(",");
success = true; success = true;
for(String str:files) { for (String str : files) {
if(!idcService.signleDownloadFile(host, str)) if (!idcService.signleDownloadFile(host, str))
success = false; success = false;
} }
} }
if(!success) if (!success)
ResultVOUtils.error(9999,"失败"); ResultVOUtils.error(9999, "失败");
} }
return ResultVOUtils.success(null); return ResultVOUtils.success(null);
} }
} }

@ -1023,7 +1023,27 @@ public class HeartService {
if (dataResponse != null) { if (dataResponse != null) {
dataResponse.setTaskId(taskId); dataResponse.setTaskId(taskId);
dataResponse.setType(BasicExportTypeEnum.SYS_SET_DATA.getRemark()); dataResponse.setType(BasicExportTypeEnum.SYS_SET_DATA.getRemark());
if (CollUtil.isNotEmpty(dataResponse.getSystemPDFTemplateEntities())) {
// List<Map<String, String>> list = new ArrayList<>(spsSyncSysSettingResponse.getSystemPDFTemplateEntities().size() * 2);
List<String> list = new ArrayList<>();
dataResponse.getSystemPDFTemplateEntities().forEach(systemPDFTemplateEntity -> {
list.add(systemPDFTemplateEntity.getPath());
});
BaseResponse<String> response = spGetHttp.postTemplateFile(list);
if (response.getCode() == 20000) {
log.info("模板文件列表上传成功");
// uploadFileLog = "\n模板文件列表上传成功";
} else {
log.info("模板文件上传失败,响应信息:{}", response.getMessage());
// uploadFileLog = "\n模板文件列表上传失败错误信息" + response.getMessage();
}
}
} }
return dataResponse; return dataResponse;
} }

Loading…
Cancel
Save