如何通过HTTP下载文件并将其内容存储在Java的字符串中

发布于 2025-01-19 04:43:01 字数 504 浏览 0 评论 0原文

正如标题所说,我正在尝试通过HTTP下载文件并将其内容存储在字符串中。因此,我的方法是:

URL u = new URL("http://url/file.txt");

ByteArrayBuffer baf = new ByteArrayBuffer(32);
InputStream in = (InputStream) u.getContent(); 
BufferedInputStream bis = new BufferedInputStream(in);

int buffer;
while((buffer = bis.read()) != -1){
    baf.append((byte)buffer);
}

bis.close();
in.close();

当代码试图从流中读取时,该代码会失败,报告流关闭。

现在,如果您尝试通过浏览器访问文件,则不会用作文本,而是将其作为要下载的文件。

我还没有在此处搜索网络的任何地方,因此对此深表兴趣!

谢谢。

I'm trying to download a file over HTTP and store its contents in a String, as the title says. My approach is thus:

URL u = new URL("http://url/file.txt");

ByteArrayBuffer baf = new ByteArrayBuffer(32);
InputStream in = (InputStream) u.getContent(); 
BufferedInputStream bis = new BufferedInputStream(in);

int buffer;
while((buffer = bis.read()) != -1){
    baf.append((byte)buffer);
}

bis.close();
in.close();

The code fails when it tries to read from the stream, reporting the stream is closed.

Now if you try to access the file through a browser, it won't be served as text, rather as a file to be downloaded.

I haven't gotten anywhere searching the web on this, so a little insight would be much appreciated!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静待花开 2025-01-26 04:43:01

这是为您提供的代码。除了尝试执行的操作外,它还能够处理GZIP压缩(如果您在标题中使用接受Accept-编码:GZIP,DEFLATE),并且会自动检测为您的编码(处理字符串所需)。

private InputStream prepareInputStream(String urlToRetrieve) throws IOException
{
    URL url = new URL(urlToRetrieve);
    URLConnection uc = url.openConnection();
    if (timeOut > 0)
    {
        uc.setConnectTimeout(timeOut);
        uc.setReadTimeout(timeOut);
    }
    InputStream is = uc.getInputStream();
    // deflate, if necesarily
    if ("gzip".equals(uc.getContentEncoding()))
        is = new GZIPInputStream(is);

    this.lastURLConnection = uc;
    return is;
}
// detects encoding associated to the current URL connection, taking into account the default encoding
public String detectEncoding()
{
    if (forceDefaultEncoding)
        return defaultEncoding;
    String detectedEncoding = detectEncodingFromContentTypeHTTPHeader(lastURLConnection.getContentType());
    if (detectedEncoding == null)
        return defaultEncoding;

    return detectedEncoding;
}


public static String detectEncodingFromContentTypeHTTPHeader(String contentType)
{
    if (contentType != null)
    {
        int chsIndex = contentType.indexOf("charset=");
        if (chsIndex != -1)
        {
            String enc = StringTools.substringAfter(contentType , "charset=");
            if(enc.indexOf(';') != -1)
                enc = StringTools.substringBefore(enc , ";");
            return enc.trim();
        }
    }
    return null;
}


// retrieves into an String object
public String retrieve(String urlToRetrieve)
throws MalformedURLException , IOException
{
    InputStream is = prepareInputStream(urlToRetrieve);
    String encoding = detectEncoding();
    BufferedReader in = new BufferedReader(new InputStreamReader(is , encoding));
    StringBuilder output = new StringBuilder(BUFFER_LEN_STRING);
    String str;
    boolean first = true;
    while ((str = in.readLine()) != null)
    {
        if (!first)
            output.append("\n");
        first = false;
        output.append(str);
    }
    in.close();
    return output.toString();
}

该代码来自info.olteanu.utils.retrieve.retrievepage

Here's a piece of code that does that for you. In addition to what you're attempting to do, it is also able to handle GZip compression (if you set it in the headers with Accept-Encoding: gzip, deflate) and automatically detects encoding for you (required for handling strings).

private InputStream prepareInputStream(String urlToRetrieve) throws IOException
{
    URL url = new URL(urlToRetrieve);
    URLConnection uc = url.openConnection();
    if (timeOut > 0)
    {
        uc.setConnectTimeout(timeOut);
        uc.setReadTimeout(timeOut);
    }
    InputStream is = uc.getInputStream();
    // deflate, if necesarily
    if ("gzip".equals(uc.getContentEncoding()))
        is = new GZIPInputStream(is);

    this.lastURLConnection = uc;
    return is;
}
// detects encoding associated to the current URL connection, taking into account the default encoding
public String detectEncoding()
{
    if (forceDefaultEncoding)
        return defaultEncoding;
    String detectedEncoding = detectEncodingFromContentTypeHTTPHeader(lastURLConnection.getContentType());
    if (detectedEncoding == null)
        return defaultEncoding;

    return detectedEncoding;
}


public static String detectEncodingFromContentTypeHTTPHeader(String contentType)
{
    if (contentType != null)
    {
        int chsIndex = contentType.indexOf("charset=");
        if (chsIndex != -1)
        {
            String enc = StringTools.substringAfter(contentType , "charset=");
            if(enc.indexOf(';') != -1)
                enc = StringTools.substringBefore(enc , ";");
            return enc.trim();
        }
    }
    return null;
}


// retrieves into an String object
public String retrieve(String urlToRetrieve)
throws MalformedURLException , IOException
{
    InputStream is = prepareInputStream(urlToRetrieve);
    String encoding = detectEncoding();
    BufferedReader in = new BufferedReader(new InputStreamReader(is , encoding));
    StringBuilder output = new StringBuilder(BUFFER_LEN_STRING);
    String str;
    boolean first = true;
    while ((str = in.readLine()) != null)
    {
        if (!first)
            output.append("\n");
        first = false;
        output.append(str);
    }
    in.close();
    return output.toString();
}

The code is from info.olteanu.utils.retrieve.RetrievePage, Phramer project.

扮仙女 2025-01-26 04:43:01

尝试此代码,它可能不会编译,因为我没有测试过它,但是在没有捕获所有可能的例外的情况下它应该可以使用,但是您可以轻松添加它。请注意超时,切勿使用无限的超时,因为如果不可用的话,您的程序将来会挂起。如果您做的不仅仅是简单的文本文件检索,则可以查看 apache commons。

    URL url = new URL("http://mydomain.com/file.txt");
    URLConnection urlConnection = url.openConnection();
    urlConnection.setConnectTimeout(1000);
    urlConnection.setReadTimeout(1000);
    BufferedReader breader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

    StringBuilder stringBuilder = new StringBuilder();

    String line;
    while((line = breader.readLine()) != null) {
        stringBuilder.append(line);
    }

    System.out.println(stringBuilder.toString());

Try this code, it might not compile since i've not tested it but it should work beside that all possible Exceptions are not caught, but you can add this easily. Note the timeouts, NEVER use infinite timeouts since your program will hang sometime in the future if the ressource is not available. If you're doing more than a simple text file retrievement you could have a look into HTTPClient of the Apache Commons.

    URL url = new URL("http://mydomain.com/file.txt");
    URLConnection urlConnection = url.openConnection();
    urlConnection.setConnectTimeout(1000);
    urlConnection.setReadTimeout(1000);
    BufferedReader breader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

    StringBuilder stringBuilder = new StringBuilder();

    String line;
    while((line = breader.readLine()) != null) {
        stringBuilder.append(line);
    }

    System.out.println(stringBuilder.toString());
七色彩虹 2025-01-26 04:43:01

查看 httpclient 来自apache commons,特别是 norefollow noreferrer“

Check out HttpClient from Apache Commons, in particular the getResponseBodyAsString() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文