请问那位知道用Socket传输文件? ??太笼统了吧,socket的基本操作也就那么几个,文件的io也就那么几个,你想作什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我写了一个多线程程序,通过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的IPinStream=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(); }}在win2k jdk1.3下本机调试通过,希望能帮到你。 谢谢wks9527(空值异常) 提供的原代码!!!!!!! 关于四舍五入取小数点2位 如何提取主板的时间呢??? 在线求救 项目完成后发现的问题,关于jdom.jar? 留言板 面试题 JComboBox为何响应不了任何键盘事件? 求JasperReport的中文文档 新手问题:小弟刚入门两天,老师布置的作业有点小问题。请教。 有没有Java的DirectDraw类库包,敬请赐教! 关于创建包后运行的问题 各位大哥,介绍几本学java的书吧~~初学者
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();
}
}在win2k jdk1.3下本机调试通过,希望能帮到你。