增加单独文件上传
parent
f0b483dfc7
commit
745af75f10
@ -0,0 +1,49 @@
|
|||||||
|
package com.glxp.api.idc.controller;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.glxp.api.common.res.BaseResponse;
|
||||||
|
import com.glxp.api.idc.service.FileService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中继服务接口
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class FileController {
|
||||||
|
@Resource
|
||||||
|
FileService fileService;
|
||||||
|
|
||||||
|
@RequestMapping(value = "/spssync/file/upload")
|
||||||
|
@ResponseBody
|
||||||
|
public BaseResponse connect(HttpServletRequest request, @RequestBody Map<String, Object> params) {
|
||||||
|
return fileService.fileUpload(request,params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping(value = "/spssync/file/uploadFile")
|
||||||
|
public BaseResponse uploadFile(HttpServletRequest request,
|
||||||
|
@RequestParam(value = "content" , required = false) String content,
|
||||||
|
@RequestParam(value = "files", required = false) MultipartFile[] files) {
|
||||||
|
//
|
||||||
|
return fileService.receiveFile(request, content, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.glxp.api.idc.service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.glxp.api.common.res.BaseResponse;
|
||||||
|
|
||||||
|
|
||||||
|
/*文件服务*/
|
||||||
|
public interface FileService {
|
||||||
|
BaseResponse fileUpload(HttpServletRequest request,Map<String,Object> params);
|
||||||
|
BaseResponse receiveFile(HttpServletRequest request,String content,MultipartFile[] files);
|
||||||
|
}
|
@ -0,0 +1,193 @@
|
|||||||
|
package com.glxp.api.idc.service.impl;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.tools.ant.util.DateUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.glxp.api.common.res.BaseResponse;
|
||||||
|
import com.glxp.api.common.util.ResultVOUtils;
|
||||||
|
import com.glxp.api.dao.idc.DbDao;
|
||||||
|
import com.glxp.api.idc.service.FileService;
|
||||||
|
import com.glxp.api.idc.utils.IDCUtils;
|
||||||
|
import com.glxp.api.util.FileUtils;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.MultipartBody;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
|
||||||
|
/*连通检测*/
|
||||||
|
@Service
|
||||||
|
public class FileServiceImpl implements FileService {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);
|
||||||
|
@Value("${file_path}")
|
||||||
|
private String filePath;
|
||||||
|
@Value("${API_KEY}")
|
||||||
|
private String apiKey;
|
||||||
|
@Value("${API_SECRET}")
|
||||||
|
private String apiSecret;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DbDao dbDao;
|
||||||
|
private String imagePath = "register/file/image2/";
|
||||||
|
@Override
|
||||||
|
public BaseResponse fileUpload(HttpServletRequest request,Map<String,Object> params) {
|
||||||
|
String host="";
|
||||||
|
try {
|
||||||
|
Map<String, Object> map = dbDao.get("select * from sync_data_set limit 1");
|
||||||
|
if(map!=null&&map.get("syncIp")!=null)
|
||||||
|
host = map.get("syncIp").toString();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
if(StringUtils.isEmpty(host)) {
|
||||||
|
try {
|
||||||
|
Map<String,Object> config =dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'");
|
||||||
|
if(config!=null&&config.get("paramValue")!=null)
|
||||||
|
host = config.get("paramValue").toString();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(StringUtils.isEmpty(host))
|
||||||
|
return ResultVOUtils.error(9999,"上传地址未配置,请至同步设置中进行设置");
|
||||||
|
if(params.get("fileName")!=null) {
|
||||||
|
String fileName = params.get("fileName").toString();
|
||||||
|
String[] strs = fileName.split(",");
|
||||||
|
String filePathSlash = filePath.substring(filePath.length() - 1).equals("/") ? "" : "/";
|
||||||
|
ArrayList<String> files = new ArrayList<>();
|
||||||
|
boolean isExists = true;
|
||||||
|
for(String str:strs) {
|
||||||
|
if (!StringUtils.isEmpty(str) && FileUtils.isFileExist(filePath + filePathSlash + imagePath + str)) {
|
||||||
|
files.add(filePath + filePathSlash + imagePath + str);
|
||||||
|
} else {
|
||||||
|
isExists = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!isExists)
|
||||||
|
return ResultVOUtils.error(9999,"文件不存在");
|
||||||
|
String result = relayFile(files,host);
|
||||||
|
if (IDCUtils.isJson(result)) {
|
||||||
|
BaseResponse baseResponse = JSON.parseObject(result,BaseResponse.class);
|
||||||
|
return baseResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ResultVOUtils.error(9999,"失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseResponse receiveFile(HttpServletRequest request,String content,MultipartFile[] files) {
|
||||||
|
boolean isRelay = false;
|
||||||
|
String filePathSlash = filePath.substring(filePath.length() - 1).equals("/") ? "" : "/";
|
||||||
|
String host = "";
|
||||||
|
try {
|
||||||
|
Map<String,Object> config =dbDao.get("select paramValue from system_param_config where paramKey='upper_server_ip'");
|
||||||
|
if(config!=null&&config.get("paramValue")!=null) {
|
||||||
|
isRelay = true;
|
||||||
|
host = config.get("paramValue").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
ArrayList<String> saveFiles = new ArrayList<>();
|
||||||
|
Date startTime = new Date();
|
||||||
|
if (files != null) {
|
||||||
|
try {
|
||||||
|
for (MultipartFile file : files) {
|
||||||
|
String imageName = filePath + filePathSlash + imagePath + file.getOriginalFilename();
|
||||||
|
saveFiles.add(imageName);
|
||||||
|
IDCUtils.writeFile(file.getBytes(), filePath + filePathSlash + imagePath, file.getOriginalFilename());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isRelay) {
|
||||||
|
String result = relayFile(saveFiles,host);
|
||||||
|
if (IDCUtils.isJson(result)) {
|
||||||
|
BaseResponse baseResponse = JSON.parseObject(result,BaseResponse.class);
|
||||||
|
return baseResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ResultVOUtils.success(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*转发图片*/
|
||||||
|
private String relayFile(ArrayList<String> files, String ip) {
|
||||||
|
String host = ip;
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
if (!StringUtils.isEmpty(host)) {
|
||||||
|
host += "/spssync/file/uploadFile";
|
||||||
|
OkHttpClient client = new OkHttpClient().newBuilder()
|
||||||
|
.connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间
|
||||||
|
.build();
|
||||||
|
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
||||||
|
|
||||||
|
MultipartBody.Builder builder = new MultipartBody.Builder();
|
||||||
|
builder.setType(MultipartBody.FORM);
|
||||||
|
String fileType = "application/octet-stream";
|
||||||
|
if (files != null && files.size() > 0) {
|
||||||
|
for (int i = 0; i < files.size(); i++) {
|
||||||
|
if (!StringUtils.isEmpty(files.get(i))) {
|
||||||
|
File file = new File(files.get(i));
|
||||||
|
builder.addFormDataPart("files", files.get(i),
|
||||||
|
RequestBody.create(MediaType.parse(fileType), file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RequestBody body = builder.build();
|
||||||
|
|
||||||
|
Request req = new Request.Builder()
|
||||||
|
.url(host)
|
||||||
|
.method("POST", body)
|
||||||
|
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
.addHeader("format", "json")
|
||||||
|
.addHeader("apiKey", apiKey)
|
||||||
|
.addHeader("secretKey", apiSecret)
|
||||||
|
.addHeader("timestamp", DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"))
|
||||||
|
.addHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept")
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(req).execute();
|
||||||
|
result = response.body().string();
|
||||||
|
logger.info("result--->" + result);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
logger.debug("未配置中继服务地址");
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue