2011-09-27 24 views
2

對不起的標題感到抱歉。使用查詢字符串從HTTP端點下載PNG文件

我有這樣一個URL: http://testserver:8080/dummy/[email protected]_1231Fv_C

當我使用瀏覽器訪問這個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)); 

非常感謝你的幫助。

+2

失敗如何顯示?你會得到一個異常堆棧跟蹤? – Barend

+0

沒有失敗或例外。 – thinkanotherone

回答

0

你在使用番石榴圖書館嗎? 然後值得注意的是,ByteStreams.copy將輸入流中的所有字節複製到輸出流。 不關閉或沖洗流

你應該沖洗&關閉你的FileOutputStream。

這將是更好,如果你分享更多的細節e.g異常等

0

我做這樣的,其工作

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