Java SE单线程下载网络上的音/视频等大文件

import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;


public class InternetTest {

    @Test
    public void test() {
        fail("Not yet implemented");
    }
    @Test
    public void getImage() throws Exception{
        //首先要获取请求的路径,路径就是要从网上获取的资源
        String urlPath= "http://c.hiphotos.baidu.com/image/pic/item/8718367adab44aedbc2df1e3b01c8701a18bfb52.jpg";
        URL url = new URL(urlPath);//建立URL类对象,抛出异常
        HttpURLConnection conn =(HttpURLConnection)url.openConnection();//得到UrlConnection对象

        conn.setRequestMethod("GET");//声明请求方式
        conn.setConnectTimeout(6*1000);//设置连接超时
        if(conn.getResponseCode() == 200){//如果结果是200,就代表他的请求是成功的
            InputStream inputStream = conn.getInputStream();//得到服务器端传回来的数据,相对客户端为输入流

            byte[] data = readInstream(inputStream);
            File file = new File("touxiang.jpg");
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(data);
            outputStream.close();       
        }       
    }

    private byte[] readInstream(InputStream inputStream) throws Exception{
        ByteArrayOutputStream byteArrayOutPutStream = new ByteArrayOutputStream();//创建ByteArrayOutputStream类
        byte[] buffer = new byte[1024];//声明缓存区
        int length = -1;//定义读取的默认长度
        while((length = inputStream.read(buffer))!= -1){
            byteArrayOutPutStream.write(buffer,0,length);//把缓存区中的输入到内存中         
        };
        byteArrayOutPutStream.close();//关闭输入流
        inputStream.close();//关闭输入流

        return byteArrayOutPutStream.toByteArray();//返回这个输入流的字节数组
    }

}

J2SE从网络中获取文本

    @Test
    public void getHtml() throws Exception{
        String urlpathString = "http://www.sina.com";
        URL url = new URL(urlpathString);//建立URL对象,抛出异常
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(6*1000);
        if(conn.getResponseCode() == 200){
            InputStream inputStream = conn.getInputStream();//得到输入流
            byte[] data = readInstream(inputStream);     //得到数据
            System.out.println(new String(data));//将字节转化为字符串,打印在控制台上面
        }
    }

System.out.println(new String(data)); 是将得到的字符串类型打印到控制台上面,返回是HTML的代码,和流量器参看的源代码是一样的。

第三种:J2SE从网络获取可以自行的文件。
在Internet类中添加getFile()方法,具体代码如下:

@Test
    public void getFile() throws Exception{
        String urlPath  = "http://dlsw.baidu.com/sw-search-sp/soft/b8/14630/YoudaoDict_V6.3.66.1117_setup.1423550350.exe";
        URL url = new URL(urlPath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(6*1000);
        if(connection.getResponseCode()== 200){
            InputStream inputStream =connection.getInputStream();
            byte[] data =readInstream(inputStream);
            File file =new File("youdao.exe");
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(data);
            outputStream.close();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章