package com.dy.pipIrrGlobal.dyFile;
|
|
import com.dy.common.util.NumUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.util.Arrays;
|
import java.util.List;
|
|
@Component
|
public class FileOperate {
|
|
@Autowired
|
private RestTemplate restTemplate ;
|
|
/**
|
* 拆分上载文件的名称
|
* @param file
|
* @return
|
*/
|
public String[] splitFileName(MultipartFile file){
|
String[] grp = new String[2] ;
|
if(file != null) {
|
String fileName = file.getOriginalFilename();
|
int lastDotIndex = fileName.lastIndexOf('.');
|
if (lastDotIndex >= 0) {
|
grp[0] = fileName.substring(0, lastDotIndex);
|
grp[1] = fileName.substring(lastDotIndex + 1);
|
if(grp[0].trim().equals("")){
|
grp[0] = "匿名" ;
|
}
|
}
|
}
|
return grp ;
|
}
|
|
/**
|
* 得到上载的文件扩展名
|
* @param file
|
* @return
|
*/
|
public String getFileExtName(MultipartFile file){
|
String fileExtName = null ;
|
if(file != null) {
|
String filename = file.getOriginalFilename();
|
int lastDotIndex = filename.lastIndexOf('.');
|
if (lastDotIndex >= 0) {
|
fileExtName = filename.substring(lastDotIndex + 1);
|
}
|
}
|
return fileExtName ;
|
}
|
|
/**
|
* 得到上载的文件主名
|
* @param fileName
|
* @return
|
*/
|
public String getFileMainName(String fileName){
|
String fileMainName = null ;
|
if(fileName != null) {
|
int lastDotIndex = fileName.lastIndexOf('.');
|
if (lastDotIndex >= 0) {
|
fileMainName = fileName.substring(0, lastDotIndex);
|
}
|
}
|
if(fileMainName == null){
|
fileMainName = "noName" ;
|
}
|
return fileMainName ;
|
}
|
|
/**
|
* 通过照片路径,得到对应缩略图的路径
|
* @param imgPath
|
* @return
|
*/
|
public String getImgFileZipPath(String imgPath){
|
String path_ = null ;
|
String prePath = null ;
|
String tailPath = null ;
|
if(imgPath != null && !imgPath.trim().equals("")) {
|
int lastDotIndex = imgPath.lastIndexOf('.');
|
if (lastDotIndex >= 0) {
|
prePath = imgPath.substring(0, lastDotIndex);
|
tailPath = imgPath.substring(lastDotIndex);
|
path_ = prePath + "_" + tailPath ;
|
}
|
}
|
if(path_ == null){
|
path_ = imgPath ;
|
}
|
return path_ ;
|
}
|
|
/**
|
* web分布式文件系统保存文件
|
* @param file
|
* @param fmUrl
|
* @param fileCtrlRqMp
|
* @param fileMethodMp
|
* @param regionNum
|
* @param fileExtName
|
* @param json
|
* @return
|
* @throws Exception
|
*/
|
public FileRestVo saveFile(MultipartFile file,
|
String fmUrl,
|
String fileCtrlRqMp,
|
String fileMethodMp,
|
String regionNum,
|
String fileExtName,
|
String json) throws Exception{
|
FileRestVo rvo = null ;
|
if(file != null && file.getBytes() != null && file.getBytes().length > 0){
|
rvo = this.restCreateFileName(fmUrl, fileExtName) ;
|
if(rvo != null){
|
String relativeFilePath = this.restSaveFile(fileCtrlRqMp, fileMethodMp, file, regionNum, json, rvo);
|
rvo.fileWebPath = relativeFilePath ;
|
//if(relativeFilePath != null){
|
// rvo.createFilePath(relativeFilePath, rvo.fileNameHash);
|
//}
|
}
|
}
|
return rvo ;
|
}
|
|
/**
|
* 生成文件名称
|
* @return
|
*/
|
private FileRestVo restCreateFileName(String fmUrl, String fileExtName) throws Exception{
|
// 准备请求数据
|
MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
|
multipartRequestData.add(FileConstant.fmPostMapping_create_paramName, fileExtName);
|
|
// 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
// 封装请求体
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
|
|
String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_create;
|
// 发送POST请求
|
FileRestVo rvo = restTemplate.postForObject(webUrl, requestEntity, FileRestVo.class);
|
|
return rvo ;
|
}
|
|
/**
|
* 把文件存储到文件系统中
|
* @param file
|
* @param regionNum
|
* @param json json数据
|
* @param rvo
|
* @return
|
* @throws Exception
|
*/
|
private String restSaveFile(String fileCtrlRqMp,
|
String fileMethodMp,
|
MultipartFile file,
|
String regionNum,
|
String json,
|
FileRestVo rvo) throws Exception{
|
// 准备请求数据
|
MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
|
multipartRequestData.add(FileConstant.filePostMapping_paramName_file, file.getResource());
|
multipartRequestData.add(FileConstant.filePostMapping_paramName_regionNum, regionNum);
|
multipartRequestData.add(FileConstant.filePostMapping_paramName_json, (json==null?"":json));
|
multipartRequestData.add(FileConstant.filePostMapping_paramName_absolutePath, rvo.fileSysAbsolutePath);
|
multipartRequestData.add(FileConstant.filePostMapping_paramName_relativePath, rvo.fileSysRelativePath);
|
multipartRequestData.add(FileConstant.filePostMapping_paramName_fileName, rvo.fileName);
|
|
// 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
//headers.setContentLength(file.getSize());
|
|
// 封装请求体
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
|
|
String fileRestUrl = rvo.fileSysRestUrl ;
|
if(!fileRestUrl.endsWith("/") && !fileRestUrl.endsWith("\\\\")){
|
fileRestUrl += "/" ;
|
}
|
fileRestUrl += (fileCtrlRqMp + "/" + fileMethodMp) ;
|
|
// 发送POST请求
|
return restTemplate.postForObject(fileRestUrl, requestEntity, Object.class).toString();
|
}
|
|
/**
|
* 解析文件名称
|
* @param fmUrl
|
* @param filePath
|
* @return
|
*/
|
public FileRestVo parse(String fmUrl, String filePath){
|
// 准备请求数据
|
MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
|
multipartRequestData.add(FileConstant.fmPostMapping_parsePath_paramName, filePath);
|
|
// 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
// 封装请求体
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
|
|
String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parsePath;
|
// 发送POST请求
|
FileRestVo rvo = restTemplate.postForObject(webUrl, requestEntity, FileRestVo.class);
|
|
return rvo ;
|
}
|
|
|
/**
|
* 解析文件名称
|
* @param fmUrl
|
* @param filePaths
|
* @return
|
*/
|
public List<FileRestVo> parse(String fmUrl, List<String> filePaths) throws Exception{
|
List<FileRestVo> rList = null ;
|
if(filePaths != null && filePaths.size() > 0) {
|
MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
|
multipartRequestData.add(FileConstant.fmPostMapping_parsePathList_paramName, filePaths);
|
|
// 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
// 封装请求体
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
|
|
String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parsePathList;
|
// 发送POST请求
|
FileRestVo[] rvos = restTemplate.postForObject(webUrl, requestEntity, FileRestVo[].class);
|
rList = Arrays.asList(rvos) ;
|
}
|
return rList ;
|
}
|
|
/**
|
* 解析文件哈希值
|
* @param fmUrl
|
* @param hashcode
|
* @return
|
*/
|
public FileRestVo parseHashcode(String fmUrl, Integer hashcode){
|
// 准备请求数据
|
MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
|
multipartRequestData.add(FileConstant.fmPostMapping_parseHashcode_paramName, hashcode);
|
|
// 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
// 封装请求体
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
|
|
String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parseHashcode;
|
// 发送POST请求
|
FileRestVo rvo = restTemplate.postForObject(webUrl, requestEntity, FileRestVo.class);
|
|
return rvo ;
|
}
|
|
/**
|
* 解析文件哈希值
|
* @param fmUrl
|
* @param hashCodes
|
* @return
|
*/
|
public List<FileRestVo> parseHashcode(String fmUrl, List<Integer> hashCodes){
|
List<FileRestVo> rList = null ;
|
if(hashCodes != null && hashCodes.size() > 0) {
|
// 准备请求数据
|
MultiValueMap<String, Object> multipartRequestData = new LinkedMultiValueMap<>();
|
multipartRequestData.add(FileConstant.fmPostMapping_parseHashcodeList_paramName, hashCodes);
|
|
// 设置请求头部,这里假设服务器接收multipart/form-data类型的数据
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
// 封装请求体
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequestData, headers);
|
|
String webUrl = fmUrl + "/" + FileConstant.fmRequestMapping + "/" + FileConstant.fmPostMapping_parseHashcodeList;
|
// 发送POST请求
|
FileRestVo[] rvos = restTemplate.postForObject(webUrl, requestEntity, FileRestVo[].class);
|
rList = Arrays.asList(rvos) ;
|
}
|
return rList ;
|
}
|
|
/**
|
* 解析文件文件路径中的哈希值并返回图片完整路径
|
* @param fmUrl
|
* @param filePath
|
* @return
|
*/
|
public String getFilePath(String fmUrl, String filePath){
|
FileRestVo rvo = null ;
|
if(filePath != null && !filePath.trim().equals("")){
|
String[] strs = filePath.split("\\?") ;
|
String hashValStr = strs[strs.length - 1] ;
|
if(hashValStr != null && !hashValStr.trim().equals("") && NumUtil.isPlusIntNumber(hashValStr)){
|
int hashVal = Integer.valueOf(hashValStr) ;
|
rvo = parseHashcode(fmUrl, hashVal) ;
|
}
|
}
|
if(rvo != null){
|
return rvo.getFileWebUrl() + filePath ;
|
}
|
return null ;
|
}
|
|
}
|