| java110-core/src/main/java/com/java110/core/client/CosUploadTemplate.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| java110-utils/pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| java110-utils/src/main/java/com/java110/utils/util/COSUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| java110-utils/src/main/java/com/java110/utils/util/OSSUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| service-common/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
java110-core/src/main/java/com/java110/core/client/CosUploadTemplate.java
New file @@ -0,0 +1,244 @@ package com.java110.core.client; import com.java110.utils.util.Base64Convert; import com.java110.utils.util.COSUtil; import com.java110.utils.util.DateUtil; import com.qcloud.cos.COSClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.UUID; @Component public class CosUploadTemplate { private static Logger logger = LoggerFactory.getLogger(CosUploadTemplate.class); /* * private static String server = "www.datasvisser.cn"; //地址 private static * int port = 41023;//端口号 private static String userName = "jntechFTP1";//登录名 * private static String userPassword ="MXUsssMjhssE+*=a3C4\\0";//密码 */ private static String FIX_PATH = "uploadFiles"; // 文件上传目录 private static String LOCAL_CHARSET = "GBK"; private static String SERVER_CHARSET = "ISO-8859-1"; private final static String localpath = "F:/";//下载到F盘下 private final static String fileSeparator = System.getProperty("file.separator"); private final static String DEFAULT_IMG_SUFFIX = ".jpg"; private final static String IMAGE_DEFAULT_PATH = "img/"; /* *图片上传工具方法 * 默认上传至 img 文件下的当前日期下 */ public String upload(String imageBase64, String server, int port, String userName, String userPassword, String ftpPath) { String fileName = ""; COSClient cosClient = null; ByteArrayInputStream is = null; String urlPath = ""; try { cosClient = COSUtil.getCOSClient(); fileName = UUID.randomUUID().toString(); if (imageBase64.contains("data:image/png;base64,")) { imageBase64 = imageBase64.replace("data:image/png;base64,", ""); fileName += ".png"; } else if (imageBase64.contains("data:image/jpeg;base64,")) { imageBase64 = imageBase64.replace("data:image/jpeg;base64,", ""); fileName += ".jpg"; } else if (imageBase64.contains("data:image/webp;base64,")) { imageBase64 = imageBase64.replace("data:image/webp;base64,", ""); fileName += ".jpg"; } else if (imageBase64.contains("data:application/octet-stream;base64,")) { imageBase64 = imageBase64.replace("data:application/octet-stream;base64,", ""); fileName += ".jpg"; } else { fileName += ".jpg"; } urlPath = IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/" + fileName; byte[] context = Base64Convert.base64ToByte(imageBase64); is = new ByteArrayInputStream(context); COSUtil.uploadByInputStream(cosClient, is, ftpPath +urlPath); } catch (Exception e) { logger.error("上传文件失败", e); throw new IllegalArgumentException("上传文件失败"); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return urlPath; } /* *文件上传工具方法 */ public String upload(MultipartFile uploadFile, String server, int port, String userName, String userPassword, String ftpPath) { String fileName = ""; COSClient cosClient = null; InputStream is = null; try { cosClient = COSUtil.getCOSClient(); fileName = UUID.randomUUID().toString() + "." + uploadFile.getOriginalFilename().substring(uploadFile.getOriginalFilename().lastIndexOf(".") + 1); fileName = IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/" + fileName; is = uploadFile.getInputStream(); COSUtil.uploadByInputStream(cosClient, is, ftpPath + fileName); } catch (Exception e) { // logger.error("上传文件失败", e); throw new IllegalArgumentException("上传文件失败"); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return fileName; } /* *文件下载工具方法 */ public byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) { byte[] return_arraybyte = null; COSClient ossClient = null; ByteArrayOutputStream byteOut = null; InputStream ins = null; ByteArrayInputStream fis = null; try { ossClient = COSUtil.getCOSClient(); COSUtil.getInputStreamByCOS(ossClient, remotePath + fileName); byteOut = new ByteArrayOutputStream(); byte[] buf = new byte[2048]; int bufsize = 0; while (ins != null && (bufsize = ins.read(buf, 0, buf.length)) != -1) { byteOut.write(buf, 0, bufsize); } fis = new ByteArrayInputStream(byteOut.toByteArray()); byteOut.flush(); byteOut.close(); byte[] buffer = new byte[fis.available()]; int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } if (offset != buffer.length) { throw new IOException("Could not completely read file "); } return_arraybyte = buffer; } catch (Exception e) { e.printStackTrace(); logger.error("从ftp读取文件失败", e); } finally { if (byteOut != null) { try { byteOut.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (ins != null) { try { ins.close(); } catch (IOException e) { e.printStackTrace(); } } } return return_arraybyte; } public static void main(String[] args) { CosUploadTemplate ftpUploadTemplate = new CosUploadTemplate(); String img = ftpUploadTemplate.download("/hc/img/20220322/", "b3d668c2-96fd-4722-82dd-431dc08941a2.png", "118.89.243.11", 617, "hcdemo", "45j74jpWTf7bNhnC"); System.out.printf("img=" + img); } public String download(String remotePath, String fileName, String server, int port, String userName, String userPassword) { COSClient ossClient = null; ByteArrayOutputStream bos = null; InputStream is = null; ByteArrayInputStream fis = null; try { ossClient = COSUtil.getCOSClient(); is = COSUtil.getInputStreamByCOS(ossClient, remotePath + fileName); if (null == is) { throw new FileNotFoundException(remotePath); } bos = new ByteArrayOutputStream(); int length; byte[] buf = new byte[2048]; while (-1 != (length = is.read(buf, 0, buf.length))) { bos.write(buf, 0, length); } fis = new ByteArrayInputStream( bos.toByteArray()); bos.flush(); byte[] buffer = new byte[fis.available()]; int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } if (offset != buffer.length) { throw new IOException("Could not completely read file "); } return Base64Convert.byteToBase64(buffer); } catch (Exception e) { logger.error("ftp通过文件名称获取远程文件流", e); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } } java110-utils/pom.xml
@@ -34,6 +34,16 @@ <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> </dependency> <dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> java110-utils/src/main/java/com/java110/utils/util/COSUtil.java
New file @@ -0,0 +1,334 @@ package com.java110.utils.util; import com.alibaba.fastjson.JSON; import com.java110.utils.cache.MappingCache; import com.qcloud.cos.COSClient; import com.qcloud.cos.ClientConfig; import com.qcloud.cos.auth.BasicCOSCredentials; import com.qcloud.cos.auth.COSCredentials; import com.qcloud.cos.exception.CosClientException; import com.qcloud.cos.exception.CosServiceException; import com.qcloud.cos.http.HttpProtocol; import com.qcloud.cos.model.*; import com.qcloud.cos.region.Region; import java.io.*; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class COSUtil { public static final String DOMAIN = "COS"; public static final String COS_SWITCH = "COS_SWITCH"; public static final String COS_SWITCH_COS = "COS"; public static final String REGION = "regionName"; public static final String ACCESS_KEY_ID = "accessKeyId"; public static final String ACCESS_KEY_SECRET = "accessKeySecret"; public static final String BUCKET_NAME = "bucketName"; /** * @return OSSClient oss客户端 * @throws * @Title: getOSSClient * @Description: 获取oss客户端 */ public static COSClient getCOSClient() { String regionName = MappingCache.getValue(DOMAIN, REGION); String secretId = MappingCache.getValue(DOMAIN, ACCESS_KEY_ID); String secretKey = MappingCache.getValue(DOMAIN, ACCESS_KEY_SECRET); return COSUtil.getCOSClient(regionName, secretId, secretKey); } private static COSClient getCOSClient(String regionName, String secretId, String secretKey) { // 1 初始化用户身份信息(secretId, secretKey)。 COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。 Region region = new Region(regionName); ClientConfig clientConfig = new ClientConfig(region); // 这里建议设置使用 https 协议 // 从 5.6.54 版本开始,默认使用了 https clientConfig.setHttpProtocol(HttpProtocol.https); // 3 生成 cos 客户端。 COSClient cosClient = new COSClient(cred, clientConfig); return cosClient; } /** * @param cosClient oss客户端 * @param url URL * @param bucketName bucket名称 * @param objectName 上传文件目录和(包括文件名)例如“test/index.html” * @return void 返回类型 * @throws * @Title: uploadByNetworkStream * @Description: 通过网络流上传文件 */ public static void uploadByNetworkStream(COSClient cosClient, URL url, String bucketName, String objectName) { try { InputStream inputStream = url.openStream(); objectName = url.getFile(); uploadByInputStream(cosClient, inputStream, objectName); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (cosClient != null) { cosClient.shutdown(); } } } /** * @param cosClient oss客户端 * @param inputStream 输入流 * @param objectName 上传文件目录和(包括文件名) 例如“test/a.jpg” * @return void 返回类型 * @throws * @Title: uploadByInputStream * @Description: 通过输入流上传文件 */ public static void uploadByInputStream(COSClient cosClient, InputStream inputStream, String objectName) throws Exception { String bucketName = MappingCache.getValue(DOMAIN, BUCKET_NAME); uploadByInputStream(cosClient, inputStream, bucketName, objectName); } /** * @param cosClient oss客户端 * @param inputStream 输入流 * @param bucketName bucket名称 * @param objectName 上传文件目录和(包括文件名) 例如“test/a.jpg” * @return void 返回类型 * @throws * @Title: uploadByInputStream * @Description: 通过输入流上传文件 */ public static void uploadByInputStream(COSClient cosClient, InputStream inputStream, String bucketName, String objectName) throws Exception { if (!objectName.startsWith("/")) { objectName = "/" + objectName; } try { ObjectMetadata objectMetadata = new ObjectMetadata(); // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据,存在内存OOM的情况 // objectMetadata.setContentLength(1024); PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream, objectMetadata); PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); //System.out.println(JSON.toJSONString(putObjectResult)); // putobjectResult会返回文件的etag String etag = putObjectResult.getETag(); if (etag == null || etag.length() == 0) { throw new Exception("文件上传失败:" + putObjectResult.getDateStr()); } } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (cosClient != null) { cosClient.shutdown(); } } } /** * @param cosClient oss客户端 * @param file 上传的文件 * @param bucketName bucket名称 * @param objectName 上传文件目录和(包括文件名) 例如“test/a.jpg” * @return void 返回类型 * @throws * @Title: uploadByFile * @Description: 通过file上传文件 */ public static void uploadByFile(COSClient cosClient, File file, String bucketName, String objectName) { try { cosClient.putObject(bucketName, objectName, file); } catch (Exception e) { e.printStackTrace(); } finally { if (cosClient != null) { cosClient.shutdown(); } } } /** * @param cosClient oss客户端 * @param bucketName bucket名称 * @param key 文件路径/名称,例如“test/a.txt” * @return void 返回类型 * @throws * @Title: deleteFile * @Description: 根据key删除oss服务器上的文件 */ public static void deleteFile(COSClient cosClient, String bucketName, String key) { cosClient.deleteObject(bucketName, key); } /** * @param cosClient oss客户端 * @param key 文件路径和名称 * @return InputStream 文件输入流 * @throws * @Title: getInputStreamByOSS * @Description:根据key获取服务器上的文件的输入流 */ public static InputStream getInputStreamByCOS(COSClient cosClient, String key) { String bucketName = MappingCache.getValue(DOMAIN, BUCKET_NAME); return getInputStreamByCOS(cosClient, bucketName, key); } /** * @param cosClient oss客户端 * @param bucketName bucket名称 * @param key 文件路径和名称 * @return InputStream 文件输入流 * @throws * @Title: getInputStreamByOSS * @Description:根据key获取服务器上的文件的输入流 */ public static InputStream getInputStreamByCOS(COSClient cosClient, String bucketName, String key) { InputStream content = null; try { // 方法1 获取下载输入流 GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key); COSObject cosObject = cosClient.getObject(getObjectRequest); content = cosObject.getObjectContent(); } catch (Exception e) { e.printStackTrace(); } return content; } /** * @param cosClient oss客户端 * @param bucketName bucket名称 * @return List<String> 文件路径和大小集合 * @throws * @Title: queryAllObject * @Description: 查询某个bucket里面的所有文件 */ public static List<String> queryAllObject(COSClient cosClient, String bucketName) { List<String> results = new ArrayList<String>(); try { ListObjectsRequest listObjectsRequest = new ListObjectsRequest(); // 设置bucket名称 listObjectsRequest.setBucketName(bucketName); // 设置最大遍历出多少个对象, 一次listobject最大支持1000 //listObjectsRequest.setMaxKeys(1000); ObjectListing objectListing = cosClient.listObjects(listObjectsRequest); for (COSObjectSummary objectSummary : objectListing.getObjectSummaries()) { results.add(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } } catch (CosServiceException e) { e.printStackTrace(); return results; } catch (CosClientException e) { e.printStackTrace(); return results; } finally { if (cosClient != null) { cosClient.shutdown(); } } return results; } public static void main(String[] args) { COSClient cosClient = null; ByteArrayOutputStream bos = null; InputStream is = null; ByteArrayInputStream fis = null; try { String regionName = "ap-guangzhou"; String secretId = "AKIDb08tQhA4AzXq1xtJiptgTjhsmfHRomlT"; String secretKey = "XzAH6fFaeSzGMErJYTYP7VdVBakfZPzJ"; String bucketName = "cyj-hc-1257195390"; cosClient = COSUtil.getCOSClient(regionName, secretId, secretKey); String remotePath = "hc/img/20220322/"; String fileName = "c2314f54-72d7-4bfd-ab4e-1eed3afd75d8.jpg"; is = COSUtil.getInputStreamByCOS(cosClient, bucketName,remotePath + fileName); if (null == is) { throw new FileNotFoundException(remotePath); } bos = new ByteArrayOutputStream(); int length; byte[] buf = new byte[2048]; while (-1 != (length = is.read(buf, 0, buf.length))) { bos.write(buf, 0, length); } fis = new ByteArrayInputStream( bos.toByteArray()); bos.flush(); byte[] buffer = new byte[fis.available()]; int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } if (offset != buffer.length) { throw new IOException("Could not completely read file "); } System.out.println(Base64Convert.byteToBase64(buffer)); } catch (Exception e) { System.out.println("ftp通过文件名称获取远程文件流" + e.getMessage()); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } } java110-utils/src/main/java/com/java110/utils/util/OSSUtil.java
@@ -18,6 +18,7 @@ public static final String DOMAIN = "OSS"; public static final String OSS_SWITCH = "OSS_SWITCH"; public static final String OSS_SWITCH_OSS = "OSS"; public static final String OSS_SWITCH_FTP = "FTP"; public static final String ENDPOINT = "endpoint"; public static final String ACCESS_KEY_ID = "accessKeyId"; public static final String ACCESS_KEY_SECRET = "accessKeySecret"; pom.xml
@@ -502,6 +502,19 @@ <artifactId>alipay-sdk-java</artifactId> <version>4.10.111.ALL</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> </dependency> <dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> <version>5.6.69</version> </dependency> </dependencies> </dependencyManagement> service-common/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java
@@ -3,12 +3,14 @@ import com.java110.common.dao.IFileServiceDao; import com.java110.config.properties.code.Java110Properties; import com.java110.core.base.smo.BaseServiceSMO; import com.java110.core.client.CosUploadTemplate; import com.java110.core.client.FtpUploadTemplate; import com.java110.core.client.JSchFtpUploadTemplate; import com.java110.core.client.OssUploadTemplate; import com.java110.dto.file.FileDto; import com.java110.intf.common.IFileInnerServiceSMO; import com.java110.utils.cache.MappingCache; import com.java110.utils.util.COSUtil; import com.java110.utils.util.OSSUtil; import com.java110.utils.util.StringUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -35,7 +37,8 @@ @Autowired private OssUploadTemplate ossUploadTemplate; @Autowired private CosUploadTemplate cosUploadTemplate; @Override public String saveFile(@RequestBody FileDto fileDto) { @@ -44,7 +47,15 @@ String fileName = ""; String ossSwitch = MappingCache.getValue(OSSUtil.DOMAIN, OSSUtil.OSS_SWITCH); if (StringUtil.isEmpty(ossSwitch) || !OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) { if ( OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) { fileName = ossUploadTemplate.upload(fileDto.getContext(), java110Properties.getFtpServer(), java110Properties.getFtpPort(), java110Properties.getFtpUserName(), java110Properties.getFtpUserPassword(), java110Properties.getFtpPath()); } else if (COSUtil.COS_SWITCH_COS.equals(ossSwitch)) { fileName = cosUploadTemplate.upload(fileDto.getContext(), java110Properties.getFtpServer(), java110Properties.getFtpPort(), java110Properties.getFtpUserName(), java110Properties.getFtpUserPassword(), java110Properties.getFtpPath()); } else { String ftpServer = MappingCache.getValue( FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_SERVER); int ftpPort = Integer.parseInt(MappingCache.getValue( FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_PORT)); String ftpUserName = MappingCache.getValue( FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERNAME); @@ -53,10 +64,6 @@ fileName = ftpUploadTemplate.upload(fileDto.getContext(), ftpServer, ftpPort, ftpUserName, ftpUserPassword, ftpPath); } else { fileName = ossUploadTemplate.upload(fileDto.getContext(), java110Properties.getFtpServer(), java110Properties.getFtpPort(), java110Properties.getFtpUserName(), java110Properties.getFtpUserPassword(), java110Properties.getFtpPath()); } return fileName; } @@ -74,7 +81,15 @@ } String context = ""; String ossSwitch = MappingCache.getValue(OSSUtil.DOMAIN, OSSUtil.OSS_SWITCH); if (StringUtil.isEmpty(ossSwitch) || !OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) { if (OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) { context = ossUploadTemplate.download(ftpPath, fileName, java110Properties.getFtpServer(), java110Properties.getFtpPort(), java110Properties.getFtpUserName(), java110Properties.getFtpUserPassword()); }else if (COSUtil.COS_SWITCH_COS.equals(ossSwitch)) { context = cosUploadTemplate.download(ftpPath, fileName, java110Properties.getFtpServer(), java110Properties.getFtpPort(), java110Properties.getFtpUserName(), java110Properties.getFtpUserPassword()); } else { String ftpServer = MappingCache.getValue( FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_SERVER); int ftpPort = Integer.parseInt(MappingCache.getValue( FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_PORT)); String ftpUserName = MappingCache.getValue( FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERNAME); @@ -82,10 +97,6 @@ context = ftpUploadTemplate.download(ftpPath, fileName, ftpServer, ftpPort, ftpUserName, ftpUserPassword); }else{ context = ossUploadTemplate.download(ftpPath, fileName, java110Properties.getFtpServer(), java110Properties.getFtpPort(), java110Properties.getFtpUserName(), java110Properties.getFtpUserPassword()); } fileDto.setContext(context);