我可以使用什么来从URL检索CSV文件并使其成为InputStream?

时间:2013-08-06 19:53:49

标签: java spring http spring-mvc

我正在开发一个Spring Web MVC项目,我需要点击一个URL并从中检索CSV。

当我手动点击URL时,它会提示我将CSV下载到文件中。我被告知这只是因为浏览器。

我也被建议使用Apache HTTP Components ...

所以我现在就拥有这个

            DefaultHttpClient client = new DefaultHttpClient();
            client.getCredentialsProvider().setCredentials( 
                new AuthScope(URL, PORTNUM),
                new UsernamePasswordCredentials(username, password));
            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();
            authCache.put( new HttpHost( URL, PORTNUM), basicAuth);
            BasicHttpContext localContext = new BasicHttpContext();
            localContext.setAttribute( ClientContext.AUTH_CACHE, authCache);

            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();

我也一直在尝试关注我公司针对该页面进行身份验证的另一个项目,但它略有不同。 这不是我需要的结果(当然......),因为它不完整。

我需要将响应返回到InputStream ...

也许我对此完全错了。

3 个答案:

答案 0 :(得分:1)

HttpEntity提供了返回getContent()的方法InputStream

您可以使用

InputStream is = entity.getContent()

并从那里继续。

答案 1 :(得分:1)

ByteArrayInputStream bais = new ByteArrayInputStream(EntityUtils.toByteArray(entity));

使用EntityUtils,您可以将实体转换为字节数组,然后创建继承ByteArrayInputStream的{​​{1}}。

答案 2 :(得分:1)

你能不能只添加

InputStream inputStream = entity.getContent();

你有什么?