1. 程式人生 > >java檔案斷點續傳的簡單實現

java檔案斷點續傳的簡單實現

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;


public class ReceiveFileThread extends Thread{
	
	private ServerSocket connectSocket=null;
	private Socket socket=null;
	private JFrame frame;
	private Container contentPanel;
	private JProgressBar progressbar;
	private DataInputStream dis;
	private DataOutputStream dos;
	private RandomAccessFile rad;
	private JLabel label;
	
	public ReceiveFileThread(){
		frame=new JFrame("接收檔案");
		try {
			connectSocket=new ServerSocket(8080);
			socket=connectSocket.accept();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void run(){
		try {
			dis=new DataInputStream(socket.getInputStream());
			dos=new DataOutputStream(socket.getOutputStream());
			dis.readUTF();
			
			int permit=JOptionPane.showConfirmDialog(frame, "是否接收檔案","檔案傳輸請求:", JOptionPane.YES_NO_OPTION);
			if (permit==JOptionPane.YES_OPTION) {
				String filename=dis.readUTF();
				dos.writeUTF("ok");
				dos.flush();
				File file=new File(filename+".temp");

				rad=new RandomAccessFile(filename+".temp", "rw");
				
				//獲得檔案大小
				long size=0;
				if(file.exists()&&file.isFile()){
					size=file.length();
				}
				
				dos.writeLong(size);//傳送已接收的大小
				dos.flush();
				long allSize=dis.readLong();
				String rsp=dis.readUTF();
				
				int barSize=(int)(allSize/1024);
				int barOffset=(int)(size/1024);							
				
				//傳輸介面
				frame.setSize(300,120);
				contentPanel =frame.getContentPane();
				contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
				progressbar = new JProgressBar();//進度條
				
				label=new JLabel(filename+" 接收中");
				contentPanel.add(label);
				
				progressbar.setOrientation(JProgressBar.HORIZONTAL);
				progressbar.setMinimum(0);
				progressbar.setMaximum(barSize);
				progressbar.setValue(barOffset);
			    progressbar.setStringPainted(true);
			    progressbar.setPreferredSize(new Dimension(150, 20));
			    progressbar.setBorderPainted(true);
			    progressbar.setBackground(Color.pink);
				
			    JButton cancel=new JButton("取消");
			    
			    JPanel barPanel=new JPanel();
			    barPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
			    
			    barPanel.add(progressbar);
			    barPanel.add(cancel);
			    
			    contentPanel.add(barPanel);
			    
			    cancel.addActionListener(new cancelActionListener());
			    
			    frame.setDefaultCloseOperation(
			    		JFrame.EXIT_ON_CLOSE);
			    frame.setVisible(true);
			    
			    //接收檔案
				if (rsp.equals("ok")) {
					rad.seek(size);
					int length;
					byte[] buf=new byte[1024];
					while((length=dis.read(buf, 0, buf.length))!=-1){
						rad.write(buf,0,length);
						progressbar.setValue(++barOffset);
					}
					System.out.println("end");
				}
				
				label.setText(filename+" 結束接收");
				
				
				dis.close();
				dos.close();
				rad.close();
				frame.dispose();
				//檔案重新命名
				if (barOffset>=barSize) {
					file.renameTo(new File(filename));
				}
				
			}else{
				dis.close();
				dos.close();
				frame.dispose();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			label.setText(" 已取消接收,連線關閉!");		
		}finally {
			frame.dispose();
		}
	}
	
	class cancelActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			try {
				dis.close();
				dos.close();
				rad.close();
				JOptionPane.showMessageDialog(frame, "已取消接收,連線關閉!", "提示:", JOptionPane.INFORMATION_MESSAGE);	
				label.setText(" 取消接收,連線關閉");
			} catch (IOException e1) {
				
			}
		}
	}
	
}
接下來我們來測試一下,下面分別是檔案接收方與傳送方的測試程式碼: