From 6ed366abe0646b40408715a1070b160c832a7912 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Fri, 25 Feb 2022 15:13:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8A=E4=BC=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 +- .../com/xkrs/model/vo/FileServerResultVo.java | 67 +++++++ .../xkrs/model/vo/FileServiceResDetailVo.java | 170 ++++++++++++++++ .../com/xkrs/service/FileServerService.java | 64 ++++++ .../service/impl/FileServerServiceImpl.java | 150 ++++++++++++++ .../java/com/xkrs/util/FileServerTool.java | 91 +++++++++ src/main/java/com/xkrs/util/RequestUtil.java | 188 ++++++++++++++++++ src/main/resources/application.properties | 2 + 8 files changed, 737 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/xkrs/model/vo/FileServerResultVo.java create mode 100644 src/main/java/com/xkrs/model/vo/FileServiceResDetailVo.java create mode 100644 src/main/java/com/xkrs/service/FileServerService.java create mode 100644 src/main/java/com/xkrs/service/impl/FileServerServiceImpl.java create mode 100644 src/main/java/com/xkrs/util/FileServerTool.java create mode 100644 src/main/java/com/xkrs/util/RequestUtil.java diff --git a/pom.xml b/pom.xml index 2458a4b..41260f0 100644 --- a/pom.xml +++ b/pom.xml @@ -153,7 +153,11 @@ 6.0.35 - + + org.apache.httpcomponents + httpmime + 4.5.1 + diff --git a/src/main/java/com/xkrs/model/vo/FileServerResultVo.java b/src/main/java/com/xkrs/model/vo/FileServerResultVo.java new file mode 100644 index 0000000..77fb534 --- /dev/null +++ b/src/main/java/com/xkrs/model/vo/FileServerResultVo.java @@ -0,0 +1,67 @@ +package com.xkrs.model.vo; + +import java.io.Serializable; + +/** + * FileServerResultVo 文件服务器查询结果 + * @author tajochen + */ +public class FileServerResultVo implements Serializable { + + /** + * 状态信息 + */ + String status; + + /** + * 提示信息 + */ + String message; + + /** + * 数据 + */ + T data; + + public FileServerResultVo() { + } + + public FileServerResultVo(String status, String msg, T data) { + this.status = status; + this.message = msg; + this.data = data; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + @Override + public String toString() { + return "FileServerResultVo{" + + "status='" + status + '\'' + + ", message='" + message + '\'' + + ", data=" + data + + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/vo/FileServiceResDetailVo.java b/src/main/java/com/xkrs/model/vo/FileServiceResDetailVo.java new file mode 100644 index 0000000..7ff9f6c --- /dev/null +++ b/src/main/java/com/xkrs/model/vo/FileServiceResDetailVo.java @@ -0,0 +1,170 @@ +package com.xkrs.model.vo; + +import java.io.Serializable; + +/** + * FileServiceResDetailVo + * @author tajochen + */ +public class FileServiceResDetailVo implements Serializable { + + /** + * 域名 + */ + String domain; + + /** + * 信息摘要 + */ + String md5; + + /** + * 上传时间戳 + */ + Long mtime; + + /** + * 文件路径 + */ + String path; + + /** + * 返回信息代码 + */ + int retcode; + + /** + * 返回信息文本 + */ + String retmsg; + + /** + * 场景 + */ + String scene; + + /** + * 场景集合 + */ + String scenes; + + /** + * 文件大小 + */ + long size; + + /** + * 文件路径 + */ + String src; + + /** + * 文件完整链接 + */ + String url; + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + + public String getMd5() { + return md5; + } + + public void setMd5(String md5) { + this.md5 = md5; + } + + public Long getMtime() { + return mtime; + } + + public void setMtime(Long mtime) { + this.mtime = mtime; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public int getRetcode() { + return retcode; + } + + public void setRetcode(int retcode) { + this.retcode = retcode; + } + + public String getRetmsg() { + return retmsg; + } + + public void setRetmsg(String retmsg) { + this.retmsg = retmsg; + } + + public String getScene() { + return scene; + } + + public void setScene(String scene) { + this.scene = scene; + } + + public String getScenes() { + return scenes; + } + + public void setScenes(String scenes) { + this.scenes = scenes; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public String getSrc() { + return src; + } + + public void setSrc(String src) { + this.src = src; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + @Override + public String toString() { + return "FileServiceResVo{" + + "domain='" + domain + '\'' + + ", md5='" + md5 + '\'' + + ", mtime=" + mtime + + ", path='" + path + '\'' + + ", retcode=" + retcode + + ", retmsg='" + retmsg + '\'' + + ", scene='" + scene + '\'' + + ", scenes='" + scenes + '\'' + + ", size=" + size + + ", src='" + src + '\'' + + ", url='" + url + '\'' + + '}'; + } +} diff --git a/src/main/java/com/xkrs/service/FileServerService.java b/src/main/java/com/xkrs/service/FileServerService.java new file mode 100644 index 0000000..b68e9b9 --- /dev/null +++ b/src/main/java/com/xkrs/service/FileServerService.java @@ -0,0 +1,64 @@ +package com.xkrs.service; + +import com.xkrs.model.vo.FileServerResultVo; +import com.xkrs.model.vo.FileServiceResDetailVo; + +import java.io.File; + +/** + * 文件服务器接口 + * @author tajochen + */ +public interface FileServerService { + + /** + * 上传文件 + * @param postFile 文件 + * @param path 路径 + * @return + */ + FileServiceResDetailVo uploadFile(File postFile, String path); + + /** + * 获取文件统计信息 + * @return + */ + FileServerResultVo getFileStat(); + + /** + * 删除文件 + * @param md5 信息摘要 + * @param path 路径 + * @return + */ + FileServerResultVo deleteFile(String md5,String path); + + /** + * 获取文件信息 + * @param md5 信息摘要 + * @param path 路径 + * @return + */ + FileServerResultVo getFileInfo(String md5,String path); + + /** + * 获取文件列表 + * @param dir 目录名 + * @return + */ + FileServerResultVo getFileList(String dir); + + /** + * 修复统计信息 + * @param date 要修复的日期,格式如:20190725 + * @return + */ + FileServerResultVo getFileRepairStat(String date); + + /** + * 同步失败修复 + * @param force 是否强行修复(0|1) + * @return + */ + FileServerResultVo getFileRepair(String force); +} diff --git a/src/main/java/com/xkrs/service/impl/FileServerServiceImpl.java b/src/main/java/com/xkrs/service/impl/FileServerServiceImpl.java new file mode 100644 index 0000000..e0fef94 --- /dev/null +++ b/src/main/java/com/xkrs/service/impl/FileServerServiceImpl.java @@ -0,0 +1,150 @@ +package com.xkrs.service.impl; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.xkrs.model.vo.FileServerResultVo; +import com.xkrs.model.vo.FileServiceResDetailVo; +import com.xkrs.service.FileServerService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import static com.xkrs.util.FileServerTool.postFileManage; +import static com.xkrs.util.RequestUtil.postFile; + +/** + * 文件服务器接口 + * @author tajochen + */ +@Service +public class FileServerServiceImpl implements FileServerService { + + Logger logger = LoggerFactory.getLogger(FileServerServiceImpl.class); + + @Value("${my.FileServerAdminAdress}") + private String fileServerAdminAdress; + + @Value("${my.FileServerAdress}") + private String fileServerAdress; + + /** + * 上传文件 + * @param theFile 本地文件 + * @param path 文件服务器路径 项目名/功能模块名/具体分类名根据业务定 + * @return + */ + @Override + public FileServiceResDetailVo uploadFile(File theFile, String path){ + Map postParam = new HashMap<>(16); + postParam.put("output","json"); + postParam.put("path",path); + postParam.put("scene",""); + String resFileServer = postFile(theFile, fileServerAdress+"/group1/upload", postParam); + ObjectMapper objectMapper = new ObjectMapper(); + FileServiceResDetailVo result = null; + try { + result = objectMapper.readValue(resFileServer,FileServiceResDetailVo.class); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return result; + } + + /** + * 获取文件统计信息 + * @return + */ + @Override + public FileServerResultVo getFileStat(){ + String requestUrl = fileServerAdminAdress + "/group1/stat"; + Map map = new HashMap<>(16); + return postFileManage(requestUrl,map); + } + + /** + * 删除文件 + * @param md5 信息摘要 + * @param path 路径 + * @return + */ + @Override + public FileServerResultVo deleteFile(String md5,String path){ + String requestUrl = fileServerAdminAdress + "/group1/delete"; + Map map = new HashMap<>(16); + if(md5 != null && !md5.isEmpty()){ + map.put("md5",md5); + } + if(path != null && !path.isEmpty()){ + map.put("path",path); + } + return postFileManage(requestUrl,map); + } + + /** + * 获取文件信息 + * @param md5 信息摘要 + * @param path 路径 + * @return + */ + @Override + public FileServerResultVo getFileInfo(String md5,String path){ + String requestUrl = fileServerAdminAdress + "/group1/get_file_info"; + Map map = new HashMap<>(16); + if(md5 != null && !md5.isEmpty()){ + map.put("md5",md5); + } + if(path != null && !path.isEmpty()){ + map.put("path",path); + } + return postFileManage(requestUrl,map); + } + + /** + * 获取文件列表 + * @param dir 目录名 + * @return + */ + @Override + public FileServerResultVo getFileList(String dir){ + String requestUrl = fileServerAdminAdress + "/group1/list_dir"; + Map map = new HashMap<>(16); + map.put("dir",dir); + return postFileManage(requestUrl,map); + } + + /** + * 修复统计信息 + * @param date 要修复的日期,格式如:20190725 + * @return + */ + @Override + public FileServerResultVo getFileRepairStat(String date){ + String requestUrl = fileServerAdminAdress + "/group1/repair_stat"; + Map map = new HashMap<>(16); + map.put("date",date); + return postFileManage(requestUrl,map); + } + + /** + * 同步失败修复 + * @param force 是否强行修复(0|1) + * @return + */ + @Override + public FileServerResultVo getFileRepair(String force){ + String requestUrl = fileServerAdminAdress + "/group1/repair"; + Map map = new HashMap<>(16); + map.put("force",force); + return postFileManage(requestUrl,map); + } + + public static void main(){ + + } + +} diff --git a/src/main/java/com/xkrs/util/FileServerTool.java b/src/main/java/com/xkrs/util/FileServerTool.java new file mode 100644 index 0000000..7d3dd3e --- /dev/null +++ b/src/main/java/com/xkrs/util/FileServerTool.java @@ -0,0 +1,91 @@ +package com.xkrs.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.xkrs.model.vo.FileServerResultVo; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * 文件服务器工具 + * @author tajochen + */ +public class FileServerTool { + + private static final String FIREFOX_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) " + + "Gecko/20100101 Firefox/91.0"; + + /** + * 发送管理命令到文件服务器 + * @param url 文件服务器路径 + * @param map 命令参数集 + * @return + */ + public static FileServerResultVo postFileManage(String url, Map map){ + Logger log = LoggerFactory.getLogger(RequestUtil.class); + String body = ""; + // 创建httpclient对象 + CloseableHttpClient client = HttpClients.createDefault(); + // 创建post方式请求对象 + HttpPost httpPost = new HttpPost(url); + // 装填参数 + List nvps = new ArrayList<>(); + if(map!=null){ + for (Map.Entry entry : map.entrySet()) { + nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + } + try { + // 设置参数到请求对象中 + httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); + // 设置header报文头信息 + httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); + httpPost.setHeader("User-Agent", FIREFOX_UA); + httpPost.setHeader("Accept", "application"); + httpPost.setHeader("Accept-Encoding", "gzip, deflate"); + // 执行请求操作,并拿到结果(同步阻塞) + CloseableHttpResponse response = client.execute(httpPost); + // 获取结果实体 + HttpEntity entity = response.getEntity(); + if (entity != null) { + // 按指定编码转换结果实体为String类型 + body = EntityUtils.toString(entity, "UTF-8"); + } + EntityUtils.consume(entity); + // 释放链接 + response.close(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + FileServerResultVo fileServerResultVo = new FileServerResultVo(); + ObjectMapper mapper = new ObjectMapper(); + try { + fileServerResultVo = mapper.readValue(body, FileServerResultVo.class); + } catch (JsonProcessingException e) { + e.printStackTrace(); + log.warn(e.toString()); + + } + return fileServerResultVo; + } +} diff --git a/src/main/java/com/xkrs/util/RequestUtil.java b/src/main/java/com/xkrs/util/RequestUtil.java new file mode 100644 index 0000000..38c9154 --- /dev/null +++ b/src/main/java/com/xkrs/util/RequestUtil.java @@ -0,0 +1,188 @@ +package com.xkrs.util; + +import org.apache.http.Consts; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.*; + +/** + * http请求工具 + * @author tajochen + **/ +public class RequestUtil { + + private static final String FIREFOX_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0"; + private static final String CHROME_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + + "(KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"; + + /** + * 模拟 get请求 + * @param url 链接 + * @param map 参数列表 + */ + public static String getStandard(String url, Map map) { + String body = ""; + // 1.拿到一个httpclient的对象 + CloseableHttpClient httpClient = HttpClients.createDefault(); + // 2.1 提交请求体 + ArrayList parameters = new ArrayList<>(); + URIBuilder builder = null; + // 装填参数 + if(map!=null){ + for (Map.Entry entry : map.entrySet()) { + parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + } + CloseableHttpResponse response = null; + try { + builder = new URIBuilder(url); + builder.setParameters(parameters); + // 2.设置请求方式和请求信息 + HttpGet httpGet = new HttpGet(builder.build()); + // 2.1 提交header头信息 + httpGet.addHeader("user-agent", FIREFOX_UA); + // 3.执行请求 + response = httpClient.execute(httpGet); + // 4.获取返回值 + assert response != null; + // 按指定编码转换结果实体为String类型 + body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + // 5.关闭连接 + response.close(); + httpClient.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + + /** + * 模拟Post请求 application/x-www-form-urlencoded + * @param url 资源地址 + * @param map 参数列表 + * @return + */ + public static String postStandard(String url, Map map) { + String body = ""; + // 创建httpclient对象 + CloseableHttpClient client = HttpClients.createDefault(); + // 创建post方式请求对象 + HttpPost httpPost = new HttpPost(url); + // 装填参数 + List nvps = new ArrayList<>(); + if(map!=null){ + for (Map.Entry entry : map.entrySet()) { + nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + } + try { + // 设置参数到请求对象中 + httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); + // 设置header报文头信息 + httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); + httpPost.setHeader("User-Agent", FIREFOX_UA); + httpPost.setHeader("Accept", "application"); + httpPost.setHeader("Accept-Encoding", "gzip, deflate"); + // 执行请求操作,并拿到结果(同步阻塞) + CloseableHttpResponse response = client.execute(httpPost); + // 获取结果实体 + HttpEntity entity = response.getEntity(); + if (entity != null) { + // 按指定编码转换结果实体为String类型 + body = EntityUtils.toString(entity, "UTF-8"); + } + EntityUtils.consume(entity); + // 释放链接 + response.close(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return body; + } + + /** + * 模拟Post请求 multipart/form-data + * @param postFile 请求文件 + * @param postUrl 请求链接 + * @param postParam 请求参数 + * @return + */ + public static String postFile(File postFile, String postUrl, Map postParam) { + Logger log = LoggerFactory.getLogger(RequestUtil.class); + String body = ""; + Map resultMap = new HashMap<>(16); + CloseableHttpClient httpClient = HttpClients.createDefault(); + try{ + // 把一个普通参数和文件上传给下面这个地址 + HttpPost httpPost = new HttpPost(postUrl); + + // 设置传输参数 + MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create(); + // 解决乱码问题 + multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + multipartEntity.setCharset(StandardCharsets.UTF_8); + + // 设置文件参数 ,获取文件名postFile.getName() + // 把文件转换成流对象FileBody + multipartEntity.addBinaryBody("file",postFile,ContentType.create("multipart/form-data", + Consts.UTF_8),postFile.getName()); + // 设计文件以外的参数 + Set keySet = postParam.keySet(); + for (String key : keySet) { + // 相当于 + multipartEntity.addPart(key, new StringBody(postParam.get(key), ContentType.create( + "multipart/form-data", Consts.UTF_8))); + } + HttpEntity reqEntity = multipartEntity.build(); + httpPost.setEntity(reqEntity); + // 设置header报文头信息 + httpPost.setHeader("User-Agent", FIREFOX_UA); + httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + httpPost.setHeader("Accept-Encoding","gzip, deflate"); + + // 发起请求,返回请求的响应 + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { + resultMap.put("statusCode", response.getStatusLine().getStatusCode()); + // 获取响应对象 + HttpEntity resEntity = response.getEntity(); + if (resEntity != null) { + // 按指定编码转换结果实体为String类型 + body = EntityUtils.toString(resEntity, StandardCharsets.UTF_8); + } + // 销毁 + EntityUtils.consume(resEntity); + } catch (Exception e) { + e.printStackTrace(); + } + httpClient.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + return body; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 211d463..bc367ae 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -51,4 +51,6 @@ spring.servlet.multipart.max-request-size = 100MB spring.main.allow-bean-definition-overriding = true +my.FileServerAdminAdress = http://127.0.0.1:4096/ +my.FileServerAdress = http://118.24.27.47:4096/