导航:首页 > 编程语言 > javaurl的使用

javaurl的使用

发布时间:2022-07-21 13:39:26

Ⅰ 如何使用java调用url接口

原贴地址http://yuanlijia1.iteye.com/blog/1088088


一、在java中调用url,并打开一个新的窗口

Java代码

Stringurl="http://10.58.2.131:8088/spesBiz/test1.jsp";
Stringcmd="cmd.exe/cstart"+url;

try{
Processproc=Runtime.getRuntime().exec(cmd);
proc.waitFor();
}
catch(Exceptione)
{
e.printStackTrace();
}


二、在java中调用url,后台调用。并取得返回值

Java代码

URLU=newURL("http://10.58.2.131:8088/spesBiz/test1.jsp");
URLConnectionconnection=U.openConnection();
connection.connect();

BufferedReaderin=newBufferedReader(newInputStreamReader(connection.getInputStream()));
Stringline;
while((line=in.readLine())!=null)
{
result+=line;
}
in.close();

Ⅱ 关于java中URL的使用问题

getResource("image/1.jpg");应该是这里路径问题。以eclipse环境为例
首先,你的project中必须把image文件夹设为源码或资源目录(重要)
这样image本身就成了资源的根目录/
在IDE编译输出/打包时,1.jpg会和你的AppletTest.class放在同个文件夹中
所以,getResource("image/1.jpg");必须改成getResource("/1.jpg");
而要使getResource("image/1.jpg");起作用就可能要放在 image/image/1.jpg(首个image做资源目录)

另外,如果你不用IDE——手工编译class,就必须把1.jpg放在相对AppletTest.class所在目录下image/1.jpg下,这样才能getResource("/image/1.jpg")

总之,getResource()中的路径,由编译后的class文件的所在位置相对于资源文件的位置决定。而不由java源码决定。

Ⅲ Java中可以使用URL定位到本地的某个文件吗

可以的,直接通过URL类实现即可。
举例:URL fileUrl = new URL("file:///E:/tmp/test.txt");
备注:引入的是“java.net.URL“类。

Ⅳ Java如何利用url下载MP3保存到本地

Java如何利用url下载MP3保存的方法:

1 /** ;

2 * TODO 下载文件到本地 ;

3 * @author nadim ;
4 * @date Sep 11, 2015 11:45:31 AM ;

5 * @param fileUrl 远程地址 ;

6 * @param fileLocal 本地路径 ;

7 * @throws Exception ;
8 */ ;

9 public void downloadFile(String fileUrl,String fileLocal) throws Exception {;

10 URL url = new URL(fileUrl);

11 HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();

12 urlCon.setConnectTimeout(6000);

13 urlCon.setReadTimeout(6000);

14 int code = urlCon.getResponseCode();

15 if (code != HttpURLConnection.HTTP_OK) {

16 throw new Exception("文件读取失败");

17 }

18 //读文件流;

19 DataInputStream in = new DataInputStream(urlCon.getInputStream());

20 DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));

21 byte[] buffer = new byte[2048];

22 int count = 0;

23 while ((count = in.read(buffer)) > 0) {;

24 out.write(buffer, 0, count);

25 }

26 out.close();

27 in.close();

28 }。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。

Ⅳ java中如何实现URL类

java中实现URL类,可以使用java工具类中的URL的类,实例如下:

importjava.io.*;
importjava.net.*;
publicclassURLTest
{
publicstaticvoidmain(String[]args)
{
try
{
URLurl=newURL("http://sports.163.com:80/nba/");//创建资源类型
Stringprotocol=url.getProtocol();//获取资源类型
Stringhost=url.getHost();//获取域名
intport=url.getPort();//获取端口
Stringfile=url.getFile();//获取路径
System.out.println("url地址的资源类型为:"+protocol+"域名为:"+host+"端口为:"+port+"路径为:"+file);
InputStreamis=url.openStream();//获取页面信息流
BufferedReaderbfr=newBufferedReader(newInputStreamReader(is));//封装成字符流
Stringlen;
while((len=bfr.readLine())!=null)
{
System.out.println(len);
}
bfr.close();
is.close();
}
catch(MalformedURLExceptione)
{
System.out.println("创建URL对象发生异常");
}
catch(IOExceptione)
{
System.out.println("发生IO操作异常");
}
}
}

Ⅵ 怎样在java里用URL引入图片

读取图片可以有以下两种方法:
①:ImageIO.read(new File("这里可以写目录,比如您提到的src/images/某张图片名"));
②:new ImageIcon("目录").getImage();
这两个方法都返回一个图片对象。可以用一个Image对象接收一下。

相对路径是指您所运行的程序的包 所在的文件夹开始的路径。
一般来说,上面两种读取方法读取时,是从项目的目录下开始找文件的。
所以,您把图片放在src下的images包中,正确的读取方法应该是:
Image img=ImageIO.read(new File("src/images/图片名"));或者
Image img=new ImageIcon("src/images/图片名").getImage();
得到这样一个Image对象后,就可以使用了。

Ⅶ java获取服务器文件,怎样用url返回

下面提供二种方法会使用java发送url请求,并获取服务器返回的值

第一种方法:
代码如下:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

(StringurlStr,Stringparam1,Stringparam2)throwsException{
StringtempStr=null;
HttpClienthttpclient=newDefaultHttpClient();
Propertiesproperties=newProperties();
HttpEntityentity=null;
StringxmlContent="";
try
{

//设置超时时间
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);

//封装需要传递的参数
List<NameValuePair>nvps=newArrayList<NameValuePair>();
nvps.add(newBasicNameValuePair("mainMemoCode",strmainMemoCode));
nvps.add(newBasicNameValuePair("recordPassWord",strrecordPassWord));
//客户端的请求方法类型
HttpPosthttpPost=newHttpPost(urlStr);
httpPost.setEntity(newUrlEncodedFormEntity(nvps,"GBK"));
HttpResponseresponse=httpclient.execute(httpPost);

//获取服务器返回Http的Content-Type的值
tempStr=response.getHeaders("Content-Type")[0].getValue().toString();

//获取服务器返回页面的值
entity=response.getEntity();
xmlContent=EntityUtils.toString(entity);
Stringstrmessage=null;
System.out.println(xmlContent);
System.out.println(response.getHeaders("Content-Type")[0].getValue().toString());
httpPost.abort();

}
catch(SocketTimeoutExceptione)
{
}
catch(Exceptionex)
{
ex.printStackTrace();
}
finally{
httpclient.getConnectionManager().shutdown();
}
第二种方法:

代码如下:


(StringurlStr,Stringparam1,Stringparam2)throwsException{

HttpURLConnectionurl_con=null;
try{
URLurl=newURL(urlStr);
StringBufferbankXmlBuffer=newStringBuffer();
//创建URL连接,提交到数据,获取返回结果
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","directclient");

PrintWriterout=newPrintWriter(newOutputStreamWriter(connection.getOutputStream(),"GBK"));
out.println(param);
out.close();
BufferedReaderin=newBufferedReader(newInputStreamReader(connection
.getInputStream(),"GBK"));

StringinputLine;

while((inputLine=in.readLine())!=null){
bankXmlBuffer.append(inputLine);
}
in.close();
tempStr=bankXmlBuffer.toString();
}
catch(Exceptione)
{
System.out.println("发送GET请求出现异常!"+e);
e.printStackTrace();

}finally{
if(url_con!=null)
url_con.disconnect();
}

returntmpeStr;
}

总结:多练习代码,熟练之后才能更快速的去了解代码的学习的方法。多去获取一些思维方面的书籍可以看看。

Ⅷ Java Applet 中使用URL

用记事本编译生成.class,然后编写.html文件,可以执行

Ⅸ java关于URL类的调用。。。

你上面的代码中一共有两个操作,1,向一个url请求数据;2,获得数据,并解析成html。你遇到的响应慢的问题不在你这个代码中,而是在你发起url请求时,url的服务器给你响应的速度。url的服务器可能有数据库的操作或其它耗时操作,当它没有给你返回数据时,你这个方法是阻塞的,并不会立刻返回。

阅读全文

与javaurl的使用相关的资料

热点内容
程序员理发店生意怎么样 浏览:601
程序员罗技 浏览:180
软考初级程序员课程2021下载 浏览:487
杭州程序员奶奶 浏览:878
不听命令造成错误 浏览:979
kool系统源码 浏览:608
流氓app在哪里看 浏览:98
域名购买了怎么指向服务器 浏览:121
安卓手机如何让照片颜色反转 浏览:859
怎么下载卓睿安手机版 浏览:514
h3crange命令 浏览:468
php前景和python 浏览:338
php压缩图片内存大小 浏览:495
在哪里可以查看云服务器的信息 浏览:70
python读取非txt文件 浏览:799
艾莫迅用什么编程软件好 浏览:227
android文件存储读取 浏览:214
php基础教程第5版 浏览:543
服务器里面怎么刷东西 浏览:194
荣耀手机如何快速把app切换页面 浏览:798