java 传输 压缩文件_java基于socket传输zip文件功能示例

本文实例讲述了java基于socket传输zip文件的方法。分享给大家供大家参考,具体如下:

服务器端程序:

import java.io.*;

import java.net.*;

import java.io.BufferedInputStream;

public class SocketServer {

ServerSocket ss=null;

Socket s=null;

DataInputStream inStream=null;

DataOutputStream outStream=null;

FileInputStream fin = null;

public SocketServer() {

try{

ss=new ServerSocket(765);

s.setSoTimeout(3000);

}catch(Exception e){

System.out.println(e.toString());

}

}

void waitForClient(){

try{

while(true){

s=ss.accept();

ThreadServer thread = new ThreadServer(s);

thread.start();

}

}catch(Exception e){

System.out.println(e.toString());

}

}

public static void main(String[] args) {

SocketServer socketServer1 = new SocketServer();

socketServer1.waitForClient();

}

}

线程类:

import java.io.*;

import java.net.*;

class ThreadServer extends Thread{

private Socket socket;

private DataInputStream inStream=null;

private DataOutputStream outStream=null;

private FileInputStream fin = null;

public ThreadServer(Socket sock){

this.socket = sock;

}

public void run(){

boolean bool = false;

//while(!bool){

try{

inStream=new DataInputStream(socket.getInputStream());

outStream=new DataOutputStream(socket.getOutputStream());

fin = new FileInputStream("C:/temp/socket/200212060001_ds.zip");

//socket.setSoTimeout(3000);

byte[] b = new byte[200];

int i;

while((i=fin.read(b))!=-1){

outStream.write(b);

}

fin.close();

socket.close();

//bool = true;

}catch(IOException ex){

System.out.println(ex);

}

//}

}

}

客户端:

import java.net.*;

import java.io.*;

public class SocketClient{

Socket s=null;

DataInputStream inStream=null;

DataOutputStream outStream=null;

FileOutputStream fout = null;

public SocketClient() {

try{

s=new Socket("192.9.207.52",765); //把这里的IP改成你运行SocketServer.class的IP

inStream=new DataInputStream(s.getInputStream());

outStream=new DataOutputStream(s.getOutputStream());

fout = new FileOutputStream("C:/temp/socket/test11.zip");

s.setSoTimeout(3000);

waitData();

}

catch(Exception e){

System.out.println(e.toString());

}

}

void init() throws Exception{

}

void waitData(){

try{

byte[] b = new byte[200];

int i;

while((i=inStream.read(b))!=-1){

fout.write(b);

}

fout.flush();

fout.close();

s.close();

}catch(Exception e){

System.out.println(e.toString());

}

}

public static void main(String[] args) {

SocketClient socketClient1 = new SocketClient();

}

}

希望本文所述对大家java程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值