2013-12-18 59 views
1

我有一個輸入流連接到服務器上的文件。輸入流是使用Apache Web Components建立的。如何將輸入流提供給用戶的瀏覽器,以便文件可以使用Apache Web Components在瀏覽器中下載?提供帶有輸入流的文件下載瀏覽器

CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
    credsProvider.setCredentials(
      new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
      new UsernamePasswordCredentials("user", "pass")); 
    CloseableHttpClient httpclient = HttpClients.custom() 
      .setDefaultCredentialsProvider(credsProvider).build(); 
    try { 
     HttpGet httpget = new HttpGet("https://website.com/file.txt"); 

     CloseableHttpResponse response = httpclient.execute(httpget); 
     try { 
      HttpEntity entity = response.getEntity(); 
      InputStream in=entity.getContent(); 
      int c; 
      while((c=in.read())!=-1){ 
       //maybe write to an ouput stream here so file can download? 
       System.out.println(c); 
      } 

      EntityUtils.consume(entity); 
     } finally { 
      response.close(); 
     } 
    } finally { 
     httpclient.close(); 
    } 
+0

你使用什麼。 JSP/Servlet或更復雜的框架。請具體說明您的問題。 – Diversity

回答

2

又一個HTTP框架:

也許這可以幫助:

CloseableHttpClient httpclient = HttpClients.createDefault(); 
HttpGet httpget = new HttpGet("http://localhost/"); 
CloseableHttpResponse response = httpclient.execute(httpget); 
try { 
    HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
     InputStream instream = entity.getContent(); 
     try { 
      // do something useful 
     } finally { 
      instream.close(); 
     } 
    } 
} finally { 
    response.close(); 
} 

Quelle的:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e49

的HttpEntity INSTANZ爲您提供可與標準的Java流類進行評估的InputStream和方法。

可能是一個答案,如果不是,請提供代碼片段或具體的問題。

+0

當前的代碼已添加。我已經有一個輸入流,我需要將該流寫入瀏覽器,以便可以下載文件。 – Nick