Ver código fonte

调整图片上传

xubh 4 anos atrás
pai
commit
cbf965beab

+ 12 - 0
src/main/java/com/minpay/common/service/IPublicService.java

@@ -291,4 +291,16 @@ public interface IPublicService extends IMINLocalService, IMINInitializer {
 	 * @throws MINBusinessException
 	 */
 	public void recordAccDetail(String accNo, String type, String trxAmt, String relNo, String remark, String operator)throws MINBusinessException;
+
+	/**
+	 * 上传文件到本地
+	 * @param file  上传文件
+	 * @param fileType 文件类型,用于区分文件路径上传
+	 * @param size 缓冲区 默认512,大文件需要手动修改
+	 * @param renameFlag 是否重命名
+	 * @return
+	 * @throws MINBusinessException
+	 * @throws BusinessCodeException
+	 */
+	public Map<String, Object> uploadFile(FileItem file,String fileType,int size,boolean renameFlag,String channel) throws MINBusinessException,BusinessCodeException;
 }

+ 132 - 5
src/main/java/com/minpay/common/service/impl/PublicServiceImpl.java

@@ -6,8 +6,10 @@ import com.minpay.common.bean.User;
 import com.minpay.common.constant.Constant;
 import com.minpay.common.exception.BusinessCodeException;
 import com.minpay.common.service.ICommonService;
+import com.minpay.common.service.IPropertiesService;
 import com.minpay.common.service.IPublicService;
 import com.minpay.common.service.mobile.MobileValidate;
+import com.minpay.common.util.FtpClientEntity;
 import com.minpay.db.table.mapper.*;
 import com.minpay.db.table.model.*;
 import com.minpay.db.table.own.mapper.RoleMapper;
@@ -19,17 +21,17 @@ import com.startup.minpay.frame.service.base.Service;
 import com.startup.minpay.frame.session.MINSession;
 import com.startup.minpay.frame.target.MINComponent;
 import com.startup.minpay.util.Log;
+import com.startup.minpay.util.RandomUtil;
+import net.coobird.thumbnailator.Thumbnails;
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.net.ftp.FTPClient;
 import org.elasticsearch.common.Strings;
 import org.springframework.util.FileCopyUtils;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.InputStream;
+import java.io.*;
 import java.util.*;
 
 @MINComponent
@@ -293,7 +295,6 @@ public class PublicServiceImpl implements IPublicService {
 	 * @param mobileValidateCode
 	 * @param mobileNo
 	 * @param transType          业务类型
-	 * @param session
 	 * @return
 	 * @throws MINBusinessException
 	 */
@@ -649,4 +650,130 @@ public class PublicServiceImpl implements IPublicService {
 		return null;
 	}
 
+	public Map<String, Object> uploadFile(FileItem file, String fileType,
+										  int size, boolean renameFlag,String channel) throws MINBusinessException,
+			BusinessCodeException {
+		StringBuffer fileUrl = new StringBuffer();
+		// 获取根路径(nginx文件服务器路径)
+		String basePath = Service.lookup(IPublicService.class)
+				.getSysParValue(channel+"_FILE_SERVER_BASE_PATH");// 为nginx根路径
+		// 获取web服务器访问路径
+		String baseUrl = Service.lookup(IPublicService.class)
+				.getSysParValue(channel+"_FILE_SERVER_URL"); // 为nginx访问路径
+
+		// 获取当前时间
+		String nowDate = DateUtil.format(new Date(), "yyyyMMdd");
+		// 文件类型存放路径
+		String fileTypePath = Service.lookup(IPublicService.class)
+				.getSysParValue(channel+"_FILE_PATH_" + fileType);
+		//获取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(); // 上传密码
+
+		int numberRead = 0;
+		if (size == 0) {
+			size = 512;
+		}
+		byte[] buffer = new byte[size];// 缓冲区
+		InputStream in = null;
+		FileOutputStream out = null;
+		String saveFileName = "";
+		double width = -1; // 记录图片宽度
+		double height = -1; // 记录图片的高度
+		String subDir = "";
+		File tofile = null;
+		InputStream input = null;
+		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 = file.getInputStream();
+			Random random = new Random();
+			tofile = new File(random.nextInt(10000)+"abbreviatedPicture.jpg");
+			try {
+				// 大于5M
+				if(file.getSize() > 4 * 1024 * 1024) {
+					Thumbnails.of(in).scale(1f).outputQuality(0.5f).toFile(tofile);
+				}
+				else if(file.getSize() > 3 * 1024 * 1024) {
+					Thumbnails.of(in).scale(1f).outputQuality(0.3f).toFile(tofile);
+				}
+				else if(file.getSize() > 2 * 1024 * 1024) {
+					Thumbnails.of(in).scale(1f).outputQuality(0.4f).toFile(tofile);
+				}
+				else if(file.getSize() > 1 * 1024 * 1024) {
+					Thumbnails.of(in).scale(1f).outputQuality(0.4f).toFile(tofile);
+				}
+			} catch (Exception ex) {
+				ex.printStackTrace();
+			}
+
+			subDir = fileTypePath+ nowDate +"/";
+
+			FtpClientEntity a = new FtpClientEntity();
+			int ftpP = 21;
+			if(!StringUtils.isEmpty(ftpPort)){
+				ftpP = Integer.valueOf(ftpPort);
+			}
+			FTPClient ftp = a.getConnectionFTP(ftpIp, ftpP, ftpName,ftpPwd);
+			if(file.getSize() > 1 * 1024 * 1024) {
+				input = new FileInputStream(tofile);
+				ftp.enterLocalActiveMode();
+				a.uploadFile(ftp, basePath, subDir, saveFileName, input);
+			}
+			else {
+				ftp.enterLocalActiveMode();
+				a.uploadFile(ftp, basePath, subDir, saveFileName, in);
+			}
+			a.closeFTP(ftp);
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				in.close();
+			} catch (IOException e2) {
+				e2.printStackTrace();
+			}
+			if(tofile != null) {
+				tofile.delete();
+			}
+			if(input != null) {
+				try {
+					input.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+		String url = baseUrl.concat(fileTypePath).concat(nowDate+"/")
+				.concat(saveFileName);
+		Log.info("文件访问路径:" + url);
+		Map<String, Object> map = new HashMap<String, Object>();
+		map.put("url", url);
+		map.put("width", width);
+		map.put("height", height);
+		return map;
+	}
+
 }

+ 1 - 0
src/main/java/com/minpay/common/util/FtpClientEntity.java

@@ -106,6 +106,7 @@ public class FtpClientEntity {
 //                 ftp.changeWorkingDirectory(path);
 			// 将上传文件存储到指定目录
 			ftp.setFileType(FTP.BINARY_FILE_TYPE);
+			ftp.enterLocalPassiveMode();
 			// 如果缺省该句 传输txt正常 但图片和其他格式的文件传输出现乱码
 			boolean c = ftp.storeFile(fileName, inputStream);
 			Log.info("上传结果:" + c);

+ 5 - 4
src/main/java/com/minpay/huicai/system/action/FileManageAction.java

@@ -28,7 +28,7 @@ import com.startup.minpay.util.Log;
 
 /**
  * 文件上传公共接口
- * 
+ *
  * @author ZHANGZZ
  *
  */
@@ -40,7 +40,7 @@ public class FileManageAction implements IMINAction {
 	private final static String UPLOAD_FILE = "uploadFile";
 
 	/**
-	 * 
+	 *
 	 * @param MINRequest
 	 * @param fileType   文件类型00图片01文档02视频
 	 * @param response
@@ -71,7 +71,8 @@ public class FileManageAction implements IMINAction {
 			throw new MINBusinessException("size参数异常,size不能小于0");
 		}
 
-		Map<String, Object> map = Service.lookup(IPublicService.class).uploadFile(file, fileType, 0, true);
+		//Map<String, Object> map = Service.lookup(IPublicService.class).uploadFile(file, fileType, 0, true);
+		Map<String, Object> map =Service.lookup(IPublicService.class).uploadFile(file, fileType, 0, true,"V01");
 
 		if ("00".equals(fileType) && !CommonUtil.isEmpty(proportion)) {
 			double minRatio = -1;
@@ -128,7 +129,7 @@ public class FileManageAction implements IMINAction {
 
 	/**
 	 * 多图片上传
-	 * 
+	 *
 	 * @param MINRequest
 	 * @param fileType   文件类型00图片01文档02视频
 	 * @param response