java-Socket文件上传/进度条 (一)

2014-11-24 07:28:25 · 作者: · 浏览: 0
客户端代码:

1、客户端运行程序:
package wtb.khd;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class KHD{
private boolean isnoClose = true;
public boolean isIsnoClose() {
return isnoClose;
}
public void setIsnoClose(boolean isnoClose) {
this.isnoClose = isnoClose;
}
public static void main(String args[]){
final KHD khd = new KHD();
String filePath = "D:\\文件.zip";
khd.setIsnoClose(true);
File upload = new File(filePath);
long fileLength = upload.length();
String fileName = upload.getName();
try {
Socket client = null;
client = new Socket("localhost",8888); //"localhost"--->服务器端IP
OutputStream out = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeUTF("文件名前缀_"+fileName);
dos.writeLong(fileLength);
FileInputStream fis = new FileInputStream(upload);
byte[] buffer = new byte[2048];
int len = 0;
double scbl = 0; //文件上传比例
int scdx = 0; //文件上传大小
final JinDuBar jdb = new JinDuBar("文件上传",System.getProperty("user.dir")+" \\bin\\image\\jindutiao.gif");
String[] uploadInfo = new String[]{"正在上传文件:" + filePath, "文件名称:"+ fileName,
"上传文件大小:"+fileLength+"字节", "已上传:0字节", "上传比例:0%"};
JLabel[] labels = jdb.getLabels();
for(int lxb = 0; lxb labels[lxb].setText(uploadInfo[lxb]);
}
JProgressBar p = jdb.getProgress();
JButton closeBtn = jdb.getCloseBtn();

closeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jdb.dispose();
khd.setIsnoClose(false);
}
});
while ((len = fis.read(buffer)) > 0 && khd.isnoClose) {
Thread.sleep(100);
scdx += len;
scbl = (double)scdx/fileLength;
int bl = (int)(scbl*100);
dos.write(buffer, 0, len);
p.setValue(bl);
p.setString(bl+"%");
String[] gxsc = new String[]{"已上传:"+scdx+"字节", "上传比例:"+bl+"%"};
for(int lxb = 0; lxb labels[lxb+3].setText(gxsc[lxb]);
}
}
jdb.dispose();
out.close();
fis.close();
client.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

/****************************/
2、进度条
package wtb.util;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
@SuppressWarnings("serial")
public class JinDuBar extends JWindow{
private JProgressBar progress;
private JButton btn;
private JLabel[] labels = new JLabel[5];
private JButton closeBtn;
public JProgressBar getProgress() {
return progress;
}
public void setProgress(JProgressBar progress) {
this.progress = progress;
}
public JButton getBtn() {
return btn;
}
public void setBtn(JButton btn) {
this.btn = btn;
}
public JLabel[] getLabels() {
return labels;
}
public void setLabels(JLabel[] labels) {
this.labels = labels;
}
public JButton getCloseBtn() {
return closeBtn;