http的java代码的简单介绍

我有一段java http下载的代码,但是我很多地方读不懂,帮我谢谢注释

package API;

在峨山县等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站建设 网站设计制作按需开发,公司网站建设,企业网站建设,高端网站设计,全网营销推广,成都外贸网站制作,峨山县网站建设费用合理。

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLDecoder;

import Get.Other_API.*;

public class DownFile implements Runnable

{

private String LOCAL_PATH="d:/";

private String Down_Path=null;

//下面两个是Down_Path的get和set的方法

public String getDown_Path() { return Down_Path;}

public void setDown_Path(String downPath) {Down_Path=downPath;}

public String getPath() { return LOCAL_PATH; }

public void setPath(String Path) {LOCAL_PATH=Path;}

public void DownNow()

{

//待下载文件地址

String fileUrl=getDown_Path();

InputStream in=null;

OutputStream out=null;

HttpURLConnection conn=null;

String fileName=null;

int count=0;

int finished=0;

int _temp=0;

try

{

//初始化连接

URL url=new URL(fileUrl);//将String类型的地址变为url对象

conn = (HttpURLConnection) url.openConnection();//开启连接

conn.setDoInput(true);//必要的,开启输入输出的设置

conn.setDoOutput(true);

//获取文件名

String disposition=conn.getHeaderField("Content-Disposition");

if(disposition!=null!"".equals(disposition))

{

//从头中获取文件名

fileName=disposition.split(";")[1].split("=")[1].replaceAll("\"","");

}

else

{

//从地址中获取文件名

fileName=fileUrl.substring(fileUrl.lastIndexOf("/")+1);

}

if(fileName!=null!"".equals(fileName))

{

//文件名解码

fileName=URLDecoder.decode(fileName, "utf-8");

}

else

{

System.out.println("Error");

//如果无法获取文件名,则随机生成一个

//fileName="file_"+(int)(Math.random()*10);

}

//读取数据

if(conn.getResponseCode()==HttpURLConnection.HTTP_OK)

{

//这种方法比较常用,得记住

byte[] buffer=new byte[2048];

in = conn.getInputStream();//获取文本的输入流

out=new FileOutputStream(new File(LOCAL_PATH,fileName));//确定输出的地方

int size=conn.getContentLength();

while((count=in.read(buffer))!=-1)//不断循环,每次读取2048比特的数据

{

if(count!=0)

{

out.write(buffer,0,count);//将count大小的数据写进去

finished+=count;//结尾的写入的位置改变,为下次写入做准备

if(_temp%500==0)

{

System.out.printf("下载已完成----%1$.2f%%\n",(double)finished/size*100);//动态输出下载的进度

_temp=0;

}

_temp++;

}

else

{

break;

}

}

}

}

catch (MalformedURLException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

finally

{

try

{

out.close();//关闭输出流

in.close();//关闭输入流

conn.disconnect();//关闭连接,有打开就有关闭

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

//因为该类实现了Runnable接口,所以得实现这个run方法

@Override

public void run() {

// TODO Auto-generated method stub

DownNow();//调用上面的DownNow方法

}

}

java http请求直接请求地址的代码怎么写

public static  String do_get(String url) throws ClientProtocolException, IOException {

  String body = "{}";

  DefaultHttpClient httpclient = new DefaultHttpClient();

  try {

      HttpGet httpget = new HttpGet(url);

      HttpResponse response = httpclient.execute(httpget);

      HttpEntity entity = response.getEntity();

      body = EntityUtils.toString(entity);

  } finally {

      httpclient.getConnectionManager().shutdown();

  }

  return body;

}

HttpPost发送字符串到服务器,服务器接收代码并显示怎么写Java代码?

服务器端接收客户端的请求的话,需要在服务器端的java文件实现HttpServlet这个接口,并且在web.xml里配置一个客户端的请求拦截。

web.xml里的代码里添加

servlet

servlet-nametestServlet/servlet-name!--这个名字可以自己定--

servlet-classcom.sun.testServlet/servlet-class!--这里是你需要接收客户端请求的那个类以及包名,也就是下面拦截到的url会转发到的那个类--

/servlet

servlet-mapping

servlet-nametestServlet/servlet-name!--和上面的name需要一样--

url-pattern/*/url-pattern!--什么类型的客户端请求会被拦截,/* 就是全拦截了--

/servlet-mapping

然后再服务器端的类文件,要实现 HttpServlet这个接口。并把doGet()方法和doPost()方法重写。

这两种方法分别对应的是客户端的get请求和post请求的处理,你的是post请求的话,就在doPost()方法内,写你的业务。

然后再用下面两句话,设置你要返回客户端的数据。

//这是设置你要返回去的数据。value才是你的数据,key是标签。

request.setAttribute("key", "value");

//这是设置你要返回去test.jsp这张页面。

request.getRequestDispatcher("test.jsp").forward(request, response);

不知道你是不是这个意思,你可以再去看看相关servlet方面的知识,

关于客户端和服务器端大概也就是有个servlet作为请求的拦截

然后经过相关判断后,选择性的传到服务器的相应类里面。

再经过类里面的业务,把得到需要的数据回传到指定的页面上。

java代码怎么将"http://…"这样一个路径,写成类似超链接那样?

java超链接:

button.setLabel("htmla href=\"http:\\\angelsinklow"angelsinklow/a/html");

如果用start的话,这样写

Runtime.getRuntime().exec("cmd /c start ‘http:\\\angelsinklow");

Runtime.getRuntime().exec("iexplore ");

对于JEditorPane,JTextPane,JTextArea,JLabel可以使用

setText("htmlA href=''test/A/html")

对于JEditorPane使用

setEditorKitForContentType("text/html", new PatchedHTMLEditorKit());

addHyperlinkListener(HyperlinkListener ... );

需要引入java.net.url包。

try{getAppletContext().showDocument(new URL("http:\\\angelsinklow"),"打开位置");}

catch(Exception ex) {System.out.println("error"); }

就超链接了。


当前文章:http的java代码的简单介绍
当前路径:http://scyanting.com/article/docgehg.html