|
@@ -1,5 +1,6 @@
|
|
|
package com.ghsc.partybuild.service.impl;
|
|
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -7,6 +8,9 @@ import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
|
|
|
import com.ghsc.partybuild.mapper.CfFileCQuery;
|
|
|
+import com.ghsc.partybuild.util.BASE64DecodedMultipartFile;
|
|
|
+import com.ghsc.partybuild.vo.ImageBase64Vo;
|
|
|
+import com.ghsc.partybuild.vo.UploadFileBase64Vo;
|
|
|
import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.commons.net.ftp.FTP;
|
|
@@ -25,8 +29,11 @@ import com.ghsc.partybuild.model.CfFileExample;
|
|
|
import com.ghsc.partybuild.service.FileService;
|
|
|
import com.ghsc.partybuild.util.FtpHelper;
|
|
|
import com.ghsc.partybuild.util.MapUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
@Service("fileService")
|
|
|
public class FileServiceImpl implements FileService {
|
|
|
|
|
@@ -46,7 +53,7 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
private String ENCODE = "utf-8";
|
|
|
|
|
|
- private List<String> imageTypes=new ArrayList<>();
|
|
|
+ private List<String> imageTypes = new ArrayList<>();
|
|
|
|
|
|
public FileServiceImpl() {
|
|
|
logger = LoggerFactory.getLogger(this.getClass());
|
|
@@ -78,12 +85,12 @@ public class FileServiceImpl implements FileService {
|
|
|
CfFile cfFile = new CfFile();
|
|
|
FTPClient ftpClient = new FTPClient();
|
|
|
String fileId = "";
|
|
|
- InputStream fileStream=null;
|
|
|
+ InputStream fileStream = null;
|
|
|
try {
|
|
|
|
|
|
cfFile.setFilesuffix(fileName.substring(fileName.lastIndexOf(".")));
|
|
|
|
|
|
- if(this.imageTypes.contains(cfFile.getFilesuffix().replace(".","").toLowerCase())) {
|
|
|
+ if (this.imageTypes.contains(cfFile.getFilesuffix().replace(".", "").toLowerCase())) {
|
|
|
byte[] inform = informImage(byteArr);
|
|
|
fileStream = new ByteArrayInputStream(inform);
|
|
|
} else {
|
|
@@ -107,7 +114,7 @@ public class FileServiceImpl implements FileService {
|
|
|
cfFile.setOperatestate("A");
|
|
|
cfFile.setOperatetime(new Date());
|
|
|
cfFile.setSyncstate("N");
|
|
|
-
|
|
|
+
|
|
|
ftpClient.changeWorkingDirectory(filePath);
|
|
|
|
|
|
ftpClient.storeFile(fileId + cfFile.getFilesuffix(), fileStream);
|
|
@@ -131,7 +138,7 @@ public class FileServiceImpl implements FileService {
|
|
|
FTPClient ftpClient = new FTPClient();
|
|
|
String fileId = "";
|
|
|
|
|
|
- if(StringUtils.isBlank(file)){
|
|
|
+ if (StringUtils.isBlank(file)) {
|
|
|
return fileId;
|
|
|
}
|
|
|
//logger.info("file:"+file.length());
|
|
@@ -150,7 +157,7 @@ public class FileServiceImpl implements FileService {
|
|
|
cfFile.setFilerefid(fileRefID);
|
|
|
cfFile.setCreatetime(new Date());
|
|
|
String filePath = createServeDir(cfFile.getCreatetime(), ftpClient);
|
|
|
- if(fileName.indexOf(".")>=0) {
|
|
|
+ if (fileName.indexOf(".") >= 0) {
|
|
|
cfFile.setFilesuffix(fileName.substring(fileName.lastIndexOf(".")));
|
|
|
} else {
|
|
|
cfFile.setFilesuffix("");
|
|
@@ -167,7 +174,7 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
//byte[] bufImg = new BASE64Decoder().decodeBuffer(file);
|
|
|
- byte[] bufImg= Base64.getDecoder().decode(file);
|
|
|
+ byte[] bufImg = Base64.getDecoder().decode(file);
|
|
|
|
|
|
ftpClient.changeWorkingDirectory(filePath);
|
|
|
|
|
@@ -188,58 +195,58 @@ public class FileServiceImpl implements FileService {
|
|
|
return fileId;
|
|
|
}
|
|
|
|
|
|
- public byte[] informImage(byte[] byteArr){
|
|
|
+ public byte[] informImage(byte[] byteArr) {
|
|
|
|
|
|
- byte[] result=null;
|
|
|
+ byte[] result = null;
|
|
|
//设置处理大小500K
|
|
|
- int imageSize=1024*1024/2;
|
|
|
+ int imageSize = 1024 * 1024 / 2;
|
|
|
|
|
|
- if(byteArr.length<imageSize){
|
|
|
- result= byteArr;
|
|
|
+ if (byteArr.length < imageSize) {
|
|
|
+ result = byteArr;
|
|
|
} else {
|
|
|
- InputStream sourceImage=new ByteArrayInputStream(byteArr);
|
|
|
- ByteArrayOutputStream osImage=null;
|
|
|
+ InputStream sourceImage = new ByteArrayInputStream(byteArr);
|
|
|
+ ByteArrayOutputStream osImage = null;
|
|
|
try {
|
|
|
|
|
|
- osImage=changeImageSize(sourceImage);
|
|
|
+ osImage = changeImageSize(sourceImage);
|
|
|
|
|
|
- while (osImage.size()>imageSize){
|
|
|
+ while (osImage.size() > imageSize) {
|
|
|
|
|
|
- osImage=changeImageQuality(new ByteArrayInputStream(osImage.toByteArray()));
|
|
|
+ osImage = changeImageQuality(new ByteArrayInputStream(osImage.toByteArray()));
|
|
|
|
|
|
}
|
|
|
|
|
|
- result=osImage.toByteArray();
|
|
|
+ result = osImage.toByteArray();
|
|
|
|
|
|
- } catch (Exception ex){
|
|
|
+ } catch (Exception ex) {
|
|
|
logger.error("informImage,图片读取失败!", ex);
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
- logger.info("informImage,ftp上传压缩图片大小为:"+result.length);
|
|
|
+ logger.info("informImage,ftp上传压缩图片大小为:" + result.length);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public ByteArrayOutputStream changeImageSize(InputStream imageStream){
|
|
|
+ public ByteArrayOutputStream changeImageSize(InputStream imageStream) {
|
|
|
|
|
|
ByteArrayOutputStream outArray = new ByteArrayOutputStream();
|
|
|
try {
|
|
|
Thumbnails.of(imageStream).size(1920, 1080).toOutputStream(outArray);
|
|
|
- } catch (Exception ex){
|
|
|
+ } catch (Exception ex) {
|
|
|
logger.error("ImageChangeSize,图片大小失败!", ex);
|
|
|
}
|
|
|
return outArray;
|
|
|
}
|
|
|
|
|
|
- public ByteArrayOutputStream changeImageQuality(InputStream imageStream){
|
|
|
+ public ByteArrayOutputStream changeImageQuality(InputStream imageStream) {
|
|
|
|
|
|
ByteArrayOutputStream outArray = new ByteArrayOutputStream();
|
|
|
try {
|
|
|
Thumbnails.of(imageStream).outputQuality(0.75f).toOutputStream(outArray);
|
|
|
- } catch (Exception ex){
|
|
|
+ } catch (Exception ex) {
|
|
|
logger.error("changeImageQuality,图片质量失败!", ex);
|
|
|
}
|
|
|
|
|
@@ -313,27 +320,32 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
@Override
|
|
|
public int deleteFile(String fileid) {
|
|
|
+ return cfFileMapper.deleteByPrimaryKey(fileid);
|
|
|
+ }
|
|
|
|
|
|
- CfFile model = cfFileMapper.selectByPrimaryKey(fileid);
|
|
|
-
|
|
|
- model.setOperatestate("D");
|
|
|
- model.setOperatetime(new Date());
|
|
|
- model.setSyncstate("N");
|
|
|
+ @Override
|
|
|
+ public int deleteFileByRefId(String fileRefId, Integer fileType) {
|
|
|
+ CfFileExample exp = new CfFileExample();
|
|
|
+ CfFileExample.Criteria cri = exp.or();
|
|
|
+ cri.andFilerefidEqualTo(fileRefId);
|
|
|
+ if (fileType != null) {
|
|
|
+ cri.andFiletypeEqualTo(fileType);
|
|
|
+ }
|
|
|
|
|
|
- return cfFileMapper.updateByPrimaryKey(model);
|
|
|
+ return cfFileMapper.deleteByExample(exp);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Object> fileNeedDown(Date beginTime) {
|
|
|
- List<Object> files=new ArrayList<>();
|
|
|
+ List<Object> files = new ArrayList<>();
|
|
|
CfFileExample exp = new CfFileExample();
|
|
|
- List<String> oStates=new ArrayList<>();
|
|
|
+ List<String> oStates = new ArrayList<>();
|
|
|
oStates.add("A");
|
|
|
oStates.add("M");
|
|
|
|
|
|
exp.or().andOperatestateIn(oStates).andOperatetimeGreaterThan(beginTime);
|
|
|
|
|
|
- List<CfFile> cfFiles=cfFileMapper.selectByExample(exp);
|
|
|
+ List<CfFile> cfFiles = cfFileMapper.selectByExample(exp);
|
|
|
|
|
|
try {
|
|
|
FTPClient ftpClient = new FTPClient();
|
|
@@ -343,19 +355,19 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
cfFiles.forEach(f -> {
|
|
|
try {
|
|
|
- FTPFile[] ftpFiles=ftpClient.listFiles(f.getFileurl());
|
|
|
- if(ftpFiles.length==0){
|
|
|
+ FTPFile[] ftpFiles = ftpClient.listFiles(f.getFileurl());
|
|
|
+ if (ftpFiles.length == 0) {
|
|
|
files.add(f.getFileid());
|
|
|
}
|
|
|
|
|
|
- } catch (Exception ex){
|
|
|
- logger.error("fileNeedDown,forEach检查文件id:"+f.getFileid(), ex);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ logger.error("fileNeedDown,forEach检查文件id:" + f.getFileid(), ex);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
ftpHelper.closeConnect(ftpClient);
|
|
|
|
|
|
- } catch (Exception ex){
|
|
|
+ } catch (Exception ex) {
|
|
|
logger.error("fileNeedDown,检查文件失败!", ex);
|
|
|
}
|
|
|
|
|
@@ -365,26 +377,26 @@ public class FileServiceImpl implements FileService {
|
|
|
@Override
|
|
|
public boolean ftpFileUplad(List<String> ftpFileInfo) {
|
|
|
|
|
|
- boolean result=false;
|
|
|
- CfFile cfFile = cfFileMapper.selectByPrimaryKey(ftpFileInfo.get(0).replace("\"",""));
|
|
|
+ boolean result = false;
|
|
|
+ CfFile cfFile = cfFileMapper.selectByPrimaryKey(ftpFileInfo.get(0).replace("\"", ""));
|
|
|
|
|
|
- if(cfFile!=null){
|
|
|
- try{
|
|
|
+ if (cfFile != null) {
|
|
|
+ try {
|
|
|
FTPClient ftpClient = new FTPClient();
|
|
|
ftpHelper.connectToServer(ftpClient);
|
|
|
ftpClient.enterLocalPassiveMode();
|
|
|
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
- String urlFile=ftpFileInfo.get(1).replace("\"","");
|
|
|
+ String urlFile = ftpFileInfo.get(1).replace("\"", "");
|
|
|
|
|
|
//byte[] ftpBytes = new BASE64Decoder().decodeBuffer(base64File);
|
|
|
- String base64File=URLDecoder.decode(urlFile,ENCODE);
|
|
|
+ String base64File = URLDecoder.decode(urlFile, ENCODE);
|
|
|
//byte[] ftpBytes=new BASE64Decoder().decodeBuffer(base64File);
|
|
|
- byte[] ftpBytes=Base64.getDecoder().decode(base64File);
|
|
|
+ byte[] ftpBytes = Base64.getDecoder().decode(base64File);
|
|
|
|
|
|
- String[] filePaths=cfFile.getFileurl().split("/");
|
|
|
+ String[] filePaths = cfFile.getFileurl().split("/");
|
|
|
|
|
|
FTPFile[] ftpFiles = ftpClient.listFiles(filePaths[0]);
|
|
|
- if(ftpFiles.length==0){
|
|
|
+ if (ftpFiles.length == 0) {
|
|
|
ftpClient.makeDirectory(filePaths[0]);
|
|
|
}
|
|
|
|
|
@@ -394,8 +406,8 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
ftpHelper.closeConnect(ftpClient);
|
|
|
|
|
|
- result=true;
|
|
|
- }catch (Exception ex){
|
|
|
+ result = true;
|
|
|
+ } catch (Exception ex) {
|
|
|
logger.error("ftpFileUplad,ftp文件上传完", ex);
|
|
|
}
|
|
|
}
|
|
@@ -405,13 +417,82 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> FindTestResult(String fileRefId) {
|
|
|
- List<Map<String, Object>> results=cfFileCQuery.FindTestResult(fileRefId);
|
|
|
+ List<Map<String, Object>> results = cfFileCQuery.FindTestResult(fileRefId);
|
|
|
|
|
|
- if(results.size()>0){
|
|
|
+ if (results.size() > 0) {
|
|
|
return results.get(0);
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getFileBase64(String fileId) throws IOException {
|
|
|
+ String base64String = "";
|
|
|
+
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ try {
|
|
|
+ this.downFtpFile(fileId, bos);
|
|
|
+
|
|
|
+ byte[] bytes = bos.toByteArray();
|
|
|
+
|
|
|
+ Base64.Encoder encoder = Base64.getEncoder();
|
|
|
+
|
|
|
+ base64String = encoder.encodeToString(bytes);
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ return base64String;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ImageBase64Vo getImageBase64(String fileId) throws IOException {
|
|
|
+ ImageBase64Vo result = new ImageBase64Vo();
|
|
|
+
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ try {
|
|
|
+ this.downFtpFile(fileId, bos);
|
|
|
+
|
|
|
+ byte[] bytes = bos.toByteArray();
|
|
|
+
|
|
|
+ BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
|
|
|
+
|
|
|
+ result.base64 = Base64.getEncoder().encodeToString(bytes);
|
|
|
+ result.height = image.getHeight();
|
|
|
+ result.width = image.getWidth();
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer uploadBase64(List<UploadFileBase64Vo> fileList, String userId) throws IOException {
|
|
|
+ Integer result = 0;
|
|
|
+
|
|
|
+ for (int i = 0; i < fileList.size(); i++) {
|
|
|
+ UploadFileBase64Vo base64File = fileList.get(i);
|
|
|
+ MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(base64File.base64);
|
|
|
+ MultipartFile[] files = new MultipartFile[]{multipartFile};
|
|
|
+
|
|
|
+ String fileName = multipartFile.getOriginalFilename();
|
|
|
+
|
|
|
+ this.saveFile(multipartFile.getBytes(), fileName, base64File.fileRefId, userId, base64File.fileType);
|
|
|
+
|
|
|
+ result++;
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|