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

利用TCP协议 下传图片、文件

2012-09-22 
利用TCP协议 上传图片、文件源码:上传图片:服务端:package com.hbsi.netimport java.io.Fileimport java.

利用TCP协议 上传图片、文件

源码:

上传图片:

服务端:

package com.hbsi.net;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class JpgServer2 {

 /**
  * @param args
  */
public static void main(String[] args) throws Exception{
  
  ServerSocket ss=new ServerSocket(10001);
  
  while(true){
   
   Socket s=ss.accept();
   
   new Thread(new JpgThread(s)).start();
  }
  
  
  
  
  //ss.close();
  
 }

}


class JpgThread implements Runnable{
 
 private Socket s;
 
 public JpgThread(Socket s){
  this.s=s;
 }

 @Override
 public void run() {
  int count=1;
  try{
   String ip=s.getInetAddress().getHostAddress();
   
   System.out.println(ip+"---连接成功…………");
   InputStream in=s.getInputStream();
   
   File dir=new File("F:\\picture");
   
   File f=new File(dir,ip+"("+count+").jpg");
   
   while(f.exists())
    f=new  File(dir,ip+"("+(count++)+").jpg");
   
   FileOutputStream fos=new FileOutputStream(f);
   
   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){
   e.printStackTrace();
  }
  
 }
 
}

客户端

package com.hbsi.net
;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class JpgClient2 {

 /**
  * @param args
  */
public static void main(String[] args)throws Exception {
 
    if(args.length==0){
     System.out.println("请指定一个jpg文件");
     return;
    }
    File f=new File(args[0]);
  
  Socket s=new Socket("192.168.49.109",10001);
  
  FileInputStream fis=new FileInputStream(f);
  
  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[] b=new byte[1024];
  int num=in.read(b);
  System.out.println(new String(b,0,num));
  
  fis.close();
  
  s.close();
  
  
  
  

 }

}

编译运行结果:

192.168.49.109---连接成功…………

上传成功

 

上传文件:

package com.hbsi.net;

import java.net.*;
import java.io.*;

public class JpgServer {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception{
  
  ServerSocket ss=new ServerSocket(9009);
  
  Socket s=ss.accept();
  
  InputStream in=s.getInputStream();
  
  FileOutputStream fos=new FileOutputStream("E:\\dog.txt");
  
  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();
  
 }

}

 

package com.hbsi.net;

import java.net.*;
import java.io.*;

public class JpgClient {

 /**
  * @param args
  */
 public static void main(String[] args)throws Exception {
  
  Socket s=new Socket("192.168.49.109",9009);
  
  FileInputStream fis=new FileInputStream("d:\\2.txt");
  
  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[] b=new byte[1024];
  int num=in.read(b);
  System.out.println(new String(b,0,num));
  
  fis.close();
  
  s.close();
  
  
  
  

 }

}

 

2楼sgx425021234昨天 20:34
thank you
1楼xueshuangshuang123昨天 20:33
good

热点排行