|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|