HTTP를 통해 파일을 다운로드하고 해당 내용을 Java의 문자열에 저장하는 방법

StackOverflow https://stackoverflow.com/questions/1427508

문제

제목에서 알 수 있듯이 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();

스트림에서 읽으려고 하면 코드가 실패하고 스트림이 닫혔다고 보고됩니다.

이제 브라우저를 통해 파일에 액세스하려고 하면 텍스트로 제공되지 않고 다운로드할 파일로 제공됩니다.

나는 이것에 대해 웹을 검색한 적이 없으므로 약간의 통찰력을 주시면 감사하겠습니다!

감사해요.

도움이 되었습니까?

해결책

체크 아웃 httpclient Apache Commons, 특히 getResponseBodyAsString () 방법.

다른 팁

다음은 이를 수행하는 코드입니다.수행하려는 작업 외에도 GZip 압축도 처리할 수 있습니다(헤더에 다음과 같이 설정한 경우). Accept-Encoding: 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, 프레이머 프로젝트.

이 코드를 시도하면 테스트하지 않았기 때문에 컴파일되지 않을 수 있지만 가능한 모든 예외가 잡히지 않았지만 쉽게 추가 할 수 있습니다. Ressource를 사용할 수 없으면 프로그램이 나중에 언젠가 매달려 있으므로 무한 타임 아웃을 사용하지 마십시오. 간단한 텍스트 파일 검색보다 더 많은 일을하고 있다면 조사 할 수 있습니다. httpclient 아파치 커먼즈의.

    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());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top