java基于socket傳輸zip文件功能示例
更新時間:2017年07月04日 11:23:47 作者:小魏的馬仔
這篇文章主要介紹了java基于socket傳輸zip文件功能,結合實例形式分析了java使用socket進行文件傳輸的具體操作步驟與服務器端、客戶端相關實現技巧,需要的朋友可以參考下
本文實例講述了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相關內容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結》、《Java文件與目錄操作技巧匯總》、《Java數據結構與算法教程》、《Java操作DOM節(jié)點技巧總結》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
SpringBoot?SPI?機制和實現自定義?starter
這篇文章主要介紹了SpringBoot?SPI機制和實現自定義?starter,全稱是Service?Provider?Interface。簡單翻譯的話,就是服務提供者接口,是一種尋找服務實現的機制2022-08-08
Spring Boot利用@Async異步調用:使用Future及定義超時詳解
這篇文章主要給大家介紹了關于Spring Boot利用@Async異步調用:使用Future及定義超時的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot具有一定的參考學習價值,需要的朋友可以參考下2018-05-05
MyBatis不同Mapper文件引用resultMap實例代碼
這篇文章主要介紹了mybatis 不同Mapper文件引用resultMap的實例代碼,非常不錯具有參考借鑒價值,需要的朋友可以參考下2017-07-07
java動態(tài)規(guī)劃算法——硬幣找零問題實例分析
這篇文章主要介紹了java動態(tài)規(guī)劃算法——硬幣找零問題,結合實例形式分析了java動態(tài)規(guī)劃算法——硬幣找零問題相關原理、實現方法與操作注意事項,需要的朋友可以參考下2020-05-05
springboot+dubbo+validation 進行rpc參數校驗的實現方法
這篇文章主要介紹了springboot+dubbo+validation 進行rpc參數校驗的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09

