java通过url(http)存取远端文件

来源:互联网 发布:免费即时通讯软件 编辑:程序博客网 时间:2024/06/07 05:52

详见代码

import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.URL;


public class Test
{
    public static void main(String[] args) throws Exception
    {
        String originaPath = "http://hw.ll0.com?file_id=1";
        InputStream input = null;
        HttpURLConnection urlConnection = null;
        try
        {
            URL url = new URL(originaPath);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            // 文件大小
            long size = urlConnection.getContentLengthLong();
            //文件流,用于存储
            input = urlConnection.getInputStream();
            //文件类型
            String mimeType = urlConnection.getContentType();
            System.out.println("size:" + size + ";mimeType:" + mimeType);
        }
        finally
        {
            if (input != null)
            {
                input.close();
            }


            if (urlConnection != null)
            {
                urlConnection.disconnect();
            }
        }
    }
}
原创粉丝点击