gpt4 book ai didi

java - 如何向实现另一个方法的方法提供参数

转载 作者:行者123 更新时间:2023-12-01 10:13:40 24 4
gpt4 key购买 nike

我使用了 Jersey 服务器,我希望端点根据参数重定向到文件的下载。

我在使用以下功能时遇到困难:

@GET
@Path("/get/{id}/{chunk}")
public Response getDescription(@PathParam("id") String id, @PathParam("chunk") String chunk) {
{

StreamingOutput fileStream = new StreamingOutput()
{
@Override
public void write(java.io.OutputStream output, String id) throws IOException, WebApplicationException
{
try
{
if (Objects.equals(chunk, new String("init"))) {
java.nio.file.Path path = Paths.get("src/main/uploads/example/frame_init.pdf");
}
else {
java.nio.file.Path path = Paths.get("src/main/uploads/example/"+ id +".pdf");
}
byte[] data = Files.readAllBytes(path);
output.write(data);
output.flush();
}
catch (Exception e)
{
throw new WebApplicationException("File Not Found !!");
}
}
};
return Response
.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = myfile.pdf")
.build();
}

我在向函数 write 传递参数时遇到问题。我在端点处有参数 id 和 chunk,但我无法在 write 方法中使用它,因为它实现了 StreamingOutput()。

我该如何处理?谢谢

最佳答案

对于java,final关键字应该可以解决你的问题。

作为更新的代码;

@GET
@Path("/get/{id}/{chunk}")
public Response getDescription(@PathParam("id") final String id, @PathParam("chunk") final String chunk) {
{

StreamingOutput fileStream = new StreamingOutput()
{
@Override
public void write(java.io.OutputStream output, String id2) throws IOException, WebApplicationException
{
try
{
if (Objects.equals(chunk, new String("init"))) {
java.nio.file.Path path = Paths.get("src/main/uploads/example/frame_init.pdf");
}
else {
java.nio.file.Path path = Paths.get("src/main/uploads/example/"+ id2 +".pdf");
}
byte[] data = Files.readAllBytes(path);
output.write(data);
output.flush();
}
catch (Exception e)
{
throw new WebApplicationException("File Not Found !!");
}
}
};
return Response
.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = myfile.pdf")
.build();
}

关于java - 如何向实现另一个方法的方法提供参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36022750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com