From 05a7f827b7ff555f88982e2dcfb45c2ef5a34a54 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期二, 09 一月 2024 14:59:26 +0800
Subject: [PATCH] 优化1.6 拆分费用bug
---
java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 103 insertions(+), 4 deletions(-)
diff --git a/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java b/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
old mode 100644
new mode 100755
index cbd2c73..8e8cd60
--- a/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
+++ b/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
@@ -7,7 +7,7 @@
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import com.java110.core.log.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@@ -31,6 +31,15 @@
private final static String fileSeparator = System.getProperty("file.separator");
private final static String DEFAULT_IMG_SUFFIX = ".jpg";
private final static String IMAGE_DEFAULT_PATH = "img/";
+
+
+ public final static String FTP_DOMAIN = "FTP_DOMAIN";
+ public final static String FTP_SERVER = "FTP_SERVER";
+ public final static String FTP_PORT = "FTP_PORT";
+ public final static String FTP_USERNAME = "FTP_USERNAME";
+ public final static String FTP_USERPASSWORD = "FTP_USERPASSWORD";
+ public final static String FTP_PATH = "FTP_PATH";
+
/*
*鍥剧墖涓婁紶宸ュ叿鏂规硶
@@ -70,9 +79,6 @@
} else if (imageBase64.contains("data:application/octet-stream;base64,")) {
imageBase64 = imageBase64.replace("data:application/octet-stream;base64,", "");
fileName += ".jpg";
- } else if (imageBase64.contains("mp4") || imageBase64.contains("MP4") || imageBase64.contains("AVI") || imageBase64.contains("avi")
- || imageBase64.contains("WMV") || imageBase64.contains("wmv")) {
- fileName += ".mp4";
} else {
fileName += ".jpg";
}
@@ -158,6 +164,60 @@
}
/*
+ *鏂囦欢涓婁紶宸ュ叿鏂规硶
+ */
+ public String upload(InputStream inputStream, String server, int port,
+ String userName, String userPassword, String fileName) {
+ FTPClient ftpClient = null;
+ String ftpPath = "/";
+
+ if(fileName.contains("/")){
+ ftpPath = fileName.substring(0,fileName.lastIndexOf("/")+1);
+ }
+
+ try {
+ // request.setCharacterEncoding("utf-8");
+
+ ftpClient = new FTPClient();
+ if (!ftpClient.isConnected()) {
+ ftpClient.connect(server, port);
+ }
+ ftpClient.login(userName, userPassword);
+ ftpClient.enterLocalPassiveMode();
+ ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
+ mkDir(ftpClient, ftpPath);// 鍒涘缓鐩綍
+ // 璁剧疆涓婁紶鐩綍 must
+ ftpClient.changeWorkingDirectory(ftpPath);
+
+ if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 寮�鍚湇鍔″櫒瀵筓TF-8鐨勬敮鎸侊紝濡傛灉鏈嶅姟鍣ㄦ敮鎸佸氨鐢║TF-8缂栫爜锛屽惁鍒欏氨浣跨敤鏈湴缂栫爜锛圙BK锛�.
+ LOCAL_CHARSET = "UTF-8";
+ }
+ //fileName = new String(uploadFile.getOriginalFilename().getBytes(LOCAL_CHARSET), SERVER_CHARSET);
+ /*fileName = id + "-" + fileName;// 鏋勫缓涓婁紶鍒版湇鍔″櫒涓婄殑鏂囦欢鍚� 20-鏂囦欢鍚�.鍚庣紑*/
+ FTPFile[] fs = ftpClient.listFiles(fileName);
+ if (fs.length == 0) {
+ System.out.println("this file not exist ftp");
+ } else if (fs.length == 1) {
+ System.out.println("this file exist ftp");
+ ftpClient.deleteFile(fs[0].getName());
+ }
+ boolean saveFlag = ftpClient.storeFile("/"+fileName, inputStream);
+ if (!saveFlag) {
+ throw new IllegalArgumentException("瀛樺偍鏂囦欢澶辫触");
+ }
+ } catch (Exception e) {
+ logger.error("涓婁紶鏂囦欢澶辫触", e);
+ throw new IllegalArgumentException("涓婁紶鏂囦欢澶辫触");
+ } finally {
+ try {
+ ftpClient.disconnect();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return fileName;
+ }
+ /*
*鏂囦欢涓嬭浇宸ュ叿鏂规硶
*/
public byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
@@ -169,6 +229,8 @@
ftpClient.connect(server, port);
}
ftpClient.login(userName, userPassword);
+ ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
+
ftpClient.enterLocalPassiveMode();
if (ftpClient != null) {
String f = new String(
@@ -340,4 +402,41 @@
return false;
}
}
+
+
+ public InputStream download(String fileName, String server, int port, String userName, String userPassword) {
+ InputStream is = null;
+ ByteArrayOutputStream bos = null;
+ ByteArrayInputStream fis = null;
+ FTPClient ftpClient = new FTPClient();
+ try {
+ if (!ftpClient.isConnected()) {
+ ftpClient.connect(server, port);
+ }
+ ftpClient.login(userName, userPassword);
+ ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
+ ftpClient.enterLocalPassiveMode();
+ int reply = ftpClient.getReplyCode();
+ if (!FTPReply.isPositiveCompletion(reply)) {
+ ftpClient.disconnect();
+ }
+ String f = new String(
+ ("/"+fileName).getBytes("GBK"),
+ FTP.DEFAULT_CONTROL_ENCODING);
+ is = ftpClient.retrieveFileStream(f);// 鑾峰彇杩滅▼ftp涓婃寚瀹氭枃浠剁殑InputStream
+ if (null == is) {
+ throw new FileNotFoundException(fileName);
+ }
+
+ } catch (Exception e) {
+ logger.error("ftp閫氳繃鏂囦欢鍚嶇О鑾峰彇杩滅▼鏂囦欢娴�", e);
+ } finally {
+ try {
+ closeConnect(ftpClient);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ return is;
+ }
}
--
Gitblit v1.8.0