「javaurl链接」java访问url
本篇文章给大家谈谈javaurl链接,以及java访问url对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java获取URL
- 2、java代码连接数据库url怎么获取
- 3、java链接mysql数据库url怎么写
- 4、java编程 关于URL类链接的问题
- 5、用java怎么写URL接口
- 6、如何使用java调用url接口
java获取URL
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class GetLinks {
private String webSource;
private String url;
public GetLinks(String url) throws MalformedURLException, IOException {
this.url = Complete(url);
webSource = getWebCon(this.url);
}
private String getWebCon(String strURL) throws MalformedURLException,
IOException {
StringBuffer sb = new StringBuffer();
java.net.URL url = new java.net.URL(strURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
return sb.toString();
}
private String Complete(String link)throws MalformedURLException{
URL url1 = new URL(link);
URL url2 = new URL(link+"/");
String handledUrl = link;
try{
StringBuffer sb1 = new StringBuffer();
BufferedReader in1 = new BufferedReader(new InputStreamReader(url1
.openStream()));
String line1;
while ((line1 = in1.readLine()) != null) {
sb1.append(line1);
}
in1.close();
StringBuffer sb2 = new StringBuffer();
BufferedReader in2 = new BufferedReader(new InputStreamReader(url2
.openStream()));
String line2;
while ((line2 = in2.readLine()) != null) {
sb2.append(line2);
}
in1.close();
if(sb1.toString().equals(sb2.toString())){
handledUrl = link+"/";
}
}catch(Exception e){
handledUrl = link;
}
return handledUrl;
}
/**
* 处理链接的相对路径
* @param link 相对路径或绝对路径
* @return 绝对路径
*/
private String urlHandler(String link) {
if (link == null)
return null;
link = link.trim();
if (link.toLowerCase().startsWith("http://")
|| link.toLowerCase().startsWith("https://")) {
return link;
}
String pare = url.trim();
if (!link.startsWith("/")) {
if (pare.endsWith("/")) {
return pare + link;
}
if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {
return pare + "/" + link;
} else {
int lastSeparatorIndex = url.lastIndexOf("/");
return url.substring(0, lastSeparatorIndex + 1) + link;
}
}else{
if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {
return pare + link;
}else{
return url.substring(0,url.indexOf("/", url.indexOf("//")+3)) + link;
}
}
}
public ListString getAnchorTagUrls() {
if (webSource == null) {
System.out.println("没有网页源代码");
return null;
}
ArrayListString list = new ArrayListString();
int index = 0;
while (index != -1) {
index = webSource.toLowerCase().indexOf("a ", index);
if (index != -1) {
int end = webSource.indexOf("", index);
String str = webSource.substring(index, end == -1 ? webSource
.length() : end);
str = str.replaceAll("\\s*=\\s*", "=");
if (str.toLowerCase().matches("^a.*href\\s*=\\s*[\'|\"]?.*")) {// "^a\\s+\\w*\\s*href\\s*=\\s*[\'|\"]?.*"
int hrefIndex = str.toLowerCase().indexOf("href=");
int leadingQuotesIndex = -1;
if ((leadingQuotesIndex = str.indexOf("\"", hrefIndex
+ "href=".length())) != -1) { // 形如a
// href="....."
int TrailingQuotesIndex = str.indexOf("\"",
leadingQuotesIndex + 1);
TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str
.length() : TrailingQuotesIndex;
str = str.substring(leadingQuotesIndex + 1,
TrailingQuotesIndex);
str = urlHandler(str);
list.add(str);
System.out.println(str);
index += "a ".length();
continue;
}
if ((leadingQuotesIndex = str.indexOf("\'", hrefIndex
+ "href=".length())) != -1) { // 形如a
// href='.....'
int TrailingQuotesIndex = str.indexOf("\'",
leadingQuotesIndex + 1);
TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str
.length() : TrailingQuotesIndex;
str = str.substring(leadingQuotesIndex + 1,
TrailingQuotesIndex);
str = urlHandler(str);
System.out.println(str);
list.add(str);
index += "a ".length();
continue;
}
int whitespaceIndex = str.indexOf(" ", hrefIndex
+ "href=".length()); // 形如a href=
//
whitespaceIndex = whitespaceIndex == -1 ? str.length()
: whitespaceIndex;
str = str.substring(hrefIndex + "href=".length(),
whitespaceIndex);
str = urlHandler(str);
list.add(str);
System.out.println(str);
}
index += "a ".length();
}
}
return list;
}
public static void main(String[] args) throws Exception {
GetLinks gl = new GetLinks("");
ListString list = gl.getAnchorTagUrls();
for(String str:list) {
System.out.println(str);
}
}
}
java代码连接数据库url怎么获取
都有统一的格式的,如下:
Microsoft SQL Server JDBC Driver (一般用来连接 SQLServer 2000)
驱动程序包名:msbase.jar mssqlserver.jar msutil.jar
驱动程序类名: com.microsoft.jdbc.sqlserver.SQLServerDriver
JDBC URL: jdbc:microsoft:sqlserver://server_name:port
默认端口1433,如果服务器使用默认端口则port可以省略
Microsoft SQL Server 2005 JDBC Driver
驱动程序包名:sqljdbc.jar
驱动程序类名: com.microsoft.sqlserver.jdbc.SQLServerDriver
JDBC URL: jdbc:sqlserver://server_name:port
默认端口1433,如果服务器使用默认端口则port可以省略
Oracle
Oracle Thin JDBC Driver
驱动程序包名:ojdbc14.jar
驱动程序类名: Oracle.jdbc.driver.OracleDriver
JDBC URL:
jdbc:oracle:thin:@//host:port/ServiceName
或
jdbc:oracle:thin:@host:port:SID
java链接mysql数据库url怎么写
连接代码如下:
public static void main(String[] args){
// 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "root";
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
// statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from student";
// 结果集
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 学号" + "\t" + " 姓名");
System.out.println("-----------------");
String name = null;
while(rs.next()) {
// 选择sname这列数据
name = rs.getString("sname");
// 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
// 然后使用GB2312字符集解码指定的字节数组
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
// 输出结果
System.out.println(rs.getString("sno") + "\t" + name);
}
rs.close();
conn.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
java编程 关于URL类链接的问题
package com.cstp.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cstp.Bean.UserBean;
import com.cstp.Dao.Users;
public class CheckLogin extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
public CheckLogin() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据cookie里的内容判断 用户是否在线状态和用户名是否存在
boolean islogin=false;
String username = "";
Cookie[] c=request.getCookies();
if(c!=null){
for(Cookie e:c){
String name=e.getName();
String value=e.getValue();
if("old".equals(name)){
//dopost方法里要求记住账号登陆成功后增加的有个cookie是("old","1")
islogin=(name.equals("1"));
}
if("user".equals(name)){
username=value;
}
}
if(isloginusername.trim().length()0){
request.getRequestDispatcher("/main.jsp").forward(request, response);
}}
else
request.getRequestDispatcher("/login.html").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
String isadmin=request.getParameter("isadmin");
String isrecord=request.getParameter("isrecord");
Users user=new Users();
//填充JavaBean
UserBean ub=new UserBean();
ub.setUsername(username);
ub.setPassword(password);
ub.setIsadmin(isadmin);
ub=user.CheckUser(ub);
request.getSession().setAttribute("ub",ub);
if(ub!=null){
if(isrecord!=null"record".equals(isrecord)){
Cookie c1=new Cookie("old","1");
c1.setMaxAge(100000);
c1.setDomain("");//设置域
Cookie c2=new Cookie("user",username);
c2.setMaxAge(100000);
c2.setDomain("");//设置域
response.addCookie(c1);
response.addCookie(c2);
}
//============================================
if(ub.getIsadmin()!=null){
if("yes".equals(ub.getIsadmin())){
request.getRequestDispatcher("/admin.html").forward(request, response);
}
else if("no".equals(ub.getIsadmin())){
request.getRequestDispatcher("/main.jsp").forward(request, response);
}
else
request.getRequestDispatcher("/fail.html").forward(request, response);
}
else
request.getRequestDispatcher("/main.jsp").forward(request, response);
//============================================
}
else
request.getRequestDispatcher("/fail.html").forward(request, response);
}
}
用java怎么写URL接口
在java中,调用http请求接口,主要通过流的方式进行调用,示例接口如下:
/**
* 程序中访问http数据接口
*/
public String searchLoginService(String urlStr) {
/** 网络的url地址 */
URL url = null;
/** http连接 */
HttpURLConnection httpConn = null;
/**//** 输入流 */
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try{
url = new URL(urlStr);
in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") );
String str = null;
while((str = in.readLine()) != null) {
sb.append( str );
}
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
} finally{
try{
if(in!=null) {
in.close();
}
}catch(IOException ex) {
logger.error(ex.getMessage(), ex);
}
}
String result =sb.toString();
System.out.println(result);
return result;
}
如何使用java调用url接口
原贴地址
一、在java中调用url,并打开一个新的窗口
Java代码
String url="";
String cmd = "cmd.exe /c start " + url;
try {
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
二、在java中调用url,后台调用。并取得返回值
Java代码
URL U = new URL("");
URLConnection connection = U.openConnection();
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine())!= null)
{
result += line;
}
in.close();
javaurl链接的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java访问url、javaurl链接的信息别忘了在本站进行查找喔。