网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何在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中使用socket对zip文件进行传输有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前题目:如何在java中使用socket对zip文件进行传输
本文地址:http://bjjierui.cn/article/jcegpj.html

其他资讯