Java 使用查询字符串从HTTP端点下载PNG文件

Java 使用查询字符串从HTTP端点下载PNG文件,java,jersey,Java,Jersey,很抱歉标题不好 我有这样一个URL: 当我使用浏览器访问此URL时,会返回一个PNG图像。 但是,当我尝试使用Jersey客户端API获取它时,我无法下载它。(我也试过java.nio) 请参阅我的代码片段 Client client = Client.create(); client.setFollowRedirects(true); WebResource r = client.resource(url); InputStream in = r.get (InputStream.class

很抱歉标题不好

我有这样一个URL:

当我使用浏览器访问此URL时,会返回一个PNG图像。 但是,当我尝试使用Jersey客户端API获取它时,我无法下载它。(我也试过java.nio)

请参阅我的代码片段

Client client = Client.create();
client.setFollowRedirects(true);
WebResource r = client.resource(url);
InputStream in = r.get (InputStream.class);
ByteStreams.copy(in, new FileOutputStream(savedFile));

非常感谢你的帮助

你在用番石榴图书馆吗? 然后值得注意的是,
ByteStreams.copy
将所有字节从输入流复制到输出流不会关闭或刷新任一流

您应该刷新并关闭FileOutputStream


如果您共享更多详细信息(例如例外情况等)会更好。

@
保留字符,也许您应该对它们进行编码(将其更改为
%40


我正在这样做,而且很有效

import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/image")
public class ImageService {

    private static final String FILE_PATH = "c:\\az-fu.png";

    @GET
    @Path("/get")
    @Produces("image/png")
    public Response getFile() {

        File file = new File(FILE_PATH);

        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition",
            "attachment; filename=image_from_server.png");
        return response.build();

    }

}
从web浏览器点击时出现的对话框:


故障是如何显示的?是否有异常堆栈跟踪?没有故障或异常。