Java获取URL对应的资源
URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。URI 类在某些特定情况下对其组成字段执行转义。建议使用 URI 管理 URL 的编码和解码,并使用 toURI()和 URI.toURL() 实现这两个类之间的转换。
URLEncoder 和 URLDecoder 类,但是只适用于 HTML 形式的编码,它与 RFC2396 中定义的编码机制不同。import java.net.URL;
import java.net.URLConnection;
public class TestURL {
public static void main(String[] args) throws IOException {
test4();
test3();
test2();
test();
}
/**
* 获取URL指定的资源。
*
* @throws IOException
*/
public static void test4() throws IOException {
URL url = new URL("http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg");
//获得此 URL 的内容。
Object obj = url.getContent();
System.out.println(obj.getClass().getName());
}
/**
* 获取URL指定的资源
*
* @throws IOException
*/
public static void test3() throws IOException {
URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
//返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
URLConnection uc = url.openConnection();
//打开的连接读取的输入流。
InputStream in = uc.getInputStream();
int c;
while ((c = in.read()) != -1)
System.out.print(c);
in.close();
}
/**
* 读取URL指定的网页内容
*
* @throws IOException
*/
public static void test2() throws IOException {
URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
reader.close();
}
/**
* 获取URL的输入流,并输出
*
* @throws IOException
*/
public static void test() throws IOException {
URL url = new URL("http://lavasoft.blog.51cto.com/62575/120430");
//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
InputStream in = url.openStream();
int c;
while ((c = in.read()) != -1)
System.out.print(c);
in.close();
}
}
public class MainClass {
public static void main(String[] args) {
String host = "www.java2s.com";
String file = "/index.html";
String[] schemes = {"http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",
"jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",
"systemresource"};
for (int i = 0; i < schemes.length; i++) {
try {
URL u = new URL(schemes[i], host, file);
System.out.println(schemes[i] + " is supported\r\n");
} catch (Exception ex) {
System.out.println(schemes[i] + " is not supported\r\n");
}
}
}
}
http://lavasoft.blog.51cto.com/62575/120445/
Java获取URL对应的资源的更多相关文章
- java 获取url及url参数解析
java 获取url及url参数解析 一.url编码:URLEncoder.encode(userName); 二.url解码: URLDecoder.decode(userName);
- Java获取URL中的顶级域名domain的工具类
方式一: import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; import jav ...
- java 读取URL中的资源
Example13_1.java import java.net.*; import java.io.*; import java.util.*; public class Example13_1 { ...
- Java获取URL链接的文件类型
问题发生: Java从网络批量读取图片并保存至本网站服务器后再插入文章中 今天转入一篇文章 http://news.qq.com/a/20170605/045860.htm 发现图片未能成功上传 查看 ...
- java获取url中的参数
获取地址栏中的url中的userName的值 String userName=new String(request.getParameter("userName")); 获取中文的 ...
- java 获取URL链接 内容
public static String getHtmlConentByUrl(String ssourl) { try { URL url = new URL( ...
- Java 获取url参数
1. 方式一:使用HttpServletRequest对象 String id = arg0.getParameter("id"); mv.addObject("id&q ...
- java批量爬去电影资源
摘要 网上有很多个人站来分享电影资源,其实有时候我们自己也想做这个一个电影站来分享资源.但是这个时候就有一个问题,电影的资源应该从哪里来呢?难道要自己一条条手动去从网络上获取,这样无疑是缓慢而又效率低 ...
- java批量爬取电影资源
摘要 网上有很多个人站来分享电影资源,其实有时候我们自己也想做这个一个电影站来分享资源.但是这个时候就有一个问题,电影的资源应该从哪里来呢?难道要自己一条条手动去从网络上获取,这样无疑是缓慢而又效率低 ...
随机推荐
- STL algorithm算法mov,move_backward(38)
move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...
- HDU - 3341 Lost's revenge(AC自己主动机+DP)
Description Lost and AekdyCoin are friends. They always play "number game"(A boring game b ...
- TCP快速重传与快速恢复原理分析(四种不同的算法)
在TCP/IP中,快速重传和恢复(fast retransmit and recovery,FRR)是一种拥塞控制算法,它能快速恢复丢失的数据包.没有FRR,如果数据包丢失了,TCP将会使用定时器来要 ...
- Web网站架构演变—高并发、大数据
转 Web网站架构演变—高并发.大数据 2018年07月25日 17:27:22 gis_morningsun 阅读数:599 前言 我们以javaweb为例,来搭建一个简单的电商系统,看看这个系 ...
- 重写ajax方法实现异步请求session过期时跳转登录页面(转)
一般我们会在过滤器里判断登录状态,如果没登录就跳转登录页面,过滤器java核心代码如下: UserItem loginUser = (UserItem)request.getSession().get ...
- [PReact] Integrate Redux with Preact
Redux is one of the most popular state-management libraries and although not specific to React, it i ...
- libSVM介绍(二)
鉴于libSVM中的readme文件有点长,并且,都是採用英文书写,这里,我把当中重要的内容提炼出来,并给出对应的样例来说明其使用方法,大家能够直接參考我的代码来调用libSVM库. 第一部分,利用l ...
- 12.2 linux USB框架分析(详细注册match匹配过程)
首先我们先来简单说一说USB的框架,之后在来具体分析源码,以便加深理解!其实USB的框架比较像“平台总线.设备.驱动”的框架,也分为总线.设备.驱动三大块.其中总线驱动是已经由内核完成的,一旦接入u ...
- [Javascript Natural] Break up language strings into parts using Natural
A part of Natural Language Processing (NLP) is processing text by “tokenizing” language strings. Thi ...
- angular表单的使用实例
原文 https://www.jianshu.com/p/da1fd5396798 大纲 1.模板驱动表单的创建 2.响应式表单的创建 3.模板驱动型表单的自定义指令 4.响应式表单的自定义指令 5. ...