Android-使用TCP/IP协议实现断点上传(客户端-服务端代码)(+0)

6 篇文章 0 订阅
1 篇文章 0 订阅
  1. public class ServerWindow extends Frame  
  2. {  
  3.     private FileServer s = new FileServer(7878);  
  4.     private Label label;  
  5.       
  6.     public ServerWindow(String title)  
  7.     {  
  8.         super(title);  
  9.         label = new Label();  
  10.         add(label, BorderLayout.PAGE_START);  
  11.         label.setText("服务器已经启动");  
  12.         this.addWindowListener(new WindowListener()   
  13.         {  
  14.             @Override  
  15.             public void windowOpened(WindowEvent e)   
  16.             {  
  17.                 new Thread(new Runnable()  
  18.                 {             
  19.                     @Override  
  20.                     public void run()   
  21.                     {  
  22.                         try   
  23.                         {  
  24.                             s.start();  
  25.                         }  
  26.                         catch (Exception e)   
  27.                         {  
  28.                             e.printStackTrace();  
  29.                         }  
  30.                     }  
  31.                 }).start();  
  32.             }  
  33.               
  34.             @Override  
  35.             public void windowIconified(WindowEvent e) {  
  36.             }  
  37.               
  38.             @Override  
  39.             public void windowDeiconified(WindowEvent e) {  
  40.             }  
  41.               
  42.             @Override  
  43.             public void windowDeactivated(WindowEvent e) {  
  44.             }  
  45.               
  46.             @Override  
  47.             public void windowClosing(WindowEvent e) {  
  48.                  s.quit();  
  49.                  System.exit(0);  
  50.             }  
  51.               
  52.             @Override  
  53.             public void windowClosed(WindowEvent e) {  
  54.             }  
  55.               
  56.             @Override  
  57.             public void windowActivated(WindowEvent e) {  
  58.             }  
  59.         });  
  60.     }  
  61.     /** 
  62.      * @param args 
  63.      */  
  64.     public static void main(String[] args)   
  65.     {  
  66.         ServerWindow window = new ServerWindow("文件上传服务端");   
  67.         window.setSize(300300);   
  68.         window.setVisible(true);  
  69.     }  
  70. }   

 
  1. public class SocketClient   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         try   
  6.         {     
  7.             //这里的套接字根据实际服务器更改   
  8.             Socket socket = new Socket("127.0.0.1"7878);  
  9.             OutputStream outStream = socket.getOutputStream();              
  10.             String filename = "QQWubiSetup.exe";  
  11.             File file = new File(filename);  
  12.             //构造上传文件头,上传的时候会判断上传的文件是否存在,是否存在上传记录   
  13.             //若是不存在则服务器会自动生成一个id,给客户端返回   
  14.             String head = "Content-Length="+ file.length() + ";filename="+ filename + ";sourceid=1278916111468/r/n";  
  15.             outStream.write(head.getBytes());  
  16.               
  17.             PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());      
  18.             String response = StreamTool.readLine(inStream);  
  19.             System.out.println(response);  
  20.             String[] items = response.split(";");  
  21.             //构造开始上传文件的位置   
  22.             String position = items[1].substring(items[1].indexOf("=")+1);  
  23.             //以读的方式开始访问   
  24.             RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");  
  25.             fileOutStream.seek(Integer.valueOf(position));  
  26.             byte[] buffer = new byte[1024];  
  27.             int len = -1;  
  28.             int i = 0;  
  29.             while( (len = fileOutStream.read(buffer)) != -1)  
  30.             {  
  31.                 outStream.write(buffer, 0, len);  
  32.                 i++;  
  33.                 //if(i==10) break;   
  34.             }  
  35.             fileOutStream.close();  
  36.             outStream.close();  
  37.             inStream.close();  
  38.             socket.close();  
  39.         }   
  40.         catch (Exception e)   
  41.         {                      
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45.     /** 
  46.     * 读取流 
  47.     * @param inStream 
  48.     * @return 字节数组 
  49.     * @throws Exception 
  50.     */  
  51.     public static byte[] readStream(InputStream inStream) throws Exception  
  52.     {  
  53.             ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  54.             byte[] buffer = new byte[1024];  
  55.             int len = -1;  
  56.             while( (len=inStream.read(buffer)) != -1)  
  57.             {  
  58.                 outSteam.write(buffer, 0, len);  
  59.             }  
  60.             outSteam.close();  
  61.             inStream.close();  
  62.             return outSteam.toByteArray();  
  63.     }  
  64. }   

 
  1. public class StreamTool   
  2. {  
  3.        
  4.      public static void save(File file, byte[] data) throws Exception   
  5.      {  
  6.          FileOutputStream outStream = new FileOutputStream(file);  
  7.          outStream.write(data);  
  8.          outStream.close();  
  9.      }  
  10.        
  11.      public static String readLine(PushbackInputStream in) throws IOException   
  12.      {  
  13.             char buf[] = new char[128];  
  14.             int room = buf.length;  
  15.             int offset = 0;  
  16.             int c;  
  17.      loop:  while (true) {  
  18.                 switch (c = in.read())   
  19.                 {  
  20.                     case -1:  
  21.                     case '/n':  
  22.                         break loop;  
  23.                     case '/r':  
  24.                         int c2 = in.read();  
  25.                         if ((c2 != '/n') && (c2 != -1)) in.unread(c2);  
  26.                         break loop;  
  27.                     default:  
  28.                         if (--room < 0) {  
  29.                             char[] lineBuffer = buf;  
  30.                             buf = new char[offset + 128];  
  31.                             room = buf.length - offset - 1;  
  32.                             System.arraycopy(lineBuffer, 0, buf, 0, offset);  
  33.                              
  34.                         }  
  35.                         buf[offset++] = (char) c;  
  36.                         break;  
  37.                 }  
  38.             }  
  39.             if ((c == -1) && (offset == 0)) return null;  
  40.             return String.copyValueOf(buf, 0, offset);  
  41.     }  
  42.        
  43.     /** 
  44.     * 读取流 
  45.     * @param inStream 
  46.     * @return 字节数组 
  47.     * @throws Exception 
  48.     */  
  49.     public static byte[] readStream(InputStream inStream) throws Exception  
  50.     {  
  51.             ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  52.             byte[] buffer = new byte[1024];  
  53.             int len = -1;  
  54.             while( (len=inStream.read(buffer)) != -1){  
  55.                 outSteam.write(buffer, 0, len);  
  56.             }  
  57.             outSteam.close();  
  58.             inStream.close();  
  59.             return outSteam.toByteArray();  
  60.     }  
  61. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值