多文件同时上传(Thread)+上传对象

实现在一台服务器上同时接受多个文件上传

等到前端的时候实现能上传各种类型的文件

记得抛出异常啊!!!!!!!

服务器的测试代码:
步骤:
实例化ServerSocket(服务器套接字)
获取连接Socket s=ss.accept(); 得到s对象
传入服务器run方法
启动Thread

public class Test_SerThread {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ServerSocket ss=null;
		int i=1;
		while (true){
			try {
				ss=new ServerSocket(Global.S_PORT);
				Socket s=ss.accept();
				System.out.println("number:"+i++);
				new Thread(new SerThread(s)).start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(ss!=null)
					try {
						ss.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
			
		}
	}
}

服务器代码:
通过构造方法传入s
网络-》内存(Input)
内存-》服务器本地文件
s.InptStream();是关键
关闭s输入流

public class SerThread implements Runnable {
	Socket s;
	public void run() {
		while(true){
		String filename="as/"+System.currentTimeMillis()+"A";
		try(BufferedInputStream bi = new BufferedInputStream(s.getInputStream());
				BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream(filename+".txt"));
				) {
				int len =0;
			byte []bs=new byte[1024];
			while((len=bi.read(bs))!=-1){
				bo.write(bs);
				bo.flush();
			}
			s.shutdownInput();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}}
	public SerThread(Socket s) {
		super();
		this.s = s;
	}
	
}

客户上传代码:(复制多个class 同时运行即可)
新建Socket
文件-》内存
内存-》网络(Out)
关闭s输出流

public class Clients {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Socket s=new Socket(Global.S_IP,Global.S_PORT);
			BufferedInputStream bi=new BufferedInputStream(new FileInputStream("a.txt"));
			BufferedOutputStream bo=new BufferedOutputStream(s.getOutputStream());
			int len =0;
			byte[] bs=new byte[1024];
			while((len=bi.read(bs))!=-1){
				bo.write(bs);
				bo.flush();
			}
			s.shutdownOutput();
			
		
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

对象上传

对象上传时要先序列化,
学生类:实现了序列化接口Serializable,有序列化id,姓名,年龄
transient :该关键字可以防止属性序列化


public class Student implements Serializable {
	private static final long serialVersionUID = 1643962259327257218L;
	private String name;
	//private transient Integer age;
	private  Integer age;
	public Student() {
		super();
	}

	public Student(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
}

服务器:

/*
 * 服务器端从网络中反序列还原学生类
 */
public class ServerDemo {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		ServerSocket ss = new ServerSocket(Global.SERVER_PORT);
		
		Socket s = ss.accept();
		InputStream is = s.getInputStream();
		ObjectInputStream ois = new ObjectInputStream(is);
		
		Object obj = ois.readObject();
		if (obj instanceof Student) {
			Student stu = (Student) obj;
			System.out.println(stu.getName() + "|" + stu.getAge());
		}
		
		ss.close();
	}
}

用户:

/*
 * 向服务器写一个学生类 (序列化)
 */
public class ClientDemo {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket s = new Socket(Global.SERVER_IP, Global.SERVER_PORT);
		
		OutputStream os = s.getOutputStream();
		
		ObjectOutputStream oos = new ObjectOutputStream(os);
		
		oos.writeObject(new Student("隔壁老王", 30));
		
		s.close();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值