黑马程序员_java入门_网路编程_02
来源:互联网 发布:网站关键词排名优化 编辑:程序博客网 时间:2024/04/28 06:51
------- android培训、java培训、期待与您交流! ----------
一.TCP上传图片
客户端
/*需求:上传图片。*//*客户端:1.服务端点。2.读取客户端已有的图片数据。3.通过socket输出流将数据发给服务端。4.读取服务端反馈信息。5.关闭。*/import java.net.*;import java.io.*;class PicClient{public static void main(String[] args)throws Exception {Socket s=new Socket("172.16.56.237",1005);FileInputStream fis=new FileInputStream("e:\\1.jpg");OutputStream out=s.getOutputStream();byte[] buf=new byte[1024];int len=0;while((len=fis.read(buf))!=-1){out.write(buf,0,len);}s.shutdownOutput();//告诉服务端数据已写完。(增加结束标记)InputStream in=s.getInputStream();byte[] bufIn=new byte[1024];int num=in.read(bufIn);System.out.println(new String(bufIn,0,num));fis.close();s.close();}}
服务端
/*服务端:*/import java.net.*;import java.io.*;class PicServer{public static void main(String[] args)throws Exception{ServerSocket ss=new ServerSocket(1005);Socket s=ss.accept();InputStream in=s.getInputStream();FileOutputStream fos=new FileOutputStream("f:\\server.jpg");byte[] buf=new byte[1024];int len=0;while((len=in.read(buf))!=-1){fos.write(buf,0,len);}OutputStream out=s.getOutputStream();out.write("图片已收到".getBytes());fos.close();s.close();ss.close();}}
二.同时多个客户上传图片
class PicThread implements Runnable{private Socket s;PicThread(Socket s){this.s=s;}public void run(){int count=1;String ip=s.getInetAddress().getHostAddress();try{System.out.println(ip+"..........connceted");InputStream in=s.getInputStream();//为了不覆盖File file=new File("f:\\"+ip+"("+(count)+")"+".jpg");while(file.exists())file=new File("f:\\"+ip+"("+(count++)+")"+".jpg");FileOutputStream fos=new FileOutputStream(file);byte[] buf=new byte[1024];int len=0;while((len=in.read(buf))!=-1){fos.write(buf,0,len);}OutputStream out=s.getOutputStream();out.write("图片已收到".getBytes());fos.close();s.close();}catch (Exception e){throw new RuntimeException(ip+"上传失败!");}}}class PicServer{public static void main(String[] args)throws Exception{ServerSocket ss=new ServerSocket(1005);while(true){Socket s=ss.accept();new Thread(new PicThread(s)).start();}}}三.自定义服务端
ServerSocket ss=new ServerSocket(1005);
Socket s=ss.accpet()
PrintWriter out=new PrintWriter(s.getOutPutStream(),true)
out.println("客服端是我")
四.Tomcat服务端
步骤:1.先运行服务端
2.自定义客户端访问
五.URL用处
/*通过URLConnection拆封:*/import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;class MyIEByGUI{private Frame f;private TextField tf;private Button but;private TextArea ta;private Dialog d;private Label lab;private Button okBut;MyIEByGUI2(){init();}public void init(){f=new Frame("my window");f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf=new TextField(60);but=new Button("转到");ta=new TextArea(25 ,70);// 行,列d=new Dialog(f,"提示信息",true);//第3个参数:指定在显示的时候是否阻止用户将内容输入到其他顶级窗口中。d.setBounds(400,200,240,150);d.setLayout(new FlowLayout());lab=new Label();okBut=new Button("确定");d.add(lab);d.add(okBut);f.add(tf);f.add(but);f.add(ta);myEvent();f.setVisible(true);}private void myEvent(){but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){try{showDir();}catch (Exception ee){String info="您输入的地址是错误的,请重新输入!";lab.setText(info);d.setVisible(true);}}});f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){try{if(e.getKeyCode()==KeyEvent.VK_ENTER)showDir();}catch (Exception ee){String info="您输入的地址是错误的,请重新输入!";lab.setText(info);d.setVisible(true);}}});okBut.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_ENTER)d.setVisible(false);}});d.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){d.setVisible(false);}});okBut.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){d.setVisible(false);}});}private void showDir()throws Exception{ ta.setText("");//清空String urlPath=tf.getText();// http://172.16.56.237:8080/myweb/index.htmlURL url=new URL(urlPath);URLConnection conn=url.openConnection();InputStream in=conn.getInputStream();byte[] buf=new byte[1024];int len=in.read(buf);ta.setText(new String(buf,0,len));}public static void main(String[] args) {new MyIEByGUI();}}
ServerSocket(int port,int backlog)
port监听的服务所使用的接口
backlog最大连接数
六.域名解析
省略
- 黑马程序员_java入门_网路编程_02
- 黑马程序员_java入门_网路编程_01
- 黑马程序员_java基础_面向对象_02
- 黑马程序员_Java基础_02
- 黑马程序员_网路编程小结
- 黑马程序员_线程_02
- 黑马程序员 网路编程
- 黑马程序员_java入门_位运算
- 黑马程序员_java入门_数组
- 黑马程序员_java入门_面向对象
- 黑马程序员_java入门_设计模式
- 黑马程序员_java入门_继承
- 黑马程序员_java入门_多态
- 黑马程序员_java入门_内部类
- 黑马程序员_java入门_异常
- 黑马程序员_java入门_包
- 黑马程序员_java入门_多线程
- 黑马程序员_java入门_泛型
- OpenCV矩阵运算
- APUE学习笔记——信号
- iSCSI
- APUE学习笔记——线程
- Word2007中看不到图片
- 黑马程序员_java入门_网路编程_02
- Linux C编程--进程介绍3--进程终止和等待
- 怎么隐藏文件类型的扩展名
- APUE学习笔记——线程控制
- C++ - 对象模型之 构造和析构函数都干了什么
- 聊天机器人Eliza 代码
- python BeautifulSoup模块的安装
- iPhone如何备份SHSH
- pat 1028 List Sorting