J2SE通过TCPIP协议实现断点续传上传实现

服务器代码:

FileServer.java

package cn.itcast.net.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.itcast.utils.StreamTool;

public class FileServer {
	 private ExecutorService executorService;//线程池
	 private int port;//监听端口
	 private boolean quit = false;//退出
	 private ServerSocket server;
	 private Map<Long, FileLog> datas = new HashMap<Long, FileLog>();//存放断点数据
	 
	 public FileServer(int port){
		 this.port = port;
		 //创建线程池,池中具有(cpu个数*50)条线程
		 executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 50);
	 }
	 /**
	  * 退出
	  */
	 public void quit(){
		this.quit = true;
		try {
			server.close();
		} catch (IOException e) {
		}
	 }
	 /**
	  * 启动服务
	  * @throws Exception
	  */
	 public void start() throws Exception{
		 server = new ServerSocket(port);
		 while(!quit){
	         try {
	           Socket socket = server.accept();
	           //为支持多用户并发访问,采用线程池管理每一个用户的连接请求
	           executorService.execute(new SocketTask(socket));
	         } catch (Exception e) {
	             e.printStackTrace();
	         }
	     }
	 }
	 
	 private final class SocketTask implements Runnable{
		private Socket socket = null;
		public SocketTask(Socket socket) {
			this.socket = socket;
		}
		
		@Override
		public void run() {
			try {
				System.out.println("accepted connection "+ socket.getInetAddress()+ ":"+ socket.getPort());
				PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
				//得到客户端发来的第一行协议数据:Content-Length=143253434;filename=xxx.3gp;sourceid=
				//如果用户初次上传文件,sourceid的值为空。
				String head = StreamTool.readLine(inStream);
				System.out.println(head);
				if(head!=null){
					//下面从协议数据中提取各项参数值
					String[] items = head.split(";");
					String filelength = items[0].substring(items[0].indexOf("=")+1);
					String filename = items[1].substring(items[1].indexOf("=")+1);
					String sourceid = items[2].substring(items[2].indexOf("=")+1);		
					long id = System.currentTimeMillis();//生产资源id,如果需要唯一性,可以采用UUID
					FileLog log = null;
					if(sourceid!=null && !"".equals(sourceid)){
						id = Long.valueOf(sourceid);
						log = find(id);//查找上传的文件是否存在上传记录
					}
					File file = null;
					int position = 0;
					if(log==null){//如果上传的文件不存在上传记录,为文件添加跟踪记录
						String path = new SimpleDateFormat("yyyy/MM/dd/HH/mm").format(new Date());
						File dir = new File("file/"+ path);
						if(!dir.exists()) dir.mkdirs();
						file = new File(dir, filename);
						if(file.exists()){//如果上传的文件发生重名,然后进行改名
							filename = filename.substring(0, filename.indexOf(".")-1)+ dir.listFiles().length+ filename.substring(filename.indexOf("."));
							file = new File(dir, filename);
						}
						save(id, file);
					}else{// 如果上传的文件存在上传记录,读取上次的断点位置
						file = new File(log.getPath());//从上传记录中得到文件的路径
						if(file.exists()){
							File logFile = new File(file.getParentFile(), file.getName()+".log");
							if(logFile.exists()){
								Properties properties = new Properties();
								properties.load(new FileInputStream(logFile));
								position = Integer.valueOf(properties.getProperty("length"));//读取断点位置
							}
						}
					}
					
					OutputStream outStream = socket.getOutputStream();
					String response = "sourceid="+ id+ ";position="+ position+ "\r\n";
					//服务器收到客户端的请求信息后,给客户端返回响应信息:sourceid=1274773833264;position=0
					//sourceid由服务生成,唯一标识上传的文件,position指示客户端从文件的什么位置开始上传
					outStream.write(response.getBytes());
					
					RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd");
					if(position==0) fileOutStream.setLength(Integer.valueOf(filelength));//设置文件长度
					fileOutStream.seek(position);//移动文件指定的位置开始写入数据
					byte[] buffer = new byte[1024];
					int len = -1;
					int length = position;
					while( (len=inStream.read(buffer)) != -1){//从输入流中读取数据写入到文件中
						fileOutStream.write(buffer, 0, len);
						length += len;
						Properties properties = new Properties();
						properties.put("length", String.valueOf(length));
						FileOutputStream logFile = new FileOutputStream(new File(file.getParentFile(), file.getName()+".log"));
						properties.store(logFile, null);//实时记录文件的最后保存位置
						logFile.close();
					}
					if(length==fileOutStream.length()) delete(id);
					fileOutStream.close();					
					inStream.close();
					outStream.close();
					file = null;
					
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
	            try {
	                if(socket!=null && !socket.isClosed()) socket.close();
	            } catch (IOException e) {}
	        }
		}
	 }
	 
	 public FileLog find(Long sourceid){
		 return datas.get(sourceid);
	 }
	 //保存上传记录
	 public void save(Long id, File saveFile){
		 //日后可以改成通过数据库存放
		 datas.put(id, new FileLog(id, saveFile.getAbsolutePath()));
	 }
	 //当文件上传完毕,删除记录
	 public void delete(long sourceid){
		 if(datas.containsKey(sourceid)) datas.remove(sourceid);
	 }
	 
	 private class FileLog{
		private Long id;
		private String path;
		public Long getId() {
			return id;
		}
		public void setId(Long id) {
			this.id = id;
		}
		public String getPath() {
			return path;
		}
		public void setPath(String path) {
			this.path = path;
		}
		public FileLog(Long id, String path) {
			this.id = id;
			this.path = path;
		}	
	 }

}

ServerWindow.java

package cn.itcast.net.server;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class ServerWindow extends Frame{
	private FileServer s = new FileServer(7878);
	private Label label;
	
	public ServerWindow(String title){
		super(title);
		label = new Label();
		add(label, BorderLayout.PAGE_START);
		label.setText("服务器已经启动");
		this.addWindowListener(new WindowListener() {
			@Override
			public void windowOpened(WindowEvent e) {
				new Thread(new Runnable() {			
					@Override
					public void run() {
						try {
							s.start();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}).start();
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				 s.quit();
				 System.exit(0);
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
			}
		});
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ServerWindow window = new ServerWindow("文件上传服务端"); 
		window.setSize(300, 300); 
		window.setVisible(true);
	}

}


客户端代码:

SocketClient.java

package cn.itcast.net.client;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.Socket;

import cn.itcast.utils.StreamTool;

public class SocketClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {                    
		//	Socket socket = new Socket("127.0.0.1", 7878);
			Socket socket = new Socket("127.0.0.1", 7878);
            OutputStream outStream = socket.getOutputStream();            
            String filename = "QQWubiSetup.exe";
            File file = new File(filename);
            String head = "Content-Length="+ file.length() + ";filename="+ filename + ";sourceid=1278916111468\r\n";
            outStream.write(head.getBytes());
            
            PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());	
			String response = StreamTool.readLine(inStream);
            System.out.println(response);
            String[] items = response.split(";");
			String position = items[1].substring(items[1].indexOf("=")+1);
			
			RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
			fileOutStream.seek(Integer.valueOf(position));
			byte[] buffer = new byte[1024];
			int len = -1;
			int i = 0;
			while( (len = fileOutStream.read(buffer)) != -1){
				outStream.write(buffer, 0, len);
				i++;
				//if(i==10) break;
			}
			fileOutStream.close();
			outStream.close();
            inStream.close();
            socket.close();
        } catch (Exception e) {                    
            e.printStackTrace();
        }

	}
	/**
	* 读取流
	* @param inStream
	* @return 字节数组
	* @throws Exception
	*/
	public static byte[] readStream(InputStream inStream) throws Exception{
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = -1;
			while( (len=inStream.read(buffer)) != -1){
				outSteam.write(buffer, 0, len);
			}
			outSteam.close();
			inStream.close();
			return outSteam.toByteArray();
	}
}


 项目源码下载:http://pan.baidu.com/share/link?shareid=267312&uk=1796216265

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JCreator是一种Java集成开发环境(IDE),用于开发和编写J2SE(Java 2平台标准版)应用程序。它是基于Windows平台的,为Java开发者提供了一个友好的界面和一系列方便的功能。 首先,JCreator提供了一个简单易用的界面,使得开发者能够轻松地创建、编辑和管理Java项目。它具有类似于其他流行IDE的布局,包括编辑器窗口、项目导航器、控制台等。 其次,JCreator具有许多有用的功能。例如,它支持自动代码补全功能,可以在编写代码时快速显示和选择可能的选项。它还具有代码重构功能,可以帮助开发者重命名、提取方法、移动代码块等,提高代码的可维护性。 此外,JCreator还提供了调试功能,允许开发者在运行时跟踪和调试代码。它具有断点设置、变量监视和运行时错误报告等功能,帮助开发者找到和修复潜在的错误。 最后,JCreator与J2SE完全兼容。它支持最新的Java语法和标准库,开发者可以使用JCreator开发各种类型的应用程序,包括控制台程序、图形界面应用和Web应用。 综上所述,JCreator是一个功能强大、易于使用的IDE,适用于开发和编写J2SE应用程序。它提供了许多有用的功能和工具,可以提高开发效率,并帮助开发者在Java开发过程中更轻松地进行调试和测试。无论是初学者还是经验丰富的开发者,都可以从JCreator中受益,并快速开发出高质量的Java应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值