|
@@ -0,0 +1,149 @@
|
|
|
+package com.minpay.common.util;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.commons.net.ftp.FTPClient;
|
|
|
+
|
|
|
+import com.min.util.DateUtil;
|
|
|
+import com.minpay.common.service.IPropertiesService;
|
|
|
+import com.minpay.db.table.mapper.PubSysparMapper;
|
|
|
+import com.minpay.db.table.model.PubSyspar;
|
|
|
+import com.minpay.db.table.model.PubSysparExample;
|
|
|
+import com.startup.minpay.frame.exception.MINBusinessException;
|
|
|
+import com.startup.minpay.frame.service.base.IMINDataBaseService;
|
|
|
+import com.startup.minpay.frame.service.base.Service;
|
|
|
+import com.startup.minpay.util.RandomUtil;
|
|
|
+
|
|
|
+public class UpLoadFile {
|
|
|
+ /**
|
|
|
+ * 上传文件方法
|
|
|
+ * @throws MINBusinessException,FileNotFoundException
|
|
|
+ * fileType 00图片
|
|
|
+ * size 缓冲区
|
|
|
+ * renameFlag 是否重命名
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static String uploadImg(InputStream in,String fileNameSuffix,String channel, String fileType) throws MINBusinessException,FileNotFoundException{
|
|
|
+// 获取根路径(nginx文件服务器路径)
|
|
|
+ String basePath = Service.lookup(IPropertiesService.class)
|
|
|
+ .getSystemProperties().get(channel+"_FILE_SERVER_BASE_PATH").getKey();// 为nginx根路径
|
|
|
+ String nowDate = DateUtil.format(new Date(), "yyyyMMdd");
|
|
|
+ // 获取web服务器访问路径
|
|
|
+ String baseUrl = Service.lookup(IPropertiesService.class)
|
|
|
+ .getSystemProperties().get(channel+"_FILE_SERVER_URL").getKey(); // 为nginx访问路径
|
|
|
+
|
|
|
+ // 文件类型存放路径
|
|
|
+ String filePath = Service.lookup(IPropertiesService.class)
|
|
|
+ .getSystemProperties().get(channel+"_FILE_PATH_" + fileType).getKey();
|
|
|
+ String fileTypePath = basePath+filePath;//"/home/ftp_file/img/btxzs/";
|
|
|
+ //获取ftp 参数
|
|
|
+ String ftpIp = Service.lookup(IPropertiesService.class)
|
|
|
+ .getSystemProperties().get(channel+"_FTP_IP").getKey(); // 上传IP
|
|
|
+ String ftpPort = Service.lookup(IPropertiesService.class)
|
|
|
+ .getSystemProperties().get(channel+"_FTP_PORT").getKey(); // 上传端口
|
|
|
+ String ftpName = Service.lookup(IPropertiesService.class)
|
|
|
+ .getSystemProperties().get(channel+"_FTP_USERNAME").getKey(); // 上传用户名
|
|
|
+ String ftpPwd = Service.lookup(IPropertiesService.class)
|
|
|
+ .getSystemProperties().get(channel+"_FTP_PASSWORD").getKey(); // 上传密码
|
|
|
+
|
|
|
+ /*List<String> gauth = new ArrayList<String>();
|
|
|
+ gauth.add(channel+"_FTP_IP");
|
|
|
+ gauth.add(channel+"_FTP_PORT");
|
|
|
+ gauth.add(channel+"_FTP_USERNAME");
|
|
|
+ gauth.add(channel+"_FTP_PASSWORD");
|
|
|
+ PubSysparExample examples = new PubSysparExample();
|
|
|
+ examples.createCriteria().andCodeIn(gauth);
|
|
|
+// 查询ftp需要参数
|
|
|
+ List<PubSyspar> productRel = Service.lookup(IMINDataBaseService.class)
|
|
|
+ .getMybatisMapper(PubSysparMapper.class).selectByExample(examples);
|
|
|
+ String logoCode = null;
|
|
|
+ String ftpIP = null;
|
|
|
+ String ftpP = null;
|
|
|
+ String ftpUserName = null;
|
|
|
+ String ftpPassword = null;
|
|
|
+ int ftpPorti = 0;
|
|
|
+ for (int i = 0; i < productRel.size(); i++) {
|
|
|
+ logoCode = productRel.get(i).getCode();
|
|
|
+ if (logoCode.equals(FTPIP)){
|
|
|
+ ftpIP = productRel.get(i).getValue();
|
|
|
+ }else if (logoCode.equals(FTPPORT)){
|
|
|
+ ftpPort = productRel.get(i).getValue();
|
|
|
+ }else if (logoCode.equals(FTPUSERNAME)){
|
|
|
+ ftpUserName = productRel.get(i).getValue();
|
|
|
+ }else if (logoCode.equals(FTPPASSWORD)){
|
|
|
+ ftpPassword = productRel.get(i).getValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ftpPorti = Integer.parseInt(ftpPort);*/
|
|
|
+ byte[] buffer = new byte[512];// 缓冲区
|
|
|
+// InputStream in = null;
|
|
|
+ File file = new File(basePath);
|
|
|
+ FileOutputStream out = null;
|
|
|
+ String saveFileName = "";
|
|
|
+ String subDir = "";
|
|
|
+ try {
|
|
|
+ /*String fileName = file.getName();
|
|
|
+ if (fileName.indexOf("\\") != -1) {
|
|
|
+ int le = fileName.lastIndexOf("\\");
|
|
|
+ fileName = fileName.substring(le + 1, fileName.length());
|
|
|
+ }
|
|
|
+ if (fileName.indexOf(".") == -1) {
|
|
|
+ throw new MINBusinessException("文件名称格式异常,请检查");
|
|
|
+ }
|
|
|
+ // 文件名后缀
|
|
|
+ String fileNameSuffix = fileName.split("\\.")[fileName.split("\\.").length - 1];*/
|
|
|
+ // 文件重命名
|
|
|
+// if (renameFlag) {
|
|
|
+ String random = RandomUtil.generateNumber(6);// 获取6位随机数字 防止重复
|
|
|
+ saveFileName = new Date().getTime() + random + "."
|
|
|
+ + fileNameSuffix;
|
|
|
+// } else {
|
|
|
+// saveFileName = fileTypePath + fileName;
|
|
|
+// }
|
|
|
+// in = new FileInputStream(file);
|
|
|
+
|
|
|
+ //subDir = "baotuan/img/" + nowDate +"/";
|
|
|
+ subDir = filePath + nowDate +"/";
|
|
|
+
|
|
|
+ FtpClientEntity a = new FtpClientEntity();
|
|
|
+ int ftpP = 21;
|
|
|
+ if(!StringUtils.isEmpty(ftpPort)){
|
|
|
+ ftpP = Integer.valueOf(ftpPort);
|
|
|
+ }
|
|
|
+ FTPClient ftp = a.getConnectionFTP(ftpIp, ftpP, ftpName,ftpPwd);
|
|
|
+// FTPClient ftp = a.getConnectionFTP(ftpIP, ftpPorti, ftpUserName,
|
|
|
+// ftpPassword);
|
|
|
+ a.uploadFile(ftp, basePath, subDir, saveFileName, in);
|
|
|
+ a.closeFTP(ftp);
|
|
|
+ }finally{
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e2) {
|
|
|
+ e2.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+// String url = "https://baotuan.keao.tech/".concat(subDir).concat(saveFileName);
|
|
|
+ String url = baseUrl.concat(subDir).concat(saveFileName);
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取文件名后缀
|
|
|
+ * @throws MINBusinessException
|
|
|
+ * @throws FileNotFoundException
|
|
|
+ */
|
|
|
+ public static String uploadImg(InputStream in, String channel) throws FileNotFoundException, MINBusinessException{
|
|
|
+ String fileNameSuffix = "png";
|
|
|
+ return uploadImg(in,fileNameSuffix, channel,"00");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|