孔纲
2025-06-06 1dc9cd9cb0d9a5fa04885f6683d421112bcc2716
java110-core/src/main/java/com/java110/core/client/CosUploadTemplate.java
@@ -65,7 +65,7 @@
            byte[] context = Base64Convert.base64ToByte(imageBase64);
            is = new ByteArrayInputStream(context);
            COSUtil.uploadByInputStream(cosClient, is,  ftpPath +urlPath);
            COSUtil.uploadByInputStream(cosClient, is, ftpPath + urlPath);
        } catch (Exception e) {
            logger.error("上传文件失败", e);
            throw new IllegalArgumentException("上传文件失败");
@@ -112,6 +112,23 @@
    }
    /*
     *文件上传工具方法
     */
    public String upload(InputStream inputStream, String ftpPath) {
        COSClient cosClient = null;
        try {
            cosClient = COSUtil.getCOSClient();
            COSUtil.uploadByInputStream(cosClient, inputStream, ftpPath);
        } catch (Exception e) {
            // logger.error("上传文件失败", e);
            throw new IllegalArgumentException("上传文件失败");
        } finally {
        }
        return ftpPath;
    }
    /*
     *文件下载工具方法
     */
    public byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
@@ -122,7 +139,11 @@
        ByteArrayInputStream fis = null;
        try {
            ossClient = COSUtil.getCOSClient();
            COSUtil.getInputStreamByCOS(ossClient, remotePath + fileName);
            ins = COSUtil.getInputStreamByCOS(ossClient, remotePath + fileName);
            if (ins == null) {
                // 处理流为null的异常情况(如抛自定义异常、打日志等)
                throw new RuntimeException("Failed to get input stream from COS");
            }
            byteOut = new ByteArrayOutputStream();
            byte[] buf = new byte[2048];
            int bufsize = 0;
@@ -241,4 +262,20 @@
        return null;
    }
    public InputStream download(String fileName) {
        COSClient ossClient = null;
        InputStream is = null;
        try {
            ossClient = COSUtil.getCOSClient();
            is = COSUtil.getInputStreamByCOS(ossClient, fileName);
            if (null == is) {
                throw new FileNotFoundException(fileName);
            }
        } catch (Exception e) {
            logger.error("ftp通过文件名称获取远程文件流", e);
        }
        return is;
    }
}