读书人

一个关于wmv格式文件的传递解决方法

发布时间: 2012-02-14 19:19:19 作者: rapoo

一个关于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 {


socket = new Socket(host, port);
out = socket.getOutputStream();
in = socket.getInputStream();

System.out.println( "Connection Success! ");
}



public void send(String filePath) throws Exception {
File file = new File(filePath);

FileInputStream fin = new FileInputStream(file);

// send fileName and separator ': '
out.write((file.getName()+ ": ").getBytes());

byte[] buff = new byte[4096];
int length = 0;
while ((length = fin.read(buff)) > 0) {
out.write(buff,0,length);
}
fin.close();
out.flush();
}

public void close() throws Exception {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
}

public static void main(String[] s) throws Exception{
Sender sender = new Sender( "127.0.0.1 " ,4040); // ip换成接收服务器B的ip
sender.connect();
sender.send( "d:/2.wma ");
sender.close();
}

}

读书人网 >J2SE开发

热点推荐