New file |
| | |
| | | package com.panzhihua.grid_app.api; |
| | | |
| | | import com.panzhihua.common.constants.FtpConstants; |
| | | import com.panzhihua.common.model.vos.R; |
| | | import com.panzhihua.common.utlis.SFTPUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.Arrays; |
| | | import java.util.Base64; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | | * @description: 公共接口 |
| | | * @author: huang.hongfa weixin hhf9596 qq 959656820 |
| | | * @create: 2020-12-10 15:41 |
| | | **/ |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping("/common/") |
| | | @Api(tags = {"公共接口"}) |
| | | public class CommonApi { |
| | | |
| | | // 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; |
| | | |
| | | /** |
| | | * 允许的图片文件后缀 |
| | | */ |
| | | private static List<String> fileExtensionAllow = Arrays.asList("gif", "jpg", "png", "jpeg"); |
| | | /** |
| | | * 允许的视频文件后缀 |
| | | */ |
| | | private static List<String> videoExtensionAllow = Arrays.asList("mp4", "mov"); |
| | | |
| | | @ApiOperation(value = "上传照片/视频 (jpg/jpeg/png/mp4/mov)") |
| | | @PostMapping(value = "uploadimage", consumes = "multipart/*", headers = "content-type=multipart/form-date") |
| | | public R uploadImage(@RequestParam MultipartFile file, HttpServletRequest request) throws IOException { |
| | | // 微信图片内容校验 |
| | | // WxMaSecCheckService wxMaSecCheckService = wxMaConfiguration.getMaService().getSecCheckService(); |
| | | String property = System.getProperty("user.dir"); |
| | | |
| | | String fileExtension = ".jpg"; |
| | | |
| | | String originName = file.getOriginalFilename(); |
| | | AtomicBoolean isVideo = new AtomicBoolean(false); |
| | | videoExtensionAllow.forEach(ext ->{ |
| | | String originNameLowerCase = originName.toLowerCase(); |
| | | if(originNameLowerCase.endsWith("." +ext)){ |
| | | isVideo.set(true); |
| | | } |
| | | }); |
| | | if(isVideo.get()){ |
| | | fileExtension = ".mp4"; |
| | | } |
| | | |
| | | |
| | | String fileName = property + File.separator + UUID.randomUUID().toString().replace("-", "") + fileExtension; |
| | | File file1 = new File(fileName); |
| | | |
| | | String name = file.getOriginalFilename(); |
| | | name = UUID.randomUUID().toString().replaceAll("-", "") + fileExtension; |
| | | try { |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | InputStream is = file.getInputStream(); |
| | | file.transferTo(file1); |
| | | boolean delete = file1.delete(); |
| | | log.info("临时文件删除【{}】", delete); |
| | | sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | sftp.logout(); |
| | | return R.ok(url + "/idcard/" + name); |
| | | } catch (Exception e) { |
| | | log.error("上传照片失败【{}】", e.getMessage()); |
| | | return R.fail(); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |