HttpNormalImpl

Gilberto

PAGINA INICIAL DO PROJETO
package com.agenda.programa;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpNormalImpl extends Http {

public final String downloadArquivo(String url){

    try{

        URL u = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)u.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.connect();

        InputStream in = conn.getInputStream();

        String arquivo = readString(in);
        conn.disconnect();
        return arquivo;
    }catch(Exception e){
        return "erro: "+e;
    }   
}

private String readString(InputStream in) throws IOException {
    byte[] bytes = readBytes(in);
    String texto = new String(bytes);
    return texto;
}

private byte[] readBytes(InputStream in) throws IOException  {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try{
        byte[] buffer = new byte[1024];
        int len;
        while((len = in.read(buffer))>0){
            bos.write(buffer,0,len);
        }
        byte[] bytes = bos.toByteArray();
        return bytes;
    }finally{
        bos.close();
        in.close();
    }
}

}


MongoDB Logo MongoDB