图片下载

master
admin 2 years ago
parent 448b015ed0
commit 61f69be34e

@ -116,9 +116,13 @@ public class IdcServiceImpl implements IdcService {
Map<String,Object> map = dbDao.get("select * from basic_export_status where id='"+params.get("taskId")+"'"); Map<String,Object> map = dbDao.get("select * from basic_export_status where id='"+params.get("taskId")+"'");
if(map==null) if(map==null)
ResultVOUtils.error(9000, "下载任务不存在"); ResultVOUtils.error(9000, "下载任务不存在");
if(!FileUtils.isFileExist(map.get("cacheFilePath").toString()))
ResultVOUtils.error(9000, "下载文件不存在");
String json = FileUtils.readFileAll(map.get("cacheFilePath").toString()); String json = FileUtils.readFileAll(map.get("cacheFilePath").toString());
JSONObject object = JSON.parseObject(json); JSONObject object = JSON.parseObject(json);
if(object==null)
ResultVOUtils.error(9000, "任务下载失败");
if(object!=null&&object.getInteger("code")!=null) { if(object!=null&&object.getInteger("code")!=null) {
BaseResponse response = JSON.parseObject(json,BaseResponse.class); BaseResponse response = JSON.parseObject(json,BaseResponse.class);
return response; return response;
@ -147,14 +151,20 @@ public class IdcServiceImpl implements IdcService {
public void asyncFetchUdiTask() { public void asyncFetchUdiTask() {
fetchTask(false); fetchTask(false);
} }
@Async @Async
@Override @Override
public void asyncFetchTask() { public void asyncFetchTask() {
initTable();
Map<String,Object> map = dbDao.get("select * from idc_var where code='system_type'");
if(map==null) {
dbDao.save("insert into idc_var (code,content) values ('system_type','IDC')");
}
fetchTask(true); fetchTask(true);
} }
private void fetchTask(boolean isIdc) { private void fetchTask(boolean isIdc) {
String host = getNextHost(); String host = getNextHost();
if(!isIdc) { if(!isIdc) {
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");
@ -284,22 +294,20 @@ public class IdcServiceImpl implements IdcService {
JSONObject json = JSONObject.parseObject(result); JSONObject json = JSONObject.parseObject(result);
if(isLastLevel()) { if(isLastLevel()) {
if(json.get("code")!=null) { if(json.get("code")!=null&&json.getInteger("code")==2000) {
success = analyMiddle(host,json.getJSONObject("data"),files,false,true); success = analyMiddle(host,json.getJSONObject("data"),files,false,true);
if(!success)
logger.info("fetchData-->解析失败");
} else { } else {
success = analyMiddle(host,json,files,false,true); success = false;
} }
if(!success)
logger.info("fetchData-->解析失败");
} else { } else {
if(json.get("code")!=null) { if(json.get("code")!=null&&json.get("data")!=null) {
logger.info("res1-->"+JSON.toJSONString(json));
syncAddTaskStatus(json.getJSONObject("data"),false,success); syncAddTaskStatus(json.getJSONObject("data"),false,success);
analyMiddle(host,json.getJSONObject("data"),files,false,false); analyMiddle(host,json.getJSONObject("data"),files,false,false);
} else { }
syncAddTaskStatus(json,false,success);
analyMiddle(host,json,files,false,false);
}
} }
if(success) { if(success) {
@ -425,10 +433,14 @@ public class IdcServiceImpl implements IdcService {
return ResultVOUtils.error(9999, ""); return ResultVOUtils.error(9999, "");
} }
@Override @Override
public void downloadFile(String fileName,HttpServletResponse response) { public void downloadFile(String fileName,HttpServletResponse response) {
OutputStream os; OutputStream os;
String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : fileName.substring(0,1).equals("/") ? "" : "/"; String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : fileName.substring(0,1).equals("/") ? "" : "/";
if(!FileUtils.isFileExist(filePath+filePathSlash+fileName))
fileName = "11.jpg";
logger.info(filePath+filePathSlash+fileName);
try { try {
if(FileUtils.isFileExist(filePath+filePathSlash+fileName)) { if(FileUtils.isFileExist(filePath+filePathSlash+fileName)) {
byte[] bytes = FileUtils.readFileByBytes(filePath+filePathSlash+fileName); byte[] bytes = FileUtils.readFileByBytes(filePath+filePathSlash+fileName);
@ -554,8 +566,14 @@ public class IdcServiceImpl implements IdcService {
List<Map> list = JSONObject.parseArray(JSON.toJSONString(data.get("data")), Map.class); List<Map> list = JSONObject.parseArray(JSON.toJSONString(data.get("data")), Map.class);
if(list!=null&&list.size()>0) { if(list!=null&&list.size()>0) {
for(int m=0;m<list.size();m++) { for(int m=0;m<list.size();m++) {
if(!StringUtils.isEmpty(filePathColumn)&&list.get(m).get(filePathColumn)!=null) { if(!StringUtils.isEmpty(filePathColumn)&&list.get(m).get(filePathColumn)!=null) {
files.add(filePath+filePathSlash+"register/file/image2/"+list.get(m).get(filePathColumn).toString()); String fileNames = list.get(m).get(filePathColumn).toString();
String[] str = fileNames.split(",");
for(int r=0;r<str.length;r++) {
if(!StringUtils.isEmpty(str[r]))
files.add(filePath+filePathSlash+"register/file/image2/"+str[r]);
}
} }
recordSql+=!StringUtils.isEmpty(recordSql) ? "," : ""; recordSql+=!StringUtils.isEmpty(recordSql) ? "," : "";
recordSql+="('"+UUID.randomUUID().toString().replaceAll("-", "")+"','"+tableName+"','"+list.get(m).get(keyColumn)+"','"+isEnd+"',now())"; recordSql+="('"+UUID.randomUUID().toString().replaceAll("-", "")+"','"+tableName+"','"+list.get(m).get(keyColumn)+"','"+isEnd+"',now())";
@ -573,7 +591,10 @@ public class IdcServiceImpl implements IdcService {
if(!StringUtils.isEmpty(chidTnames[7])&&childList.size()>0) { if(!StringUtils.isEmpty(chidTnames[7])&&childList.size()>0) {
for(int x=0;x<childList.size();x++) { for(int x=0;x<childList.size();x++) {
if(childList.get(i).get(chidTnames[7])!=null) { if(childList.get(i).get(chidTnames[7])!=null) {
files.add(filePath+filePathSlash+"register/file/image2/"+childList.get(i).get(chidTnames[7])); String[] str = childList.get(i).get(chidTnames[7]).toString().split(",");
for(int s=0;s<str.length;s++) {
files.add(filePath+filePathSlash+"register/file/image2/"+str[s]);
}
} }
} }
} }
@ -626,28 +647,33 @@ public class IdcServiceImpl implements IdcService {
/*增加同步任务状态*/ /*增加同步任务状态*/
private void syncAddTaskStatus(Map<String,Object> json,boolean isUpload,boolean success) { private void syncAddTaskStatus(Map<String,Object> json,boolean isUpload,boolean success) {
String content = JSON.toJSONString(json); try
String datePath = DateUtil.formatDate(new Date(),"yyyy-MM-dd"); {
String fileName = filePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi"; String content = JSON.toJSONString(json);
String backFileName = backFilePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi"; String datePath = DateUtil.formatDate(new Date(),"yyyy-MM-dd");
if(!FileUtils.makeDirectory( filePath +datePath)) String fileName = filePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi";
IDCUtils.createDirectory(filePath +datePath+"/"); String backFileName = backFilePath +datePath+"/"+json.get("messageType")+"-"+json.get("messageId")+".udi";
if(!FileUtils.makeDirectory(backFilePath +datePath)) if(!FileUtils.makeDirectory( filePath +datePath))
IDCUtils.createDirectory(filePath +datePath+"/"); IDCUtils.createDirectory(filePath +datePath+"/");
if(!FileUtils.makeDirectory(backFilePath +datePath))
FileUtils.SaveFileAs(content, fileName); IDCUtils.createDirectory(filePath +datePath+"/");
FileUtils.SaveFileAs(content, backFileName);
FileUtils.SaveFileAs(content, fileName);
//type,transportType,superiorService,subordinateService,cacheFilePath FileUtils.SaveFileAs(content, backFileName);
Map<String,Object> map = new HashMap<>();
map.put("type", json.get("messageType")); //type,transportType,superiorService,subordinateService,cacheFilePath
map.put("transportType", isUpload ? "1" : "2"); Map<String,Object> map = new HashMap<>();
map.put("scheduleType", isUpload ? "1" : "0"); map.put("type", json.get("messageType"));
map.put("taskId", json.get("messageId")); map.put("transportType", isUpload ? "1" : "2");
map.put("cacheFilePath", fileName); map.put("scheduleType", isUpload ? "1" : "0");
map.put("status", success ? "1" : "0"); map.put("taskId", json.get("messageId"));
map.put("cacheFilePath", fileName);
saveExportStatus(map); map.put("status", success ? "1" : "0");
saveExportStatus(map);
} catch (Exception ex) {
logger.error(ex.getMessage());
}
} }
@ -698,7 +724,7 @@ public class IdcServiceImpl implements IdcService {
for(int z=0;z<30;z++) { for(int z=0;z<30;z++) {
if(list.get(i).get("data"+z)!=null) { if(list.get(i).get("data"+z)!=null) {
if(list.get(i).get("filePathColumn"+z)!=null) { if(list.get(i).get("filePathColumn"+z)!=null) {
List<Map<String,Object>> childList = (ArrayList<Map<String,Object>>)list.get(i).get("data"+z); List<Map<String,Object>> childList = (List<Map<String,Object>>)list.get(i).get("data"+z);
analyToFile(host,list.get(i).get("filePathColumn"+z).toString(),childList); analyToFile(host,list.get(i).get("filePathColumn"+z).toString(),childList);
} }
} else { } else {
@ -903,29 +929,34 @@ public class IdcServiceImpl implements IdcService {
boolean success = true; boolean success = true;
for(String fileName:files) { for(String fileName:files) {
if(!StringUtils.isEmpty(fileName)) { if(!StringUtils.isEmpty(fileName)) {
if(!signleDownloadFile(syncIp,fileName)) String[] str = fileName.split(",");
success = false; for(int i=0;i<str.length;i++) {
if(!StringUtils.isEmpty(str[i])) {
logger.info("downloadFile-->"+str[i]);
if(!signleDownloadFile(syncIp,str[i]))
success = false;
}
}
} }
} }
return success; return success;
} }
private boolean signleDownloadFile(String syncIp,String fileName) { private boolean signleDownloadFile(String syncIp,String fileName) {
OkHttpClient client = new OkHttpClient(); OkHttpClient client = new OkHttpClient().newBuilder()
RequestBody requestBody = new MultipartBody.Builder() .build();;
.setType(MultipartBody.FORM) MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
.addFormDataPart("fileName", fileName)
.build(); RequestBody body = RequestBody.create(mediaType, "fileName="+fileName);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(syncIp+"/spssync/common/downloadFile") .url(syncIp+"/spssync/common/downloadFile")
.post(requestBody) .post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build(); .build();
try { try {
Response result = client.newCall(request).execute(); Response result = client.newCall(request).execute();
if (MediaType.parse("application/force-download").equals(result.body().contentType())) { if (MediaType.parse("application/force-download").equals(result.body().contentType())) {
try (InputStream inputStream = result.body().byteStream()) { try (InputStream inputStream = result.body().byteStream()) {
String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : "/"; String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : "/";

Loading…
Cancel
Save