首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

34、TCP并发下传,URL

2013-03-21 
34、TCP并发上传,URL一、TCP并发上传任意文件?? ? 服务器端要用到多线程技术?import java.io.*import java.

34、TCP并发上传,URL

一、TCP并发上传任意文件

?

? ? 服务器端要用到多线程技术

?

import java.io.*;import java.net.*;class Client{public static void main(String[] args)throws Exception{//创建客户端的socket服务,指定目的主机和端口Socket s = new Socket("192.168.0.253",10000);FileInputStream fis = new FileInputStream("e:/装机软件_29001.zip");BufferedReader brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));OutputStream os = s.getOutputStream();byte[] buf = new byte[1024];int len = 0;while((len = fis.read(buf))!=-1){os.write(buf,0,len);}//加上结束标记s.shutdownOutput();String res = brIn.readLine();System.out.println(res);s.close();fis.close();}}class Server{public static void main(String[] args)throws Exception{ServerSocket ss = new ServerSocket(10000);while(true){Socket s = ss.accept();new Thread(new Method(s)).start();}}}class Method implements Runnable{Socket s;Method(Socket s){this.s = s;}public void run(){int count = 0;try{String ip = s.getInetAddress().getHostAddress();System.out.println(ip+" 连接....");InputStream is = s.getInputStream();File file = new File("d:/a.zip");while(file.exists())file = new File("d:/a"+(count++)+".zip");FileOutputStream fos = new FileOutputStream(file);BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));byte[] buf = new byte[1024];int len = 0;while((len=is.read(buf))!=-1){fos.write(buf,0,len);}bwOut.write("上传成功");bwOut.newLine();bwOut.flush();s.close();}catch (Exception e){System.out.println("上传失败");}}}

?

二、URL

?

    类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。
public final class URL implements Serializable{//根据 String 表示形式创建 URL 对象public URL(String spec)throws MalformedURLException{}//获取与此 URL 关联协议的默认端口号public int getDefaultPort(){}//获取此 URL 的文件名。返回的文件部分将与 getPath() 相同,再加上 getQuery() 值的规范化形式(如果有)。public String getFile(){}//获取此 URL 的主机名(如果适用)。public String getHost(){}//获取此 URL 的路径部分。public String getPath(){}//获取此 URL 的端口号public int getPort(){}//获取此 URL 的协议名称public String getProtocol(){}//获取此 URL 的查询部分public String getQuery(){}//返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。 //每次调用此 URL 的协议处理程序的 openConnection 方法都打开一个新的连接。public URLConnection openConnection()                             throws IOException{}}

?

import java.net.*;public class URLTest {public static void main(String[] args) throws MalformedURLException {URL url = new URL("http://pan.baidu.com/disk/home?dir/path=%2Fday24");System.out.println(url.getHost());// pan.baidu.comSystem.out.println(url.getProtocol());// httpSystem.out.println(url.getPort());// -1System.out.println(url.getDefaultPort());// 80System.out.println(url.getPath());// /disk/homeSystem.out.println(url.getFile());// /disk/home?dir/path=%2Fday24System.out.println(url.getQuery());// dir/path=%2Fday24}}

?

三、URLConnection

?

    抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接。此类的实例可用于读取和写入此 URL 引用的资源通常,创建一个到 URL 的连接需要几个步骤通过在 URL 上调用 openConnection 方法创建连接对象。?处理设置参数和一般请求属性。?使用 connect 方法建立到远程对象的实际连接。?远程对象变为可用。远程对象的头字段和内容变为可访问
public abstract class URLConnection{//打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。 public abstract void connect()                      throws IOException{}//返回从此打开的连接读取的输入流。public InputStream getInputStream()                           throws IOException{}//返回写入到此连接的输出流。public OutputStream getOutputStream()                             throws IOException{}}

?

import java.io.*;import java.net.*;public class URLTest {public static void main(String[] args) throws IOException {URL url = new URL("http://www.baidu.com");URLConnection con = url.openConnection();BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));String line = null;while((line=br.readLine())!=null){System.out.println(line);}}}

?

热点排行