Android socket 文件传输: 用于app更新,大文件上传下载

<span style="font-size:24px;">客户端上传文件</span>
/**
 * 	
 * @author lijp8
 * @input  intent 待传输文件的地址
 *	@function 将文件传输到服务器,并返回传输状态
 */
	class MyThread extends Thread{

		private Intent intentThread;
		private Socket socket = null;	
		private ArrayList<AttendencePerson> personArray =new ArrayList<AttendencePerson>(); 
		//选择要传输的文件
		//private String filePath= null;
		File fi= new File(DateBaseActivity.dbFilePath);
		
		
		public MyThread(Intent intent){
			this.intentThread= intent;
		}
		@Override
		public void run(){
            //定义消息  
            Message msg = new Message();  
            msg.what = 0x11;  
            Bundle bundle = new Bundle();  
            bundle.clear();  
            try {  
                //连接服务器 并设置连接超时为5秒  
                socket = new Socket();  
                socket.connect(new InetSocketAddress(DateBaseActivity.IP, 8888), 5000);  
                //获取输出流  ,   向服务器发送信息  
                
                DataOutputStream out=new DataOutputStream(socket.getOutputStream());  //向服务器发送消息
                DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(DateBaseActivity.dbFilePath)));
                out.writeUTF(fi.getName());
				out.flush();
				out.writeLong((long)fi.length());
				out.flush();
				byte[] buf= new byte[(int)fi.length()];
				while(true){
					int read =0;
						if(fis!=null)
						{ 
							read=fis.read(buf);
						}else{
							System.out.println("文件传输错误");
							break;
						}

					if(read ==-1){
						break;
					}

					out.write(buf, 0, read);
					
				}
				out.flush();
				System.out.println("文件传输完成");
                 
				//获取输入流  ,   接受服务器的信息  
				DataInputStream in = new DataInputStream(socket.getInputStream()); // 接收来自服务器的消息
				String readMsg = in.readUTF();
				
                bundle.putString("msg", readMsg);  
                msg.setData(bundle);  
                //发送消息 修改UI线程中的组件  
                myHandler.sendMessage(msg);  
                //关闭各种输入输出流  
                in.close();  
                out.close();  
                socket.close();  
            } catch (SocketTimeoutException aa) {  
                //连接超时 在UI界面显示消息  
                bundle.putString("msg", "服务器连接失败!请检查网络是否打开");  
                msg.setData(bundle);  
                //发送消息 修改UI线程中的组件  
                myHandler.sendMessage(msg);  
            } catch (IOException e) {  
                e.printStackTrace();  
                Log.w("ljp", e.getClass().getName() + ": " + e.getMessage());     
              //连接超时 在UI界面显示消息  
                bundle.putString("msg", e.getClass().getName() + ": " + e.getMessage());  
                msg.setData(bundle);  
                //发送消息 修改UI线程中的组件  
                myHandler.sendMessage(msg);  
            }  
        }   
	}
	}


<span style="font-size:18px;">服务端下载程序</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;"></span><pre name="code" class="java">	class ServerDatabaseThread extends Thread{
		public void run() {
			try {
				
				//当前时间
				Date day=new Date();
				SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
				
				
				ServerSocket ss=new ServerSocket(8888); 创建一个ServerSocket对象,并让这个ServerSocket在8888端口监听				
						
				int i=1;
				while(true){
					Socket socket=ss.accept(); //调用ServerSocket的accept()方法,接受客户端所发送的请求,如果客户端没有发送数据,那么该线程就停滞不继续


				try {
						//ObjectInputStream in=new ObjectInputStream(socket.getInputStream());//接收客户端信息
						DataInputStream in=new DataInputStream(socket.getInputStream());//接收客户端信息
						DataOutputStream out=new DataOutputStream(socket.getOutputStream());  //向客户端发送消息
						String savePath= "f:/test/"+df.format(day);
						long fileLen=0;
						int passedLen =0; // 传输进度
						
						savePath += in.readUTF();   //读取数据库文件名
						fileLen = in.readLong(); //文件长度
						System.out.println("数据库文件大小:"+fileLen);
						
						byte[] buf= new byte[(int)fileLen];
						DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
						while(true){
							int read =0;
							if(passedLen>=fileLen)
								break;
							
								if(in!=null)
								{ 
									read=in.read(buf);
								}else{
									System.out.println("文件传输错误");
									//out.writeUTF("文件传输错误");
									//out.flush();
									break;
								}
							passedLen+=read;
							if(read ==-1){
								break;
							}
							System.out.println( "文件传输进度为"+(passedLen*100/fileLen)+ "%");
							fileOut.write(buf, 0, read);
							
						}
						System.out.println("文件传输完成");
						passedLen=0;
						fileOut.close();
						

						out.writeUTF("数据库上传完成");
						out.flush();
						
						
						
				
						
						in.close();   //关闭流
						out.close();//关闭流
						
						socket.close();//关闭打开的socket
						
					} catch (Exception e) {
						System.out.println(e.getMessage());
					}
				}
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CVchina_BUAA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值