|
|
@@ -0,0 +1,292 @@
|
|
|
+package com.template.controller;
|
|
|
+
|
|
|
+import com.jcraft.jsch.*;
|
|
|
+import com.template.annotation.DESRespondSecret;
|
|
|
+import com.template.annotation.PassToken;
|
|
|
+import com.template.api.ExcelControllerAPI;
|
|
|
+import com.template.api.FileControllerAPI;
|
|
|
+import com.template.common.utils.CustomMultipartFile;
|
|
|
+import com.template.common.utils.TimeExchange;
|
|
|
+import com.template.model.pojo.SmartAccess;
|
|
|
+import com.template.model.pojo.SmartApply;
|
|
|
+import com.template.model.pojo.SmartUser;
|
|
|
+import com.template.model.pojo.SystemUser;
|
|
|
+import com.template.model.request.uploadFileResponse;
|
|
|
+import com.template.model.result.CommonResult;
|
|
|
+import com.template.model.result.PageUtils;
|
|
|
+import com.template.services.SmartAccessService;
|
|
|
+import com.template.services.SmartApplyService;
|
|
|
+import com.template.services.SmartUserService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RestController
|
|
|
+//返回参数加密注解
|
|
|
+@DESRespondSecret
|
|
|
+public class FileController implements FileControllerAPI {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SmartAccessService smartAccessService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @PassToken
|
|
|
+ public CommonResult uploadFilesByCos() throws Exception {
|
|
|
+ List<SmartAccess> updateUsers = new ArrayList<>();
|
|
|
+
|
|
|
+ for (int i = 0;i<15000;i++) {
|
|
|
+ Thread.sleep(5000);
|
|
|
+ PageUtils<SmartAccess> pageDatas = smartAccessService.getPageDatas(1,120);
|
|
|
+ List<SmartAccess> users = pageDatas != null && pageDatas.getList().size() > 0 ? pageDatas.getList() : new ArrayList<>();
|
|
|
+ for (SmartAccess user : users) {
|
|
|
+ if(org.springframework.util.StringUtils.hasText(user.getImage())){
|
|
|
+ MultipartFile fileData = convert(user.getImage());
|
|
|
+ String result = uploadFileStatic(fileData,"/home/images/warnings","https://xj.chuanghai-tech.com/wzimage/warnings/");
|
|
|
+ if(org.springframework.util.StringUtils.hasText(result)){
|
|
|
+ user.setImage(result);
|
|
|
+ updateUsers.add(user);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ String sdsd = "";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean result = smartAccessService.updateBatchById(updateUsers);
|
|
|
+ }
|
|
|
+
|
|
|
+ return CommonResult.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ //图片格式 头像 https://xj.chuanghai-tech.com/wzimage/355377EF8A889F8C9009ABA2B9F0C08A.png
|
|
|
+ //告警图片 https://xj.chuanghai-tech.com/wzimage/warnings/v2-bb4414441455d233c5d963cbd4c6ba26_xld.jpeg
|
|
|
+ @Override
|
|
|
+ public CommonResult uploadFile(MultipartFile file) throws Exception {
|
|
|
+ //限制文件类型
|
|
|
+ List<String> allowedTypes = new ArrayList<>();
|
|
|
+ allowedTypes.add("image/png");
|
|
|
+ allowedTypes.add("image/jpeg");
|
|
|
+ allowedTypes.add("image/jpg");
|
|
|
+ allowedTypes.add("application/pdf");
|
|
|
+ allowedTypes.add("audio/mpeg");
|
|
|
+ allowedTypes.add("video/mp4");
|
|
|
+ String contentType = file.getContentType();
|
|
|
+
|
|
|
+ if (!allowedTypes.contains(contentType)) {
|
|
|
+ return CommonResult.fail("不支持的文件类型: " + contentType);
|
|
|
+ }
|
|
|
+ System.out.println( "测试上传文件");
|
|
|
+ uploadFileResponse result = new uploadFileResponse();
|
|
|
+ System.out.println( "测试上传文件1");
|
|
|
+ String url = uploadFileStatic(file,"/home/images","https://xj.chuanghai-tech.com/wzimage/");
|
|
|
+ System.out.println( "测试上传文件2");
|
|
|
+ if(org.springframework.util.StringUtils.hasText(url)){
|
|
|
+ result.setFileUrl(url);
|
|
|
+ }else{
|
|
|
+ return CommonResult.fail();
|
|
|
+ }
|
|
|
+ return CommonResult.ok(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String uploadFileStatic(MultipartFile file, String remotePath,String urlEx) throws IOException {
|
|
|
+ uploadFileResponse result = new uploadFileResponse();
|
|
|
+ System.out.println("上传文件进来了");
|
|
|
+ //String name = TimeExchange.DateNowTimeStamo() + "-" + file.getOriginalFilename();
|
|
|
+ String name = file.getOriginalFilename().replace(" ","");
|
|
|
+
|
|
|
+ String fileName = getDecodedFileNameFromUrl(name);
|
|
|
+ if (fileName.contains(",")) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String user = "root"; //SFTP服务器用户名
|
|
|
+ String password = "Chuanghai2023"; // SFTP服务器密码
|
|
|
+ String host = "223.84.177.126"; // SFTP服务器地址
|
|
|
+ System.out.println("服务器地址"+host);
|
|
|
+ int port = 22; // SFTP 服务器端口
|
|
|
+ String remoteDirPath = remotePath; // 远程目录路径
|
|
|
+
|
|
|
+ String urlexist = urlEx + fileName;
|
|
|
+ if (isImageUrlValid(urlexist)) {
|
|
|
+ result.setFileUrl(urlexist);
|
|
|
+ return result.getFileUrl();
|
|
|
+ }
|
|
|
+
|
|
|
+ //从MultipartFile对象中获取流
|
|
|
+ InputStream inputStream = null;
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+ //String downpath="/book/《Python爬虫开发与项目实战》.pdf";
|
|
|
+ //String spath="C:/Users/F1339769/Desktop";
|
|
|
+ JSch jsch = new JSch();
|
|
|
+ Session session = null;
|
|
|
+ ChannelSftp channelSftp = null;
|
|
|
+ try {
|
|
|
+ session = jsch.getSession(user, host, port);
|
|
|
+ session.setPassword(password);
|
|
|
+ session.setConfig("StrictHostKeyChecking", "no");
|
|
|
+ session.connect(30000);
|
|
|
+ System.out.println("连接成功");
|
|
|
+ channelSftp = (ChannelSftp) session.openChannel("sftp");
|
|
|
+ channelSftp.connect();
|
|
|
+ // 创建远程目录
|
|
|
+ String[] dirArr = remoteDirPath.split("/");
|
|
|
+ String path = "";
|
|
|
+ for (int i = 0; i < dirArr.length; i++) {
|
|
|
+ path = path + "/" + dirArr[i];
|
|
|
+ try {
|
|
|
+ channelSftp.cd(path);
|
|
|
+ } catch (SftpException e) {
|
|
|
+ channelSftp.mkdir(path);
|
|
|
+ channelSftp.cd(path);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 文件上传到远程服务器上
|
|
|
+ channelSftp.put(inputStream, fileName);
|
|
|
+ // 从远程服务器上下载文件
|
|
|
+ //channelSftp.get(downpath, spath);
|
|
|
+ } catch (JSchException | SftpException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (channelSftp != null) {
|
|
|
+ channelSftp.disconnect();
|
|
|
+ }
|
|
|
+ if (session != null) {
|
|
|
+ session.disconnect();
|
|
|
+ }
|
|
|
+
|
|
|
+ String url = urlEx + fileName;
|
|
|
+
|
|
|
+ System.out.println(remoteDirPath);
|
|
|
+ System.out.println(fileName);
|
|
|
+ System.out.println(name);
|
|
|
+
|
|
|
+ if (isImageUrlValid(urlexist)) {
|
|
|
+ result.setFileUrl(url);
|
|
|
+ }else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result.getFileUrl();
|
|
|
+ }
|
|
|
+ public static boolean isImageUrlValid(String imageUrl) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(imageUrl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("HEAD");
|
|
|
+ connection.setConnectTimeout(5000);
|
|
|
+ connection.setReadTimeout(5000);
|
|
|
+
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+
|
|
|
+ // 检查HTTP状态码
|
|
|
+ if (responseCode != HttpURLConnection.HTTP_OK) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查Content-Type是否为图片类型
|
|
|
+ String contentType = connection.getContentType();
|
|
|
+ return contentType != null && contentType.startsWith("image/");
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在线图片地址转换为MultipartFile文件
|
|
|
+ *
|
|
|
+ * @param imageUrl
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static MultipartFile convert(String imageUrl) throws IOException {
|
|
|
+ System.out.println(imageUrl);
|
|
|
+ URL url = new URL(imageUrl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ // 设置请求头,避免被服务器拒绝
|
|
|
+ connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
|
|
+
|
|
|
+ try (InputStream inputStream = connection.getInputStream();
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
|
+
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] bytes = outputStream.toByteArray();
|
|
|
+ String fileName = extractFilenameFromUrl(imageUrl);
|
|
|
+ String contentType = determineContentTypeFromUrl(imageUrl);
|
|
|
+
|
|
|
+ return new CustomMultipartFile(bytes, fileName, contentType);
|
|
|
+ } finally {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从URL中提取文件名
|
|
|
+ */
|
|
|
+ private static String extractFilenameFromUrl(String imageUrl) {
|
|
|
+ try {
|
|
|
+ String path = new URL(imageUrl).getPath();
|
|
|
+ String filename = path.substring(path.lastIndexOf('/') + 1);
|
|
|
+
|
|
|
+ // 如果文件名没有扩展名,添加默认扩展名
|
|
|
+ if (!filename.contains(".")) {
|
|
|
+ filename += ".jpg";
|
|
|
+ }
|
|
|
+
|
|
|
+ return filename;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "downloaded_image.jpg";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据URL推断内容类型
|
|
|
+ */
|
|
|
+ private static String determineContentTypeFromUrl(String imageUrl) {
|
|
|
+ if (imageUrl.toLowerCase().endsWith(".jpg") || imageUrl.toLowerCase().endsWith(".jpeg")) {
|
|
|
+ return "image/jpeg";
|
|
|
+ } else if (imageUrl.toLowerCase().endsWith(".png")) {
|
|
|
+ return "image/png";
|
|
|
+ } else if (imageUrl.toLowerCase().endsWith(".gif")) {
|
|
|
+ return "image/gif";
|
|
|
+ } else if (imageUrl.toLowerCase().endsWith(".bmp")) {
|
|
|
+ return "image/bmp";
|
|
|
+ } else if (imageUrl.toLowerCase().endsWith(".webp")) {
|
|
|
+ return "image/webp";
|
|
|
+ } else {
|
|
|
+ return "image/jpeg"; // 默认类型
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDecodedFileNameFromUrl(String url) {
|
|
|
+ try {
|
|
|
+ // 提取URL中的文件名部分
|
|
|
+ String fileName = url.substring(url.lastIndexOf('/') + 1);
|
|
|
+
|
|
|
+ // 如果有查询参数,去掉查询参数部分
|
|
|
+ if (fileName.contains("?")) {
|
|
|
+ fileName = fileName.substring(0, fileName.indexOf("?"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // URL解码
|
|
|
+ return URLDecoder.decode(fileName, "UTF-8");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|