java - 从远程文件读取字符串 - Java

标签 java string download

我想从我的服务器读取我的程序的待办事项列表。 我尝试过使用 BufferedReaderInputStream,但还没有完全掌握它的窍门。

网址是:http://team-m4gkbeatz.eu/Beatsleigher/UniversalAndroidToolkit/UAT.todo

非常感谢任何帮助。

最佳答案

这是我创建的一个函数,它使用从url.openStream()获得的InputStream。它将页面作为字符串返回。您可以稍后处理该页面。

public String getpage(URL url)
    {
        try {
            // try opening the URL
            URLConnection urlConnection = url.openConnection();                        
            urlConnection.setAllowUserInteraction(false);

            InputStream urlStream = url.openStream();            
            byte buffer[] = new byte[1000];
            int numRead = urlStream.read(buffer);
            String content = new String(buffer, 0, numRead);

            while ((numRead != -1) && (content.length() < MAX_PAGE_SIZE)) {
                numRead = urlStream.read(buffer);
                if (numRead != -1) {
                    String newContent = new String(buffer, 0, numRead);
                    content += newContent;
                }
            }
            return content;
        } catch (IOException e) {            
            e.printTrackStace();
        }catch(IndexOutOfBoundsException e1){            
            e1.printTrackStace();
        }
    }

使用以下方式调用此函数:

getpage(new URL("http://team-m4gkbeatz.eu/Beatsleigher/UniversalAndroidToolkit/UAT.todo"));

关于java - 从远程文件读取字符串 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20313934/

相关文章:

video - 在 C++ 中下载 youtube 视频

Angular Spring Boot 文件下载

java - 为互斥请求参数设计 API 的更好方法是什么?

java - 安卓错误 : Could not read input channel file descriptors from parcel

python - 将字符串拆分为多个部分(使用正则表达式?)

c# - 我应该在 C# 中将 XML 生成为字符串吗?

java - Angular 2 : download file from Rest webservice

java - 简单日期格式间歇性地返回错误日期

java - 如何使用共享首选项将媒体静音

Javascript document.getElementById ("id").value 在元素为空文本框时返回 null 而不是空字符串