我想知道当我从 Minio 接收输入流时是否需要缓冲区。
我使用 Minio 作为对象存储,并使用 Dropwizard 作为客户端和 Minio 之间的后端。现在,当我使用 minio 中的 getObject 方法时,我会得到一个 inputStream。
public InputStream getObject(String bucketName, String objectName, long offset)
在我看来,它会是这样的
@Path("/file")
public class FileResource {
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile() throws Exception {
InputStream is = minioClient.getObject("mybucket", "myobject");
return Response.ok(is)
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"file.txt\"")
.build();
}
}
据我了解,可以仅将此输入流作为响应返回给客户端,并进行必要的内容配置。
现在有必要使用 bufferedInputStream 吗?而GET请求要等待多长时间才会超时?
最佳答案
我无法访问 Minio。但是使用简单的本地文件,您的方法效果很好。
import com.google.common.net.HttpHeaders;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.file.Paths;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/file")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public class FileResource {
@GET
public Response getFile() {
try {
InputStream is = new FileInputStream(Paths.get("/tmp/foo.txt").toFile());
return Response.ok(is)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"file.txt\"")
.build();
} catch (FileNotFoundException ex) {
throw new InternalServerErrorException(ex.getMessage());
}
}
}
包含一些文本的简单文件 /tmp/foo.txt
将通过正确的 HTTP 响应返回。使用curl
:
$ curl -v http://localhost:8080/file
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /file HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Wed, 11 Apr 2018 14:15:06 GMT
< Content-Disposition: attachment; filename="file.txt"
< Content-Type: application/octet-stream
< Vary: Accept-Encoding
< Content-Length: 12
<
foo
bar
baz
关于java - 从Minio获取文件时是否需要使用bufferInputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49776003/