2024-01-22 11:29:27 +08:00
|
|
|
|
package com.zcloud.util;
|
|
|
|
|
|
2024-03-19 16:01:15 +08:00
|
|
|
|
import com.aliyun.oss.ClientException;
|
2024-01-22 11:29:27 +08:00
|
|
|
|
import com.aliyun.oss.OSS;
|
|
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
|
import com.aliyun.oss.OSSException;
|
|
|
|
|
import com.jcraft.jsch.*;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
|
|
|
|
import com.zcloud.entity.PageData;
|
|
|
|
|
import com.zcloud.service.system.FileUploadLogService;
|
2024-03-19 16:01:15 +08:00
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
2024-01-22 11:29:27 +08:00
|
|
|
|
import org.omg.CORBA.SystemException;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class Smb {
|
|
|
|
|
@Resource
|
|
|
|
|
private FileUploadLogService fileUploadLogService;
|
|
|
|
|
|
|
|
|
|
private static String endpoint = "https://oss-cn-zhangjiakou.aliyuncs.com";
|
|
|
|
|
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
|
2025-08-10 23:45:34 +08:00
|
|
|
|
private static String accessKeyId = "LTAI5tL5QVQ3E6wySAuvbLbo";
|
|
|
|
|
private static String accessKeySecret = "BaaiuHEeAaYOquqmWcAN5Mf5giWaAC";
|
2024-01-22 11:29:27 +08:00
|
|
|
|
// 填写Bucket名称。
|
|
|
|
|
private static String bucketName = "qyag";
|
|
|
|
|
|
|
|
|
|
public static void sshSftp(MultipartFile file, String fileName, String path) throws Exception {
|
|
|
|
|
|
|
|
|
|
String objectName = "YTHFile" + path + "/" + fileName;
|
|
|
|
|
// 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
|
|
|
|
|
// 创建OSSClient实例。
|
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
// 创建PutObject请求。
|
|
|
|
|
ossClient.putObject(bucketName, objectName, file.getInputStream());
|
|
|
|
|
// 创建PutObjectRequest对象。
|
|
|
|
|
// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
|
|
|
|
|
// ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
|
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
|
|
|
|
|
// metadata.setObjectAcl(CannedAccessControlList.Private);
|
|
|
|
|
// putObjectRequest.setMetadata(metadata);
|
|
|
|
|
|
|
|
|
|
// 上传文件。
|
|
|
|
|
} catch (OSSException oe) {
|
|
|
|
|
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
|
|
+ "but was rejected with an error response for some reason.");
|
|
|
|
|
System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
|
|
System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
|
|
System.out.println("Request ID:" + oe.getRequestId());
|
|
|
|
|
System.out.println("Host ID:" + oe.getHostId());
|
|
|
|
|
} catch (Exception ce) {
|
|
|
|
|
System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
|
|
+ "a serious internal problem while trying to communicate with OSS, "
|
|
|
|
|
+ "such as not being able to access the network.");
|
|
|
|
|
System.out.println("Error Message:" + ce.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (ossClient != null) {
|
|
|
|
|
ossClient.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void sshSftp(MultipartFile file, String fileName, String path, String corpId) throws Exception {
|
|
|
|
|
|
|
|
|
|
String objectName = "YTHFile" + path + "/" + fileName;
|
|
|
|
|
// 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
|
|
|
|
|
// 创建OSSClient实例。
|
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
// 创建PutObject请求。
|
|
|
|
|
ossClient.putObject(bucketName, objectName, file.getInputStream());
|
|
|
|
|
// 创建PutObjectRequest对象。
|
|
|
|
|
// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
|
|
|
|
|
// ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
|
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
|
|
|
|
|
// metadata.setObjectAcl(CannedAccessControlList.Private);
|
|
|
|
|
// putObjectRequest.setMetadata(metadata);
|
|
|
|
|
|
|
|
|
|
// 上传文件。
|
|
|
|
|
|
|
|
|
|
long fileSize = file.getSize();//单位:字节
|
|
|
|
|
PageData pd = new PageData();
|
|
|
|
|
pd.put("FILEUPLOADLOG_ID", UuidUtil.get32UUID());
|
|
|
|
|
pd.put("CORPINFO_ID", corpId);
|
|
|
|
|
pd.put("CREATTIME", DateUtil.date2Str(new Date()));
|
|
|
|
|
pd.put("FILE_PATH", path + "/" + fileName);
|
|
|
|
|
pd.put("FILE_SIZE", fileSize);
|
|
|
|
|
fileUploadLogService.save(pd);
|
|
|
|
|
} catch (OSSException oe) {
|
|
|
|
|
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
|
|
+ "but was rejected with an error response for some reason.");
|
|
|
|
|
System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
|
|
System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
|
|
System.out.println("Request ID:" + oe.getRequestId());
|
|
|
|
|
System.out.println("Host ID:" + oe.getHostId());
|
|
|
|
|
} catch (Exception ce) {
|
|
|
|
|
System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
|
|
+ "a serious internal problem while trying to communicate with OSS, "
|
|
|
|
|
+ "such as not being able to access the network.");
|
|
|
|
|
System.out.println("Error Message:" + ce.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (ossClient != null) {
|
|
|
|
|
ossClient.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除文件
|
|
|
|
|
*
|
|
|
|
|
* @throws SftpException
|
|
|
|
|
* @throws JSchException
|
|
|
|
|
*/
|
|
|
|
|
public static void delete(String directoryFile) throws Exception {
|
|
|
|
|
|
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 删除文件或目录。如果要删除目录,目录必须为空。
|
|
|
|
|
ossClient.deleteObject(bucketName, "YTHFile" + directoryFile);
|
|
|
|
|
} catch (OSSException oe) {
|
|
|
|
|
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
|
|
+ "but was rejected with an error response for some reason.");
|
|
|
|
|
System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
|
|
System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
|
|
System.out.println("Request ID:" + oe.getRequestId());
|
|
|
|
|
System.out.println("Host ID:" + oe.getHostId());
|
|
|
|
|
} catch (Exception ce) {
|
|
|
|
|
System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
|
|
+ "a serious internal problem while trying to communicate with OSS, "
|
|
|
|
|
+ "such as not being able to access the network.");
|
|
|
|
|
System.out.println("Error Message:" + ce.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (ossClient != null) {
|
|
|
|
|
ossClient.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void createDir(String createpath, ChannelSftp sftp) {
|
|
|
|
|
try {
|
|
|
|
|
if (isDirExist(createpath, sftp)) {
|
|
|
|
|
sftp.cd(createpath);
|
|
|
|
|
}
|
|
|
|
|
String pathArry[] = createpath.split("/");
|
|
|
|
|
StringBuffer filePath = new StringBuffer("/");
|
|
|
|
|
for (String path : pathArry) {
|
|
|
|
|
if (path.equals("")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
filePath.append(path + "/");
|
|
|
|
|
if (isDirExist(filePath.toString(), sftp)) {
|
|
|
|
|
sftp.cd(filePath.toString());
|
|
|
|
|
} else {
|
|
|
|
|
// 建立目录
|
|
|
|
|
sftp.mkdir(filePath.toString());
|
|
|
|
|
// 进入并设置为当前目录
|
|
|
|
|
sftp.cd(filePath.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sftp.cd(createpath);
|
|
|
|
|
} catch (SftpException e) {
|
|
|
|
|
// throw new SystemException("创建路径错误:" + createpath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean isDirExist(String directory, ChannelSftp sftp) {
|
|
|
|
|
boolean isDirExistFlag = false;
|
|
|
|
|
try {
|
|
|
|
|
SftpATTRS sftpATTRS = sftp.lstat(directory);
|
|
|
|
|
isDirExistFlag = true;
|
|
|
|
|
return sftpATTRS.isDir();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (e.getMessage().toLowerCase().equals("no such file")) {
|
|
|
|
|
isDirExistFlag = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return isDirExistFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String saveFile(MultipartFile multipartFile) throws Exception {
|
|
|
|
|
return this.saveFile(multipartFile, Const.FILEPATHFILE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String saveFile(MultipartFile file, String cost) throws Exception {
|
2024-03-30 14:55:41 +08:00
|
|
|
|
return this.saveFile(file, cost, Const.FILEPATHFILE);
|
2024-01-22 11:29:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 16:01:15 +08:00
|
|
|
|
public String saveFile(MultipartFile file, String modular, String path) throws Exception {
|
|
|
|
|
String fileName = Warden.get32UUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
|
|
byte[] bytes = file.getBytes();
|
|
|
|
|
String ffile = DateUtil.getDays();
|
|
|
|
|
if (StringUtils.isNotBlank(modular)) {
|
|
|
|
|
this.sshSftp(file, fileName, path + modular + "/" + ffile);
|
|
|
|
|
return path + modular + "/" + ffile + "/" + fileName;
|
|
|
|
|
} else {
|
|
|
|
|
this.sshSftp(file, fileName, path + ffile);
|
|
|
|
|
return path + ffile + "/" + fileName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除文件
|
|
|
|
|
*
|
|
|
|
|
* @throws SftpException
|
|
|
|
|
* @throws JSchException
|
|
|
|
|
*/
|
|
|
|
|
public void deleteFile(String directoryFile) throws Exception {
|
|
|
|
|
|
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 删除文件或目录。如果要删除目录,目录必须为空。
|
|
|
|
|
ossClient.deleteObject(bucketName, "YTHFile" + directoryFile);
|
|
|
|
|
} catch (OSSException oe) {
|
|
|
|
|
PageData pd = new PageData();
|
|
|
|
|
pd.put("FILE_PATH",directoryFile);
|
|
|
|
|
fileUploadLogService.delete(pd);
|
|
|
|
|
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
|
|
+ "but was rejected with an error response for some reason.");
|
|
|
|
|
System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
|
|
System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
|
|
System.out.println("Request ID:" + oe.getRequestId());
|
|
|
|
|
System.out.println("Host ID:" + oe.getHostId());
|
|
|
|
|
} catch (ClientException ce) {
|
|
|
|
|
System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
|
|
+ "a serious internal problem while trying to communicate with OSS, "
|
|
|
|
|
+ "such as not being able to access the network.");
|
|
|
|
|
System.out.println("Error Message:" + ce.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (ossClient != null) {
|
|
|
|
|
ossClient.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-01-22 11:29:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|