一个关于wmv格式文件的传递

xperiod_2008 2007-02-28 05:41:24
现在我想把一个wmv格式的文件从一台服务器复制到另外一台服务器,要用java的io来实现,谁知道该怎么做呢?怎么来进行复制?谁能给出比较详细的代码?谢谢,非常急切!
...全文
316 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xperiod_2008 2007-03-06
  • 打赏
  • 举报
回复
楼上的说说这两个类都是干什么的呢?看不明白啊,也没有注释
xperiod_2008 2007-03-02
  • 打赏
  • 举报
回复
楼上的给我代码吧
jiqi62120 2007-03-02
  • 打赏
  • 举报
回复
我试试
xperiod_2008 2007-03-02
  • 打赏
  • 举报
回复
up!
jiqi62120 2007-03-02
  • 打赏
  • 举报
回复
SaveFileThread 的 run()最后加句 s.close();
jiqi62120 2007-03-02
  • 打赏
  • 举报
回复
下面才是对的
麻烦把上面的删除吧,泄漏信息了。。。 -_-!

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

/***************************************
* 先运行位于服务器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();
}

}
xperiod_2008 2007-03-01
  • 打赏
  • 举报
回复
你帮我写一个吧,还没有搞定!谢谢楼上的兄弟!
interpb 2007-02-28
  • 打赏
  • 举报
回复
呵呵 要下班了 如果我到家你还没有搞定

就帮你写一个
xperiod_2008 2007-02-28
  • 打赏
  • 举报
回复
具体怎么实现呢?能给出详细的代码吗?分不够再开贴!
interpb 2007-02-28
  • 打赏
  • 举报
回复
用FileIn(Out)putStream来读写就可以
xperiod_2008 2007-02-28
  • 打赏
  • 举报
回复
up!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧