From e3defa1ab8791afd0ed5ab7d73c1385d28df993e Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期日, 17 五月 2020 15:19:35 +0800
Subject: [PATCH] 优化代码输出流没有关闭问题
---
CommonService/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java | 69 ++++++++++++++++++++++++++++++++--
1 files changed, 64 insertions(+), 5 deletions(-)
diff --git a/CommonService/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java b/CommonService/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java
index 838f775..a58cc28 100644
--- a/CommonService/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java
+++ b/CommonService/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java
@@ -1,15 +1,24 @@
package com.java110.common.smo.impl;
import com.java110.common.dao.IFileServiceDao;
+import com.java110.config.properties.code.Java110Properties;
import com.java110.core.base.smo.BaseServiceSMO;
+import com.java110.core.client.JSchFtpUploadTemplate;
import com.java110.core.smo.file.IFileInnerServiceSMO;
import com.java110.dto.file.FileDto;
-import com.java110.utils.util.BeanConvertUtil;
+import com.java110.core.client.FtpUploadTemplate;
+import com.java110.utils.util.Base64Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
+import sun.misc.BASE64Encoder;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
import java.util.List;
+import java.util.UUID;
@RestController
public class FileInnerServiceSMOImpl extends BaseServiceSMO implements IFileInnerServiceSMO {
@@ -17,17 +26,67 @@
@Autowired
private IFileServiceDao fileServiceDaoImpl;
+ @Autowired
+ private Java110Properties java110Properties;
+
+ @Autowired
+ private FtpUploadTemplate ftpUploadTemplate;
+
+ @Autowired
+ private JSchFtpUploadTemplate jSchFtpUploadTemplate;
+
+
@Override
- public int saveFile(@RequestBody FileDto fileDto) {
+ public String saveFile(@RequestBody FileDto fileDto) {
- int saveFileFlag = fileServiceDaoImpl.saveFile(BeanConvertUtil.beanCovertMap(fileDto));
+ //int saveFileFlag = fileServiceDaoImpl.saveFile(BeanConvertUtil.beanCovertMap(fileDto));
- return saveFileFlag;
+
+ String fileName = ftpUploadTemplate.upload(fileDto.getContext(), java110Properties.getFtpServer(),
+ java110Properties.getFtpPort(), java110Properties.getFtpUserName(),
+ java110Properties.getFtpUserPassword(), java110Properties.getFtpPath());
+
+// String fileName = jSchFtpUploadTemplate.upload(fileDto.getContext(), java110Properties.getFtpServer(),
+// java110Properties.getFtpPort(), java110Properties.getFtpUserName(),
+// java110Properties.getFtpUserPassword(), java110Properties.getFtpPath());
+ return fileName;
}
@Override
public List<FileDto> queryFiles(@RequestBody FileDto fileDto) {
- return BeanConvertUtil.covertBeanList(fileServiceDaoImpl.getFiles(BeanConvertUtil.beanCovertMap(fileDto)), FileDto.class);
+ //return BeanConvertUtil.covertBeanList(fileServiceDaoImpl.getFiles(BeanConvertUtil.beanCovertMap(fileDto)), FileDto.class);
+ List<FileDto> fileDtos = new ArrayList<>();
+ String fileName = fileDto.getFileSaveName();
+ String ftpPath = java110Properties.getFtpPath();
+ if (fileName.contains("/")) {
+ ftpPath += fileName.substring(0, fileName.lastIndexOf("/")+1);
+ fileName = fileName.substring(fileName.lastIndexOf("/")+1, fileName.length());
+ }
+ byte[] fileImg = ftpUploadTemplate.downFileByte(ftpPath, fileName, java110Properties.getFtpServer(),
+ java110Properties.getFtpPort(), java110Properties.getFtpUserName(),
+ java110Properties.getFtpUserPassword());
+ try {
+ File file = new File("/home/hc/img/"+ UUID.randomUUID().toString()+".jpg");
+ File fileParent = file.getParentFile();
+ if (!fileParent.exists()) {
+ fileParent.mkdirs();// 鑳藉垱寤哄绾х洰褰�
+ }
+ if(!file.exists()){
+ file.createNewFile();
+ }
+ OutputStream out = new FileOutputStream(file);
+ out.write(fileImg);
+ out.flush();
+ out.close();
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ //String context = new BASE64Encoder().encode(fileImg);
+ String context = Base64Convert.byteToBase64(fileImg);
+
+ fileDto.setContext(context);
+ fileDtos.add(fileDto);
+ return fileDtos;
}
public IFileServiceDao getFileServiceDaoImpl() {
--
Gitblit v1.8.0