------- 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.html URL 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最大连接数
六.域名解析
省略