最近公司,要做一个自动更新方案,中间涉及到文件传输部分,我采用了用Socket来实现,具体看以下原代码.主要采用了对像序列化实现.
序列化的一个数据包.
public class DataPacket implements Serializable{
private String fileName;
private long fileSize;
private String filePath;
public DataPacket(String fileName,long fileSize,String filePath){
this.fileName=fileName;
this.fileSize=fileSize;
this.filePath=filePath;
}
public DataPacket(){
}
public void setFileName(String fileName){
this.fileName=fileName;
}
public String getFileName(){
return this.fileName;
}
public void setFileSize(long fileSize){
this.fileSize=fileSize;
}
public long getFileSize(){
return this.fileSize;
}
public void setFilePath(String filePath){
this.filePath=filePath;
}
public String getFilePath(){
return this.filePath;
}
}
客户端部分
public class SocketFileClient extends Thread{
private static final ReadConfig cfg=ReadConfig.newInstance();
/***
*存文件
*/
private Vector vFile=new Vector();
private Vector vFileName=new Vector();
private Vector vFileSize=new Vector();
/***
*文件全路径
*/
private Vector vPath=new Vector();
public SocketFileClient(){
initFileList(new File(cfg.localPath));
this.start();
}
private void initFileList(File file){
File [] list=file.listFiles();
for(int i=0;i<list.length;i++){
if(list[i].isFile())
{
vFile.add(list[i].getAbsolutePath());
vFileName.add(list[i].getName());
vFileSize.add(list[i].length());
}
else if(list[i].isDirectory())
{
initFileList(list[i]);
}
}
}
public void run(){
ObjectOutputStream out=null;
Socket sck=null;
BufferedInputStream in=null;
byte [] buffer=new byte[1024];
try
{
for(int i=0;i<vFile.size();i++)
{
sck=this.getSocket();
String filePath=vFile.get(i).toString();
out=this.getObjectOutputStream(sck);
DataPacket packet=new DataPacket(vFileName.get(i).toString()
,Long.parseLong(vFileSize.get(i).toString()),filePath);
out.writeObject(packet);
File file=new File(filePath);
UserLogger.logDebug("文件绝对路径:"+filePath);
in=new BufferedInputStream(new FileInputStream(file));
while(true){
int read=0;
if(in!=null){
read=in.read(buffer);
}
if(read==-1){
break;
}
out.write(buffer,0,read);
}
out.flush();
sck.close();
}
out.close();
in.close();
}
catch(Exception ex){
UserLogger.logDebug(ex);
try{
sck.close();
}
catch(Exception exx){
UserLogger.logDebug(exx);
}
}
}
private Socket getSocket(){
Socket sck=null;
try{
sck=new Socket(cfg.host,cfg.port);
}
catch(UnknownHostException ex){
UserLogger.logDebug("getSocket",ex);
}
catch(IOException exc){
UserLogger.logDebug("getSocket",exc);
}
return sck;
}
public static void main(String [] args){
SocketFileClient client=new SocketFileClient();
Vector v=client.vFile;
UserLogger.logDebug(v.size()+"");
}
private ObjectOutputStream getObjectOutputStream(Socket sck){
ObjectOutputStream out=null;
try{
out=new ObjectOutputStream(sck.getOutputStream());
}
catch(IOException ex){
UserLogger.logDebug("getObjectOutputStream",ex);
}
return out;
}
}
服务器部分(中间用了一个多线程,用于多用户上传)
public class MulitFile extends Thread{
private static final ReadConfig cfg=ReadConfig.newInstance();
private Socket sck;
public MulitFile(Socket sck){
this.sck=sck;
}
public void run(){
ObjectInputStream in=this.getObjectInputStream(sck);
BufferedOutputStream out=null;
byte [] buffer=new byte[1024];
try{
DataPacket packet=(DataPacket)in.readObject();
if(packet!=null){
String path=packet.getFilePath();
path=cfg.remotePath+path.substring(path.indexOf(":")+1,path.length());
path=this.replace(path,packet.getFileName(),"");
File file=new File(path);
if(!file.exists()) file.mkdirs();
out=new BufferedOutputStream(new FileOutputStream(file));
while(true){
int read=0;
if(in!=null){
read=in.read(buffer);
}
if(read==-1){
break;
}
out.write(buffer,0,read);
}
out.flush();
out.close();
UserLogger.logDebug(path);
}
}
catch(Exception ex){
UserLogger.logDebug(ex);
}
}
private ObjectInputStream getObjectInputStream(Socket sck){
ObjectInputStream in=null;
try{
in=new ObjectInputStream(sck.getInputStream());
}
catch(IOException ex){
UserLogger.logDebug(ex);
}
return in;
}
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;
}
}
服务器起动:
public class SocketFileServer
{
private static final ReadConfig cfg=ReadConfig.newInstance();
private boolean flag=false;
public SocketFileServer(){
try{
ServerSocket ssk=new ServerSocket(cfg.port);
while(!flag){
Socket sck=ssk.accept();
MulitFile mu=new MulitFile(sck);
Thread thread=new Thread(mu);
thread.start();
}
}
catch(Exception ex){
UserLogger.logDebug(ex);
}
}
public static void main(String [] args)
{
SocketFileServer svr=new SocketFileServer();
}
}