日期:2014-05-20  浏览次数:20822 次

一个关于wmv格式文件的传递
现在我想把一个wmv格式的文件从一台服务器复制到另外一台服务器,要用java的io来实现,谁知道该怎么做呢?怎么来进行复制?谁能给出比较详细的代码?谢谢,非常急切!

------解决方案--------------------
下面才是对的
麻烦把上面的删除吧,泄漏信息了。。。 -_-!

/*---------------------*/

/***************************************
* 先运行位于服务器B的Receiver
* 再运行位于服务器A的Sender进行文件传送
* 注端口被占用的话请换个端口试
***************************************/

import java.net.ServerSocket;
import java.io.IOException;
import java.net.Socket;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;

public class Receiver{
private ServerSocket ss;
private int serverPort;
private boolean stopFlag;
private String savePath;

public Receiver(int serverPort) {
this.serverPort = serverPort;
}

public void receiveTo(String savePath) throws IOException{
this.savePath = savePath;
ss = new ServerSocket(serverPort);

while (!stopFlag) {
Socket s = ss.accept();
new SaveFileThread(s).start();
}
}

public void stop() {
stopFlag = true;
}

class SaveFileThread extends Thread {
Socket s;

public SaveFileThread(Socket s) {
this.s = s;
}

public void run() {
try{
InputStream in = s.getInputStream();

byte[]buff = new byte[4096];
int length = 0;

// read fileName and separator

length = in.read(buff);

int separatorPos = findSeparator(buff);
String fileName = new String(buff,0,separatorPos);

File saveFile = new File(savePath+ "/ "+fileName);

FileOutputStream fout = new FileOutputStream(saveFile);
fout.write(buff,separatorPos + 1,length - separatorPos -1);

while ((length = in.read(buff)) > 0) {
fout.write(buff,0,length);
}
fout.flush();

in.close();
fout.close();

} catch (Exception ex){
ex.printStackTrace();
}
}

private int findSeparator(byte[]b) {
for (int i = 0; i < b.length; i++) {
if (b[i] == ': ') {
return i;
}
}
return 0;
}
}

public static void main(String[]s) throws IOException{
new Receiver(4040).receiveTo( "c:/ ");
}
}

/************************************************************************/

import java.net.Socket;
import java.io.*;
import java.net.*;

public class Sender {
private Socket socket = null;

private InputStream in = null;

private OutputStream out = null;

private String host;

private int port;

public Sender(String host, int port) {
this.host = host;
this.port = port;
}

public void connect() throws Exception {