Java 如何跟踪writeObject()进程
我尝试了以下方法: 但它甚至没有打印出一个数字:/ 文件大小为700mb,大约需要15秒 如果我理解正确的话,第二个类应该连接到outputstream 而我在其他任何地方都找不到解决办法 问题:我无法跟踪writeObject的进程-我正在尝试使用progressbar来显示有多少文件已上载Java 如何跟踪writeObject()进程,java,tracking,progress,objectoutputstream,Java,Tracking,Progress,Objectoutputstream,我尝试了以下方法: 但它甚至没有打印出一个数字:/ 文件大小为700mb,大约需要15秒 如果我理解正确的话,第二个类应该连接到outputstream 而我在其他任何地方都找不到解决办法 问题:我无法跟踪writeObject的进程-我正在尝试使用progressbar来显示有多少文件已上载 public class Main { public static void main(String[] args) throws Exception { Stri
public class Main {
public static void main(String[] args) throws Exception {
String file_name = "M:\\Config\\huge.avi";
File file = new File(file_name);
System.out.println(file.getPath());
if(file.exists()){
Socket socket = new Socket("localhost", 4112);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
FileInputStream fis = new FileInputStream(file);
MyCountingStream mcs = new MyCountingStream(oos);
oos.writeObject(file.getName());
byte [] buffer = new byte[(int)file.length()];
Integer bytesRead = 0;
Timeline aftertickz = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
System.out.println(mcs.bytesWrittenSoFar());
}
}));
aftertickz.setCycleCount(Timeline.INDEFINITE);
aftertickz.play();
while ((bytesRead = fis.read(buffer)) > 0) {
oos.writeObject(bytesRead);
oos.flush();
oos.writeObject(Arrays.copyOf(buffer, buffer.length));
}
oos.close();
ois.close();
mcs.close();
fis.close();
socket.close();
System.exit(0);
}else{
System.out.println("File doesn't exist");
System.exit(0);
}
}
}
public class MyCountingStream extends DataOutputStream {
public MyCountingStream(OutputStream out) {
super(out);
}
public int bytesWrittenSoFar() {
return written;
}
}
您可以将文件作为原始字节流发送,而不是使用writeObject,然后跟踪发送的字节数/文件大小(字节数*100),四舍五入到最接近的整数 或者。。。。。 看看:
请详细说明您的问题可能已经忘记了,因为它现在在顶部。我已经搜索了1个小时,无法处理它。。你能给我一个你的原始字节流的例子吗?我不会写实际的代码,我会告诉你什么搜索会给你这个概念。搜索类似java的内容,跟踪下载进度。