关于SOCKET网络传输 文件夹,出现问题
我现在写了一个Java Socket 程序,就是服务器部分向客户端部分发送一个文件夹,可是现在写的结果出现了,就是服务器发送过去后,在客户端第一文件可以接收到,但是第二个,第三个....都为0字节,现在急死了,CSDN的的哥哥姐姐们,帮帮忙看看,为啥 ,然后给写解决办法,和一点点源程序,小弟,感激不尽!!!
以下为服务器代码:
import java.net.*;
import java.util.Vector;
import com.cyberwisdom.tools.*;
import java.io.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import com.cyberwisdom.tools.Log;
import com.cyberwisdom.tools.share.DataPacket;
public class MultiFileSuport extends Thread{
private Socket sck;
private Vector vFile;//缓存文件列表
public MultiFileSuport(Socket sck,Vector vFile){
this.sck=sck;
this.vFile=vFile;
this.start();
Log.logDebug("Thread start success");
}
public void run(){
ObjectOutputStream out=null;
DataOutputStream dOut=null;
BufferedInputStream in=null;
byte [] buffer=new byte[1024*5];
try{
out=this.getObjectOutputStream(sck);
out.writeObject(vFile);//发送文件列表参数,在客户端在处理路径
out.flush();
//发送文件流
for(int i=0;i<vFile.size();i++)
{
dOut=this.getDataOutputStream(sck);
File file=new File(vFile.get(i).toString());
in=new BufferedInputStream(new FileInputStream(file));
dOut.writeLong(file.length());
while(true){
int read=0;
if(in!=null){
read=in.read(buffer);
}
if(read==-1){
break;
}
dOut.write(buffer,0,read);
}
}
dOut.flush();
dOut.close();
}
catch(Exception ex){
Log.writeLog("","MultiFileSuport->run",ex);
}
}
private ObjectOutputStream getObjectOutputStream(Socket sck){
ObjectOutputStream out=null;
try{
out=new ObjectOutputStream(sck.getOutputStream());
}catch(IOException ex){
Log.writeLog("","MultiFileSuport->getObjectOutputStream",ex);
}
return out;
}
private DataOutputStream getDataOutputStream(Socket skc){
DataOutputStream out=null;
try{
out=new DataOutputStream(sck.getOutputStream());
}catch(IOException ex){
Log.writeLog("","MultiFileSuport->getDataOutputStream",ex);
}
return out;
}
}
以下为客户端部分:
import java.net.*;
import java.io.*;
import java.util.Vector;
import com.cyberwisdom.tools.ReadConfig;
import com.cyberwisdom.tools.Log;
import com.cyberwisdom.tools.share.DataPacket;
import com.cyberwisdom.tools.StringUtil;
public class SocketFileClient extends Thread
{
private ReadConfig cfg=ReadConfig.newInstance();
private boolean isStop=false;
byte [] buf=new byte[1024*5];
private Vector vFile=new Vector();
public SocketFileClient(){
this.start();
}
public void run(){
BufferedOutputStream out=null;
ObjectInputStream in=null;
Socket sck=this.getSocket();
DataInputStream dIn=null;
try{
in=this.getObjectInputStream(sck);
vFile=(Vector)in.readObject();
if(!vFile.isEmpty()){
for(int i=0;i<vFile.size();i++){
String path=vFile.get(i).toString();
String fileName=this.getFileName(path);
path=this.replace(path.substring(path.indexOf(":")+2),fileName,"");
String sDirec=cfg.localFilePath+path;
//建立文件夹
File fDircetory=new File(sDirec);
if(!fDircetory.exists()){
fDircetory.mkdirs();
}
//建立文件
File fCreate=new File(sDirec+fileName);
if(!fCreate.exists()){
fCreate.createNewFile();
}
dIn=this.getDataInputStream(sck);
out=new BufferedOutputStream(new FileOutputStream(fCreate));
if(dIn!=null){
//写入文件
while(true){
int read=0;
if(dIn!=null){
read=dIn.read(buf);
}
if(read==-1){
break;
}
out.write(buf,0,read);
}
out.flush();
}
else{
Log.logDebug("结束一个文件");
continue;
}
Log.logDebug(sDirec+fileName);
Log.logDebug("成功写入文件");
}
}
}
catch(Exception ex){
Log.writeLog("","SocketFileClient->download",ex);
}
}
private DataInputStream getDataInputStream(Socket sck){
DataInputStream in=null;
try{
in=new DataInputStream(sck.getInputStream());
}
catch(IOException ex){
Log.writeLog("","Socket->getDataInputStream",ex);
}
return in;
}
private Socket getSocket(){
Socket sck=null;
try{
sck=new Socket(cfg.host,cfg.port);
}
catch(UnknownHostException ex){
Log.writeLog("","Socket->getSocket",ex);
}
catch(IOException exc){
Log.writeLog("","Socket->getSocket",exc);
}
return sck;
}
private ObjectInputStream getObjectInputStream(Socket sck){
ObjectInputStream in=null;
try{
in=new ObjectInputStream(sck.getInputStream());
}
catch(IOException ex){
Log.writeLog("","Socket->getObjectInputStream",ex);
}
return in;
}
private String getFileName(String path){
File file=new File(path);
return file.getName();
}
private String replace( String line, String oldString, String newString )
{
if (line == null) {
return null;
}
int i=0;
if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
}