文件分发,多线程学的不好,求思路 求大神指教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 delivery 一个文件,同时发送到若干台服务器上 还是给你写了段代码,你参考一下吧:package com.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class Test { public static byte[] context = new byte[0]; public static void main(String[] args) { File file = new File("E:/IPOP V3.6帮助文档.doc"); readFile(file); String fileName = file.getName(); WriterClient client1 = new WriterClient("E:/client/1/", fileName); WriterClient client2 = new WriterClient("E:/client/2/", fileName); client1.start(); client2.start(); } public static void readFile(File file) { InputStream in = null; try { in = new FileInputStream(file); int i = 0; byte[] buf = new byte[1024 * 4]; while((i=in.read(buf)) != -1){ byte[] newCxt = new byte[context.length + i]; System.arraycopy(context, 0, newCxt, 0, context.length); System.arraycopy(buf, 0, newCxt, context.length, i); context = newCxt; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }}class WriterClient extends Thread{ private String address; private String fileName; public WriterClient(String address, String fileName){ this.address = address; this.fileName = fileName; } @Override public void run(){ File file = new File(address, fileName); OutputStream out = null; try { out = new FileOutputStream(file); out.write(Test.context); out.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }} 你怎么分发? 不还是要分发到 目标服务器的 ftp 或sftp 或者硬盘目录被队列调用 被 有什么疑惑? 如果这样的话就不好处理了。如果传的文件比较多的话可以这样实现:找一个中心服务器,管理十台或多台机器,这十几台机器上都有一个文件分发的程序,如果某个机器收到一个文件,它就会把这个文件分发到其它机器上,中心服务器分别管理这几台机器的文件分配工作。这样中心服务器负载不就小多了。比如中心服务器是0,下边管理了1-10个机器,这10个机器,现在有a-z总共26文件要分发,可以把a-c 分给1号,d-f 分给2号,。y-z 分给10号,然后后它们收到文件后,分别把文件发给:1-100这几台机器。这样中心服务器就没多大负载了。如果是只有一个大文件也可以考虑,上边的这种模式。只不过是,中心服务器把程序发到它下边管理的十台或多台机器上,这几台机器只分发给某几个固定的机器。从理论上分析,一个机器不管是单线程还是多线程总共的流量是不变的。只有把这个任务交给多台机器才是最好的。我认为这种问题没有一个最好的答案,要结合自己的业务不断的作调整。才可能找到更适合的。 请教 struts2 如何设置session过期时间呢? 如何使用jxl复制excel中的一行或者多行 lucene 2.9获取得分? 请人帮我做一个CRUD的java web 程序,1-3个月,兼职 如何能调试ejb? 表单中怎样传递<select>标签中的数据 ---求教 axis namespase 问题 Struts中表单提交问题 过路的大侠,请问struts中的plug-in标记怎么用的 郁闷,散分散分 正则表达式截取字符串 ssh 框架整合
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class Test {
public static byte[] context = new byte[0];
public static void main(String[] args) {
File file = new File("E:/IPOP V3.6帮助文档.doc");
readFile(file);
String fileName = file.getName();
WriterClient client1 = new WriterClient("E:/client/1/", fileName);
WriterClient client2 = new WriterClient("E:/client/2/", fileName);
client1.start();
client2.start();
}
public static void readFile(File file) {
InputStream in = null;
try {
in = new FileInputStream(file);
int i = 0;
byte[] buf = new byte[1024 * 4];
while((i=in.read(buf)) != -1){
byte[] newCxt = new byte[context.length + i];
System.arraycopy(context, 0, newCxt, 0, context.length);
System.arraycopy(buf, 0, newCxt, context.length, i);
context = newCxt;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}class WriterClient extends Thread{
private String address;
private String fileName;
public WriterClient(String address, String fileName){
this.address = address;
this.fileName = fileName;
}
@Override
public void run(){
File file = new File(address, fileName);
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(Test.context);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
比如中心服务器是0,下边管理了1-10个机器,这10个机器,现在有a-z总共26文件要分发,可以把a-c 分给1号,d-f 分给2号,。y-z 分给10号,然后后它们收到文件后,分别把文件发给:1-100这几台机器。这样中心服务器就没多大负载了。
如果是只有一个大文件也可以考虑,上边的这种模式。只不过是,中心服务器把程序发到它下边管理的十台或多台机器上,这几台机器只分发给某几个固定的机器。
从理论上分析,一个机器不管是单线程还是多线程总共的流量是不变的。只有把这个任务交给多台机器才是最好的。我认为这种问题没有一个最好的答案,要结合自己的业务不断的作调整。才可能找到更适合的。