Sorry for the poor title.
I have a URL like this: http://testserver:8080/dummy/server?@_1231Fv_C
When I access this URL using browser, this returns me a PNG ima开发者_运维技巧ge. But, when I try to fetch it using Jersey client API, I can't download it. (I tried java.nio as well)
See my code snippet
Client client = Client.create();
client.setFollowRedirects(true);
WebResource r = client.resource(url);
InputStream in = r.get (InputStream.class);
ByteStreams.copy(in, new FileOutputStream(savedFile));
Thanks a lot for your help.
@
characters are reserved, maybe you should encode them (change it to %40
).
- URL Encoding
- RFC1738
Are you using Guava library?
Then it's worth to note that ByteStreams.copy
Copies all bytes from the input stream to the output stream. Does not close or flush either stream.
You Should flush & close your FileOutputStream.
It will be better if you share more details e.g Exceptions etc.
I am doing like this and its working
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();
}
}
Dialog when I hit from web browser:
精彩评论