package com;
import java.awt.FileDialog;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Server extends JFrame {
private Socket socket;
private DataInputStream input;
private DataOutputStream output;
public Server() {
try {
ServerSocket s = new ServerSocket(8007);
socket = s.accept();
FileDialog d = new FileDialog(this, "选择发送文件", FileDialog.LOAD);
d.setVisible(true);
File file = new File(d.getDirectory() + d.getFile());
output = new DataOutputStream(socket.getOutputStream());
input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
output.writeUTF(file.getName());
output.flush();
output.writeLong((long) file.length());
output.flush();
int size = 1024000;
byte[] buf = new byte[size];
while(true){
int read = 0;
if(input != null){
read = input.read(buf);
}
if(read == -1){
break;
}
output.write(buf, 0, read);
}
output.flush();
output.close();
input.close();
socket.close();
JOptionPane.showMessageDialog(null, "发送完毕!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
}
package com;
import java.awt.FlowLayout;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class Client extends JFrame{
private Socket socket;
private DataInputStream input;
private DataOutputStream output;
private JLabel lab;
private JProgressBar bar;
public Client(){
super("正在接收文件");
this.setLayout(null);
lab = new JLabel("文件长度:");
lab.setBounds(5, 10, 150, 30);
JLabel hasOver = new JLabel("已完成");
hasOver.setBounds(5, 40, 150, 20);
bar = new JProgressBar();
bar.setBounds(5, 80, 180, 10);
bar.setMaximum(100);
bar.setValue(0);
this.add(lab);
this.add(hasOver);
this.add(bar);
this.setBounds(130, 90, 200, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
getFile();
}
public void getFile(){
try {
socket = new Socket("127.0.0.1", 8007);
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
JOptionPane.showMessageDialog(null, "开始接收文件");
String path = "E:\\";
int size = 1024000;
byte[] buf = new byte[size];
int passlen = 0;
long len = 0;
path += input.readUTF();
DataOutputStream fileout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
len = input.readLong();
lab.setText(lab.getText()+len);
while(true){
int read = 0;
if(input != null){
read = input.read(buf);
}
passlen += read;
if(read == -1){
break;
}
fileout.write(buf, 0, read);
bar.setValue((int)((double)passlen/len * 100));
}
fileout.flush();
fileout.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "文件接收完毕!");
System.exit(0);
}
public static void main(String[] args) {
new Client();
}
}
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
read = fis.read(buf);
if (-1 == read) { // 读取文件至结尾,条件判断没问题
break;
}
ps.write(buf, 0, read);
ps.flush(); // 写好之后刷新下
}
fis.close();
while (true) {
int read = 0;
read = inputStream.read(buf);
passedlen += read;
if (read == -1) { // 这个条件判断有问题!
break;
}
//下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比
// System.out.println("文件接收了" + (passedlen * 100/ len) + "%\n");
fileOut.write(buf, 0, read);
fileOut.flush();
}
// len为应该接收的字节,read为每次接收到的字节
// 应该接收的字节数减去已经接收到的字节数为0时就可以退出循环了
if (0 == (len -= read)) {
break;
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。