3 文件已重命名
12个文件已修改
2个文件已删除
4个文件已添加
| | |
| | | </dependency> |
| | | |
| | | |
| | | <!-- 文件上传工具类 --> |
| | | <dependency> |
| | | <groupId>commons-fileupload</groupId> |
| | | <artifactId>commons-fileupload</artifactId> |
| | | <version>1.4</version> |
| | | </dependency> |
| | | |
| | | |
| | | <!-- io常用工具类 --> |
| | | <dependency> |
| | | <groupId>commons-io</groupId> |
| | |
| | | </dependency> |
| | | |
| | | |
| | | <dependency> |
| | | <groupId>com.jcraft</groupId> |
| | | <artifactId>jsch</artifactId> |
| | | <version>0.1.53</version> |
| | | </dependency> |
| | | |
| | | |
| | | |
| | | </dependencies> |
| | | |
| | | |
| | |
| | | package com.dg.core; |
| | | |
| | | import com.dg.core.util.ServletUtils; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.web.servlet.config.annotation.CorsRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | |
| | | // 跨域允许时间 |
| | | .maxAge(3600); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取完整的请求路径,包括:域名,端口,上下文访问路径 |
| | | * |
| | | * @return 服务地址 |
| | | */ |
| | | public String getUrl() |
| | | { |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | return getDomain(request); |
| | | } |
| | | |
| | | public static String getDomain(HttpServletRequest request) |
| | | { |
| | | StringBuffer url = request.getRequestURL(); |
| | | String contextPath = request.getServletContext().getContextPath(); |
| | | return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString(); |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | import com.dg.core.CorsConfig; |
| | | import com.dg.core.ResultData; |
| | | import com.dg.core.db.gen.AjaxResult; |
| | | import com.dg.core.file.FtpConstants; |
| | | import com.dg.core.file.MimeTypeUtils; |
| | | import com.dg.core.file.SFTPUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | |
| | | import static com.dg.core.file.FileTypeUploadUtils.assertAllowed; |
| | | |
| | | |
| | | @Api(tags = {"通用接口"}) |
| | |
| | | |
| | | @Autowired |
| | | private CorsConfig corsConfig; |
| | | |
| | | |
| | | // FTP 登录用户名 |
| | | @Value("${ftp.username}") |
| | | private String userName; |
| | | // FTP 登录密码 |
| | | @Value("${ftp.password}") |
| | | private String password; |
| | | // FTP 服务器地址IP地址 |
| | | @Value("${ftp.host}") |
| | | private String host; |
| | | // FTP 端口 |
| | | @Value("${ftp.port}") |
| | | private int port; |
| | | @Value("${ftp.url}") |
| | | private String url; |
| | | |
| | | |
| | | @RequestMapping("/hello") |
| | |
| | | { |
| | | List<String> urls = new ArrayList<String>(); |
| | | List<String> fileNames = new ArrayList<String>(); |
| | | List<String> newFileNames = new ArrayList<String>(); |
| | | List<String> originalFilenames = new ArrayList<String>(); |
| | | for (MultipartFile file : files) { |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String name = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension; |
| | | try { |
| | | // assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | // SFTPUtil sftp = new SFTPUtil(); |
| | | // sftp.login(); |
| | | // InputStream is = file.getInputStream(); |
| | | // sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | // sftp.logout(); |
| | | // |
| | | // urls.add(url + "/idcard/" + name); |
| | | // fileNames.add(name); |
| | | // newFileNames.add(FileUtils.getName(name)); |
| | | // originalFilenames.add(file.getOriginalFilename()); |
| | | assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | InputStream is = file.getInputStream(); |
| | | sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | sftp.logout(); |
| | | |
| | | urls.add(url + "/idcard/" + name); |
| | | fileNames.add(name); |
| | | originalFilenames.add(file.getOriginalFilename()); |
| | | |
| | | } catch (Exception e) { |
| | | // log.error("上传照片失败【{}】", e.getMessage()); |
| | | return ResultData.error("上传文件失败"); |
| | | return ResultData.error("上传文件失败 "+e.getMessage()); |
| | | } |
| | | } |
| | | AjaxResult ajax = new AjaxResult(); |
| | | ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER)); |
| | | ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| | | return ResultData.success(ajax); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 通用上传请求(多个) |
| | | */ |
| | | @ApiOperation("文件上传接口(通用)") |
| | | @PostMapping("/upload") |
| | | public ResultData uploadFile(MultipartFile file) throws Exception |
| | | { |
| | | List<String> urls = new ArrayList<String>(); |
| | | List<String> fileNames = new ArrayList<String>(); |
| | | List<String> originalFilenames = new ArrayList<String>(); |
| | | |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String name = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension; |
| | | try { |
| | | assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | InputStream is = file.getInputStream(); |
| | | sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | sftp.logout(); |
| | | |
| | | // urls.add(url + "/idcard/" + name); |
| | | // fileNames.add(name); |
| | | // originalFilenames.add(file.getOriginalFilename()); |
| | | return ResultData.success(url + "/idcard/" + name); |
| | | } catch (Exception e) { |
| | | return ResultData.error("上传文件失败 "+e.getMessage()); |
| | | } |
| | | |
| | | |
| | | // AjaxResult ajax = new AjaxResult(); |
| | | // ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | // ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | // ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | import com.dg.core.util.TableDataInfo; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | |
| | | import com.dg.core.service.IGuideRepairOrderService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | |
| | | import com.dg.core.util.TableDataInfo; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.web.bind.annotation.*; |
| | |
| | | import com.dg.core.util.TableDataInfo; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | |
| | | import com.dg.core.util.TableDataInfo; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | |
| | | |
| | | import io.netty.channel.ConnectTimeoutException; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.http.Consts; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.HttpResponse; |
File was renamed from flower_city/src/main/java/com/dg/core/exception/base/BaseException.java |
| | |
| | | package com.dg.core.exception.base; |
| | | package com.dg.core.exception.file; |
| | | |
| | | |
| | | import com.dg.core.util.MessageUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | /** |
| | | * 基础异常 |
| | | * |
| | | * @author ruoyi |
| | | * |
| | | * @author |
| | | */ |
| | | public class BaseException extends RuntimeException |
| | | { |
| | | public class BaseException extends RuntimeException { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | |
| | | */ |
| | | private String defaultMessage; |
| | | |
| | | public BaseException(String module, String code, Object[] args, String defaultMessage) |
| | | { |
| | | public BaseException(String module, String code, Object[] args, String defaultMessage) { |
| | | this.module = module; |
| | | this.code = code; |
| | | this.args = args; |
| | | this.defaultMessage = defaultMessage; |
| | | } |
| | | |
| | | public BaseException(String module, String code, Object[] args) |
| | | { |
| | | public BaseException(String module, String code, Object[] args) { |
| | | this(module, code, args, null); |
| | | } |
| | | |
| | | public BaseException(String module, String defaultMessage) |
| | | { |
| | | public BaseException(String module, String defaultMessage) { |
| | | this(module, null, null, defaultMessage); |
| | | } |
| | | |
| | | public BaseException(String code, Object[] args) |
| | | { |
| | | public BaseException(String code, Object[] args) { |
| | | this(null, code, args, null); |
| | | } |
| | | |
| | | public BaseException(String defaultMessage) |
| | | { |
| | | public BaseException(String defaultMessage) { |
| | | this(null, null, null, defaultMessage); |
| | | } |
| | | |
| | | @Override |
| | | public String getMessage() |
| | | { |
| | | public String getMessage() { |
| | | String message = null; |
| | | if (!StringUtils.isEmpty(code)) |
| | | { |
| | | if (!StringUtils.isEmpty(code)) { |
| | | message = MessageUtils.message(code, args); |
| | | } |
| | | if (message == null) |
| | | { |
| | | if (message == null) { |
| | | message = defaultMessage; |
| | | } |
| | | return message; |
| | | } |
| | | |
| | | public String getModule() |
| | | { |
| | | public String getModule() { |
| | | return module; |
| | | } |
| | | |
| | | public String getCode() |
| | | { |
| | | public String getCode() { |
| | | return code; |
| | | } |
| | | |
| | | public Object[] getArgs() |
| | | { |
| | | public Object[] getArgs() { |
| | | return args; |
| | | } |
| | | |
| | | public String getDefaultMessage() |
| | | { |
| | | public String getDefaultMessage() { |
| | | return defaultMessage; |
| | | } |
| | | } |
| | |
| | | package com.dg.core.exception.file; |
| | | |
| | | |
| | | import com.dg.core.exception.base.BaseException; |
| | | |
| | | /** |
| | | * 文件信息异常类 |
| | |
| | | package com.dg.core.exception.file; |
| | | |
| | | import org.apache.commons.fileupload.FileUploadException; |
| | | |
| | | |
| | | import org.apache.tomcat.util.http.fileupload.FileUploadException; |
| | | |
| | | import java.util.Arrays; |
| | | |
File was renamed from flower_city/src/main/java/com/dg/core/util/MessageUtils.java |
| | |
| | | package com.dg.core.util; |
| | | package com.dg.core.exception.file; |
| | | |
| | | import com.dg.core.spring.SpringUtils; |
| | | import org.springframework.context.MessageSource; |
| | | import org.springframework.context.i18n.LocaleContextHolder; |
| | | |
| | |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class MessageUtils |
| | | { |
| | | public class MessageUtils { |
| | | /** |
| | | * 根据消息键和参数 获取消息 委托给spring messageSource |
| | | * |
| | | * @param code 消息键 |
| | | * @param args 参数 |
| | | * @param code |
| | | * 消息键 |
| | | * @param args |
| | | * 参数 |
| | | * @return 获取国际化翻译值 |
| | | */ |
| | | public static String message(String code, Object... args) |
| | | { |
| | | public static String message(String code, Object... args) { |
| | | MessageSource messageSource = SpringUtils.getBean(MessageSource.class); |
| | | return messageSource.getMessage(code, args, LocaleContextHolder.getLocale()); |
| | | } |
File was renamed from flower_city/src/main/java/com/dg/core/spring/SpringUtils.java |
| | |
| | | package com.dg.core.spring; |
| | | package com.dg.core.exception.file; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.aop.framework.AopContext; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| | |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ApplicationContextAware; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | /** |
| | | * spring工具类 方便在非spring管理环境中获取bean |
| | | * |
| | | * @author ruoyi |
| | | * @author huanghongfa |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware |
| | | { |
| | | public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware { |
| | | /** Spring应用上下文环境 */ |
| | | private static ConfigurableListableBeanFactory beanFactory; |
| | | |
| | | private static ApplicationContext applicationContext; |
| | | |
| | | @Override |
| | | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException |
| | | { |
| | | SpringUtils.beanFactory = beanFactory; |
| | | } |
| | | |
| | | @Override |
| | | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException |
| | | { |
| | | SpringUtils.applicationContext = applicationContext; |
| | | } |
| | | |
| | | /** |
| | | * 获取对象 |
| | |
| | | * |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getBean(String name) throws BeansException |
| | | { |
| | | return (T) beanFactory.getBean(name); |
| | | public static <T> T getBean(String name) throws BeansException { |
| | | return (T)beanFactory.getBean(name); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws BeansException |
| | | * |
| | | */ |
| | | public static <T> T getBean(Class<T> clz) throws BeansException |
| | | { |
| | | T result = (T) beanFactory.getBean(clz); |
| | | public static <T> T getBean(Class<T> clz) throws BeansException { |
| | | T result = beanFactory.getBean(clz); |
| | | return result; |
| | | } |
| | | |
| | |
| | | * @param name |
| | | * @return boolean |
| | | */ |
| | | public static boolean containsBean(String name) |
| | | { |
| | | public static boolean containsBean(String name) { |
| | | return beanFactory.containsBean(name); |
| | | } |
| | | |
| | |
| | | * @throws NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.isSingleton(name); |
| | | } |
| | | |
| | |
| | | * @throws NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static Class<?> getType(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.getType(name); |
| | | } |
| | | |
| | |
| | | * @throws NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static String[] getAliases(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { |
| | | return beanFactory.getAliases(name); |
| | | } |
| | | |
| | | /** |
| | | * 获取aop代理对象 |
| | | * |
| | | * |
| | | * @param invoker |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getAopProxy(T invoker) |
| | | { |
| | | return (T) AopContext.currentProxy(); |
| | | public static <T> T getAopProxy(T invoker) { |
| | | return (T)AopContext.currentProxy(); |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return 当前的环境配置 |
| | | */ |
| | | public static String[] getActiveProfiles() |
| | | { |
| | | public static String[] getActiveProfiles() { |
| | | return applicationContext.getEnvironment().getActiveProfiles(); |
| | | } |
| | | |
| | |
| | | * |
| | | * @return 当前的环境配置 |
| | | */ |
| | | public static String getActiveProfile() |
| | | { |
| | | public static String getActiveProfile() { |
| | | final String[] activeProfiles = getActiveProfiles(); |
| | | return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; |
| | | return !ObjectUtils.isEmpty(activeProfiles) ? activeProfiles[0] : null; |
| | | } |
| | | |
| | | @Override |
| | | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| | | SpringUtils.beanFactory = beanFactory; |
| | | } |
| | | |
| | | @Override |
| | | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| | | SpringUtils.applicationContext = applicationContext; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dg.core.file; |
| | | |
| | | import com.dg.core.exception.file.FileSizeLimitExceededException; |
| | | import com.dg.core.exception.file.InvalidExtensionException; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * 文件上传工具类 |
| | | * |
| | | * @author |
| | | */ |
| | | public class FileTypeUploadUtils { |
| | | /** |
| | | * 默认大小 50M |
| | | */ |
| | | public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024; |
| | | |
| | | /** |
| | | * 默认的文件名最大长度 100 |
| | | */ |
| | | public static final int DEFAULT_FILE_NAME_LENGTH = 100; |
| | | |
| | | |
| | | /** |
| | | * 文件大小校验 |
| | | * |
| | | * @param file 上传的文件 |
| | | * @return |
| | | * @throws FileSizeLimitExceededException 如果超出最大大小 |
| | | * @throws InvalidExtensionException |
| | | */ |
| | | public static final void assertAllowed(MultipartFile file, String[] allowedExtension) |
| | | throws FileSizeLimitExceededException, InvalidExtensionException { |
| | | long size = file.getSize(); |
| | | if (size > DEFAULT_MAX_SIZE) { |
| | | throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024); |
| | | } |
| | | |
| | | String fileName = file.getOriginalFilename(); |
| | | String extension = getExtension(file); |
| | | if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) { |
| | | if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) { |
| | | throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, |
| | | fileName); |
| | | } else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) { |
| | | throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, |
| | | fileName); |
| | | } else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) { |
| | | throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, |
| | | fileName); |
| | | } else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) { |
| | | throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension, |
| | | fileName); |
| | | } else { |
| | | throw new InvalidExtensionException(allowedExtension, extension, fileName); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 判断MIME类型是否是允许的MIME类型 |
| | | * |
| | | * @param extension |
| | | * @param allowedExtension |
| | | * @return |
| | | */ |
| | | public static final boolean isAllowedExtension(String extension, String[] allowedExtension) { |
| | | for (String str : allowedExtension) { |
| | | if (str.equalsIgnoreCase(extension)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取文件名的后缀 |
| | | * |
| | | * @param file 表单文件 |
| | | * @return 后缀名 |
| | | */ |
| | | public static final String getExtension(MultipartFile file) { |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | if (StringUtils.isEmpty(extension)) { |
| | | extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType())); |
| | | } |
| | | return extension; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dg.core.file; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | | * @description: ftp |
| | | * @author: huang.hongfa weixin hhf9596 qq 959656820 |
| | | * @create: 2020-11-30 13:39 |
| | | **/ |
| | | public class FtpConstants { |
| | | /** |
| | | * ftp站点 |
| | | */ |
| | | public static final String FTPHOST = "47.104.148.185"; |
| | | /** |
| | | * 端口号 |
| | | */ |
| | | public static final Integer FTPPORT = 22; |
| | | /** |
| | | * ftp访问用户名 |
| | | */ |
| | | public static final String FTPUSERNAME = "root"; |
| | | /** |
| | | * ftp访问密码 |
| | | */ |
| | | public static final String FTPPASSWORD = "evanN7F2K5M6"; |
| | | /** |
| | | * excel模板 |
| | | */ |
| | | public static final String FTPFILEPATH_MUBAN = "/mnt/data/ftp/muban/"; |
| | | /** |
| | | * 党员保存 |
| | | */ |
| | | public static final String FTPFILEPATH_IDCARD = "/mnt/data/ftp/idcard/"; |
| | | /** |
| | | * http访问路径 |
| | | */ |
| | | public static final String HTTP_URL = "http://image.panzhihua.nhys.cdnhxx.com/"; |
| | | } |
New file |
| | |
| | | package com.dg.core.file; |
| | | |
| | | /** |
| | | * 媒体类型工具类 |
| | | * |
| | | * @author |
| | | */ |
| | | public class MimeTypeUtils |
| | | { |
| | | public static final String IMAGE_PNG = "image/png"; |
| | | |
| | | public static final String IMAGE_JPG = "image/jpg"; |
| | | |
| | | public static final String IMAGE_JPEG = "image/jpeg"; |
| | | |
| | | public static final String IMAGE_BMP = "image/bmp"; |
| | | |
| | | public static final String IMAGE_GIF = "image/gif"; |
| | | |
| | | public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" }; |
| | | |
| | | public static final String[] FLASH_EXTENSION = { "swf", "flv" }; |
| | | |
| | | public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg", |
| | | "asf", "rm", "rmvb" }; |
| | | |
| | | public static final String[] VIDEO_EXTENSION = { "mp4", "avi", "rmvb" }; |
| | | |
| | | public static final String[] DEFAULT_ALLOWED_EXTENSION = { |
| | | // 图片 |
| | | "bmp", "gif", "jpg", "jpeg", "png", |
| | | // word excel powerpoint |
| | | "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt", |
| | | // 压缩文件 |
| | | "rar", "zip", "gz", "bz2", |
| | | // 视频格式 |
| | | "mp4", "avi", "rmvb", |
| | | // pdf |
| | | "pdf" }; |
| | | |
| | | public static String getExtension(String prefix) |
| | | { |
| | | switch (prefix) |
| | | { |
| | | case IMAGE_PNG: |
| | | return "png"; |
| | | case IMAGE_JPG: |
| | | return "jpg"; |
| | | case IMAGE_JPEG: |
| | | return "jpeg"; |
| | | case IMAGE_BMP: |
| | | return "bmp"; |
| | | case IMAGE_GIF: |
| | | return "gif"; |
| | | default: |
| | | return ""; |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.dg.core.file; |
| | | |
| | | import com.jcraft.jsch.*; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import java.io.*; |
| | | import java.util.Properties; |
| | | import java.util.Vector; |
| | | |
| | | /** |
| | | * @version 1.0.0 |
| | | * @ClassName: SFTPUtil |
| | | * @Description: sftp连接工具类 |
| | | */ |
| | | public class SFTPUtil { |
| | | private transient Logger log = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | private ChannelSftp sftp; |
| | | |
| | | private Session session; |
| | | |
| | | // FTP 登录用户名 |
| | | private String userName; |
| | | // FTP 登录密码 |
| | | private String password; |
| | | // FTP 服务器地址IP地址 |
| | | private String host; |
| | | // FTP 端口 |
| | | private int port; |
| | | |
| | | /** |
| | | * 构造基于密码认证的sftp对象 |
| | | * |
| | | * @param userName |
| | | * @param password |
| | | * @param host |
| | | * @param port |
| | | */ |
| | | public SFTPUtil(String userName, String password, String host, int port) { |
| | | this.userName = userName; |
| | | this.password = password; |
| | | this.host = host; |
| | | this.port = port; |
| | | } |
| | | |
| | | public SFTPUtil() { |
| | | this.userName = FtpConstants.FTPUSERNAME; |
| | | this.password = FtpConstants.FTPPASSWORD; |
| | | this.host = FtpConstants.FTPHOST; |
| | | this.port = FtpConstants.FTPPORT; |
| | | } |
| | | |
| | | public static void main(String[] args) throws SftpException, IOException { |
| | | SFTPUtil sftp = new SFTPUtil("root", "evanN7F2K5M6", "47.104.148.185", 22); |
| | | sftp.login(); |
| | | File file = new File("F:\\settings.xml"); |
| | | InputStream is = new FileInputStream(file); |
| | | // 多级目录创建并上传 |
| | | sftp.uploadMore("/mnt/data/ftp/ss", "settings.xml", is); |
| | | sftp.logout(); |
| | | } |
| | | |
| | | /** |
| | | * 连接sftp服务器 |
| | | * |
| | | * @throws Exception |
| | | */ |
| | | public void login() { |
| | | try { |
| | | JSch jsch = new JSch(); |
| | | log.info("sftp connect by host:{} username:{}", host, userName); |
| | | |
| | | session = jsch.getSession(userName, host, port); |
| | | log.info("Session is build"); |
| | | if (password != null) { |
| | | session.setPassword(password); |
| | | } |
| | | Properties config = new Properties(); |
| | | config.put("StrictHostKeyChecking", "no"); |
| | | |
| | | session.setConfig(config); |
| | | session.connect(); |
| | | log.info("Session is connected"); |
| | | |
| | | Channel channel = session.openChannel("sftp"); |
| | | channel.connect(); |
| | | log.info("channel is connected"); |
| | | |
| | | sftp = (ChannelSftp)channel; |
| | | log.info(String.format("sftp server host:[%s] port:[%s] is connect successfull", host, port)); |
| | | } catch (JSchException e) { |
| | | log.error("Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", host, port, |
| | | e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 关闭连接 server |
| | | */ |
| | | public void logout() { |
| | | if (sftp != null) { |
| | | if (sftp.isConnected()) { |
| | | sftp.disconnect(); |
| | | log.info("sftp is closed already"); |
| | | } |
| | | } |
| | | if (session != null) { |
| | | if (session.isConnected()) { |
| | | session.disconnect(); |
| | | log.info("sshSession is closed already"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 递归根据路径创建文件夹 |
| | | * |
| | | * @param dirs |
| | | * 根据 / 分隔后的数组文件夹名称 |
| | | * @param tempPath |
| | | * 拼接路径 |
| | | * @param length |
| | | * 文件夹的格式 |
| | | * @param index |
| | | * 数组下标 |
| | | * @return |
| | | */ |
| | | public void mkdirDir(String[] dirs, String tempPath, int length, int index) { |
| | | // 以"/a/b/c/d"为例按"/"分隔后,第0位是"";顾下标从1开始 |
| | | index++; |
| | | if (index < length) { |
| | | // 目录不存在,则创建文件夹 |
| | | tempPath += "/" + dirs[index]; |
| | | } |
| | | try { |
| | | log.info("检测目录[" + tempPath + "]"); |
| | | sftp.cd(tempPath); |
| | | if (index < length) { |
| | | mkdirDir(dirs, tempPath, length, index); |
| | | } |
| | | } catch (SftpException ex) { |
| | | log.warn("创建目录[" + tempPath + "]"); |
| | | try { |
| | | sftp.mkdir(tempPath); |
| | | sftp.cd(tempPath); |
| | | } catch (SftpException e) { |
| | | e.printStackTrace(); |
| | | log.error("创建目录[" + tempPath + "]失败,异常信息[" + e.getMessage() + "]"); |
| | | |
| | | } |
| | | log.info("进入目录[" + tempPath + "]"); |
| | | mkdirDir(dirs, tempPath, length, index); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将输入流的数据上传到sftp作为文件(多层目录) |
| | | * |
| | | * @param directory |
| | | * 上传到该目录(多层目录) |
| | | * @param sftpFileName |
| | | * sftp端文件名 |
| | | * @param input |
| | | * 输入流 |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | | public void uploadMore(String directory, String sftpFileName, InputStream input) throws SftpException { |
| | | try { |
| | | sftp.cd(directory); |
| | | } catch (SftpException e) { |
| | | // 目录不存在,则创建文件夹 |
| | | String[] dirs = directory.split("/"); |
| | | String tempPath = ""; |
| | | int index = 0; |
| | | mkdirDir(dirs, tempPath, dirs.length, index); |
| | | } |
| | | sftp.put(input, sftpFileName);// 上传文件 |
| | | } |
| | | |
| | | /** |
| | | * 将输入流的数据上传到sftp作为文件 |
| | | * |
| | | * @param directory |
| | | * 上传到该目录(单层目录) |
| | | * @param sftpFileName |
| | | * sftp端文件名 |
| | | * @param input |
| | | * 输入流 |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | | public void upload(String directory, String sftpFileName, InputStream input) throws SftpException { |
| | | try { |
| | | sftp.cd(directory); |
| | | } catch (SftpException e) { |
| | | log.warn("directory is not exist"); |
| | | sftp.mkdir(directory); |
| | | sftp.cd(directory); |
| | | } |
| | | sftp.put(input, sftpFileName); |
| | | log.info("file:{} is upload successful", sftpFileName); |
| | | } |
| | | |
| | | /** |
| | | * 上传单个文件 |
| | | * |
| | | * @param directory |
| | | * 上传到sftp目录 |
| | | * @param uploadFile |
| | | * 要上传的文件,包括路径 |
| | | * @throws FileNotFoundException |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | | public void upload(String directory, String uploadFile) throws FileNotFoundException, SftpException { |
| | | File file = new File(uploadFile); |
| | | upload(directory, file.getName(), new FileInputStream(file)); |
| | | } |
| | | |
| | | /** |
| | | * 将byte[]上传到sftp,作为文件。注意:从String生成byte[]是,要指定字符集。 |
| | | * |
| | | * @param directory |
| | | * 上传到sftp目录 |
| | | * @param sftpFileName |
| | | * 文件在sftp端的命名 |
| | | * @param byteArr |
| | | * 要上传的字节数组 |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | | public void upload(String directory, String sftpFileName, byte[] byteArr) throws SftpException { |
| | | upload(directory, sftpFileName, new ByteArrayInputStream(byteArr)); |
| | | } |
| | | |
| | | /** |
| | | * 将字符串按照指定的字符编码上传到sftp |
| | | * |
| | | * @param directory |
| | | * 上传到sftp目录 |
| | | * @param sftpFileName |
| | | * 文件在sftp端的命名 |
| | | * @param dataStr |
| | | * 待上传的数据 |
| | | * @param charsetName |
| | | * sftp上的文件,按该字符编码保存 |
| | | * @throws UnsupportedEncodingException |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | | public void upload(String directory, String sftpFileName, String dataStr, String charsetName) |
| | | throws UnsupportedEncodingException, SftpException { |
| | | upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName))); |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param directory |
| | | * 下载目录 |
| | | * @param downloadFile |
| | | * 下载的文件 |
| | | * @param saveFile |
| | | * 存在本地的路径 |
| | | * @throws SftpException |
| | | * @throws FileNotFoundException |
| | | * @throws Exception |
| | | */ |
| | | public void download(String directory, String downloadFile, String saveFile) |
| | | throws SftpException, FileNotFoundException { |
| | | if (directory != null && !"".equals(directory)) { |
| | | sftp.cd(directory); |
| | | } |
| | | File file = new File(saveFile); |
| | | sftp.get(downloadFile, new FileOutputStream(file)); |
| | | log.info("file:{} is download successful", downloadFile); |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param directory |
| | | * 下载目录 |
| | | * @param downloadFile |
| | | * 下载的文件名 |
| | | * @return 字节数组 |
| | | * @throws SftpException |
| | | * @throws IOException |
| | | * @throws Exception |
| | | */ |
| | | public byte[] download(String directory, String downloadFile) throws SftpException, IOException { |
| | | if (directory != null && !"".equals(directory)) { |
| | | sftp.cd(directory); |
| | | } |
| | | InputStream is = sftp.get(downloadFile); |
| | | byte[] fileData = IOUtils.toByteArray(is); |
| | | log.info("file:{} is download successful", downloadFile); |
| | | return fileData; |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param directory |
| | | * 下载目录 |
| | | * @param downloadFile |
| | | * 下载的文件名 |
| | | * @return 字节数组 |
| | | * @throws SftpException |
| | | * @throws IOException |
| | | * @throws Exception |
| | | */ |
| | | public InputStream downloadInputStream(String directory, String downloadFile) throws SftpException, IOException { |
| | | if (directory != null && !"".equals(directory)) { |
| | | sftp.cd(directory); |
| | | } |
| | | InputStream is = sftp.get(downloadFile); |
| | | // byte[] fileData = IOUtils.toByteArray(is); |
| | | // log.info("file:{} is download successful", downloadFile); |
| | | return is; |
| | | } |
| | | |
| | | /** |
| | | * 删除文件 |
| | | * |
| | | * @param directory |
| | | * 要删除文件所在目录 |
| | | * @param deleteFile |
| | | * 要删除的文件 |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | | public void delete(String directory, String deleteFile) throws SftpException { |
| | | sftp.cd(directory); |
| | | sftp.rm(deleteFile); |
| | | } |
| | | |
| | | /** |
| | | * 列出目录下的文件 |
| | | * |
| | | * @param directory |
| | | * 要列出的目录 |
| | | * @return |
| | | * @throws SftpException |
| | | */ |
| | | public Vector<?> listFiles(String directory) throws SftpException { |
| | | return sftp.ls(directory); |
| | | } |
| | | |
| | | /** |
| | | * 判断文件或者目录是否存在 |
| | | * |
| | | * @param path |
| | | * 目录或者文件的路径 |
| | | * @return 存在结果 |
| | | */ |
| | | public boolean isExistDir(String path) { |
| | | boolean isExist = false; |
| | | try { |
| | | SftpATTRS sftpATTRS = sftp.lstat(path); |
| | | isExist = true; |
| | | return sftpATTRS.isDir(); |
| | | } catch (Exception e) { |
| | | if (e.getMessage().toLowerCase().equals("no such file")) { |
| | | isExist = false; |
| | | } |
| | | } |
| | | return isExist; |
| | | |
| | | } |
| | | } |
| | |
| | | spring.datasource.username=root |
| | | spring.datasource.password=nahanMysqlPwd&&RemoteRemote2019nahan |
| | | #spring.datasource.url=jdbc:mysql://10.8.0.74:3306/flower_city_automessage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&allowMultiQueries=true&serverTimezone=GMT%2B8 |
| | | spring.datasource.url=jdbc:mysql://localhost:3306/flower_city_automessage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&allowMultiQueries=true&serverTimezone=GMT%2B8 |
| | | #spring.datasource.url=jdbc:mysql://10.8.0.74:3306/huacheng_smart_life?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&allowMultiQueries=true&serverTimezone=GMT%2B8 |
| | | #spring.datasource.url=jdbc:mysql://localhost:3306/flower_city_automessage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&allowMultiQueries=true&serverTimezone=GMT%2B8 |
| | | spring.datasource.url=jdbc:mysql://10.8.0.74:3306/huacheng_smart_life?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&allowMultiQueries=true&serverTimezone=GMT%2B8 |
| | | |
| | | |
| | | |
| | |
| | | |
| | | |
| | | |
| | | ftp.username=root |
| | | ftp.password=6-gj!bx@TNb!Dwpjd]1h1Kvqmc |
| | | ftp.host=182.151.31.48 |
| | | ftp.port=22 |
| | | ftp.url=http://test.panzhihua.nhys.cdnhxx.com/ |
| | | |
| | | |
| | | |
| | | |