JAVA:获得网页下载文件内容

 

//urlString为文件的下载路径,如http://abc.com/asd.mp3

 

public static String getBuffer(String urlString) throws Exception {
        StringBuffer sb = new StringBuffer();  //存放下载的文件序列 
        String line = null; 
        BufferedReader buffer = null; 
         
               // 创建一个URL对象 
        URL url = new URL(urlString); 
               // 创建一个Http连接 
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
        // 使用IO流读取数据 
        buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 
            while ((line = buffer.readLine()) != null) { 
                    sb.append(line); 
                    sb.append("\n");
                    } 
        buffer.close(); 
        return sb.toString();
    }

 

 

 

你可能感兴趣的:(JAVA:获得网页下载文件内容)