StreamUtil

[b][color=red]开发者博客:[url]http://www.developsearch.com[/url][/color][/b]

/**
* 流操作
*
* @author chenxin
* @version [版本号, 2012-5-21]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class StreamUtil {

// 从文件读取数据
public static byte[] getContentFromFile(String filePath) throws IOException
{
FileInputStream fin = new FileInputStream(filePath);
ByteArrayOutputStream os = new ByteArrayOutputStream();
int n = -1;
byte[] buf = new byte[1024];
while ((n = fin.read(buf)) != -1)
{
os.write(buf, 0, n);
}
fin.close();
return os.toByteArray();
}

// 写数据到文件
public static void writeContentToFile(String filePath, byte[] content) throws IOException
{
FileOutputStream fout = new FileOutputStream(filePath, false);

fout.write(content);

fout.close();

}

// 从输入流读取数据
public static byte[] getContentFromInputStream(InputStream in) throws IOException
{
return getContentFromInputStream(in, Integer.MAX_VALUE);
}

// 从输入流读取数据
public static byte[] getContentFromInputStream(InputStream in, int length) throws IOException
{
ByteArrayOutputStream os = new ByteArrayOutputStream();

byte[] buf = new byte[1024];
while (os.size() < length)
{
int n = in.read(buf);
if (n > 0)
{
os.write(buf, 0, n);
}
else if (n < 0)
{
break;
}
}
in.close();
return os.toByteArray();
}

// 从输入流读取数据
public static String getContentStrFromInputStream(InputStream in) throws IOException
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int iCnt = 0;
while ((iCnt = in.read(buf)) > 0)
{
os.write(buf, 0, iCnt);
}
in.close();
return os.toString("UTF-8");
}

public static void main(String[] args)
{
}

/**
* 将输入流的内容全部输出到输出流
*
* @param is 输入流
* @param os 输出流
* @throws IOException
*/
public static void copy(InputStream is, OutputStream os) throws IOException
{
synchronized (is)
{
synchronized (os)
{
byte[] buffer = new byte[256];
while (true)
{
int bytesRead = is.read(buffer);
if (bytesRead == -1)
{
break;
}
os.write(buffer, 0, bytesRead);
}
}
}
}


}
2025-08-04 15:58:53.163 DEBUG [tech-base-common]-[dev] --- [http-nio-30002-exec-3]-[,] [c.s.t.m.DFSResourceMapper.selectList ,debug,135] : ==> Preparing: SELECT id,resource_id,app_name,endpoint,bucket_name,file_path,file_name,expire_hour,version_id,upload_time,status,error_message,network_type,create_time,update_time,create_by,update_by,is_deleted FROM t_tbc_resource WHERE (resource_id = ? AND is_deleted = ?) 2025-08-04 15:58:53.163 DEBUG [tech-base-common]-[dev] --- [http-nio-30002-exec-3]-[,] [c.s.t.m.DFSResourceMapper.selectList ,debug,135] : ==> Parameters: 7cuOnqUsUzZffxBaYD5EKi(String), 0(Integer) 2025-08-04 15:58:53.167 DEBUG [tech-base-common]-[dev] --- [http-nio-30002-exec-3]-[,] [c.s.t.m.DFSResourceMapper.selectList ,debug,135] : <== Total: 1 2025-08-04 15:58:53.172 ERROR [tech-base-common]-[dev] --- [http-nio-30002-exec-3]-[,] [c.s.t.c.advice.GlobalExceptionHandler ,handleErrorResponse,61] : GlobalExceptionHandler ErrorResponseException, io.minio.errors.ErrorResponseException: The specified key does not exist. at io.minio.S3Base$1.$sw$original$onResponse$vg3qou3(S3Base.java:747) at io.minio.S3Base$1.$sw$original$onResponse$vg3qou3$accessor$$sw$sb7edj2(S3Base.java) at io.minio.S3Base$1$$sw$auxiliary$pnhov63.call(Unknown Source) at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86) at io.minio.S3Base$1.onResponse(S3Base.java) at okhttp3.internal.connection.RealCall$AsyncCall.$sw$original$run$3opc2g1(RealCall.kt:519) at okhttp3.internal.connection.RealCall$AsyncCall.$sw$original$run$3opc2g1$accessor$$sw$gcmnir0(RealCall.kt) at okhttp3.internal.connection.RealCall$AsyncCall$$sw$auxiliary$4jo1t90.call(Unknown Source) at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:840) 这是报错信息 @GetMapping({"/tbc/dfs/rid/{resourceId}"}) public ResponseEntity<InputStreamResource> getResource(@PathVariable String resourceId) throws Exception { DFSResource dfsResource = this.dfsAppInfoService.getResourceById(resourceId); if (null == dfsResource) { log.error("请求resource接口:resourceId:[{}] 不存在", resourceId); throw new TBCBaseException(ResultCode.RESOURCE_ID_NOT_EXIST.getCode(), resourceId + "不存在"); } else { DateTime uploadTime = DateUtil.date(dfsResource.getUploadTime()); DateTime now = DateUtil.date(); DateTime expireTime = DateUtil.offsetHour(uploadTime, dfsResource.getExpireHour()); if (now.isAfter(expireTime)) { log.info("请求resource接口:resourceId:[{}] 已过期", resourceId); throw new TBCBaseException(ResultCode.RESOURCE_EXPIRED); } else { DFSAppInfo dfsAppInfo = this.dfsAppInfoService.getDFSAppInfo(dfsResource.getAppName(), dfsResource.getBucketName()); if (null == dfsAppInfo) { log.error("请求resource接口:app信息不存在:appName: [{}] bucketName:[{}] 已过期", dfsResource.getAppName(), dfsResource.getBucketName()); throw new AppRegisterException(ResultCode.MINIO_APP_NOT_REGISTER); } else { MinioClient minioClient = this.minioContainer.getMinioClient(dfsResource.getEndpoint(), dfsAppInfo); String objectName = String.format("%s/%s", dfsResource.getFilePath(), dfsResource.getFileName()); GetObjectArgs getObjectArgs = (GetObjectArgs)((GetObjectArgs.Builder)((GetObjectArgs.Builder)((GetObjectArgs.Builder)GetObjectArgs.builder().bucket(dfsResource.getBucketName())).object(objectName)).versionId(dfsResource.getVersionId())).build(); GetObjectResponse response = minioClient.getObject(getObjectArgs); return StreamUtil.processResponse(dfsResource.getFileName(), response); } } } }这是代码 分析下原因
08-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值