Android下使用TCP/IP协议实现断点上传

0.使用http协议是不能实现断点上传的,对于文件大小不一,与实际需求可以使用Socket断点上传

1.上传原理:Android客户端发送上传文件头字段给服务器,服务器判断文件是否在服务器上,文件是否有上传的记录,若是文件不存在,服务器则返回一个id(断点数据)通知客户端从什么位置开始上传,客户端开始从获得的位置开始上传文件

2.实例演示

(0)服务器端代码

  1. public class FileServer   
  2. {  
  3.      //线程池  
  4.      private ExecutorService executorService;  
  5.      //监听端口  
  6.      private int port;  
  7.      //退出  
  8.      private boolean quit = false;  
  9.      private ServerSocket server;  
  10.      //存放断点数据  
  11.      private Map<Long, FileLog> datas = new HashMap<Long, FileLog>();  
  12.        
  13.      public FileServer(int port)  
  14.      {  
  15.          this.port = port;  
  16.          //创建线程池,池中具有(cpu个数*50)条线程  
  17.          executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 50);  
  18.      }  
  19.        
  20.      /** 
  21.       * 退出 
  22.       */  
  23.      public void quit()  
  24.      {  
  25.         this.quit = true;  
  26.         try   
  27.         {  
  28.             server.close();  
  29.         }   
  30.         catch (IOException e)   
  31.         {  
  32.             e.printStackTrace();  
  33.         }  
  34.      }  
  35.        
  36.      /** 
  37.       * 启动服务 
  38.       * @throws Exception 
  39.       */  
  40.      public void start() throws Exception  
  41.      {  
  42.          //实现端口监听  
  43.          server = new ServerSocket(port);  
  44.          while(!quit)  
  45.          {  
  46.              try   
  47.              {  
  48.                Socket socket = server.accept();  
  49.                //为支持多用户并发访问,采用线程池管理每一个用户的连接请求  
  50.                executorService.execute(new SocketTask(socket));  
  51.              }   
  52.              catch (Exception e)   
  53.              {  
  54.                  e.printStackTrace();  
  55.              }  
  56.          }  
  57.      }  
  58.        
  59.      private final class SocketTask implements Runnable  
  60.      {  
  61.         private Socket socket = null;  
  62.         public SocketTask(Socket socket)   
  63.         {  
  64.             this.socket = socket;  
  65.         }  
  66.           
  67.         @Override  
  68.         public void run()   
  69.         {  
  70.             try   
  71.             {  
  72.                 System.out.println("accepted connection "+ socket.getInetAddress()+ ":"+ socket.getPort());  
  73.                 //这里的输入流PushbackInputStream可以回退到之前的某个点开始进行读取  
  74.                 PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());  
  75.                 //得到客户端发来的第一行协议数据:Content-Length=143253434;filename=xxx.3gp;sourceid=  
  76.                 //如果用户初次上传文件,sourceid的值为空。  
  77.                 String head = StreamTool.readLine(inStream);  
  78.                 System.out.println(head);  
  79.                 if(head!=null)  
  80.                 {  
  81.                     //下面从协议数据中提取各项参数值  
  82.                     String[] items = head.split(";");  
  83.                     String filelength = items[0].substring(items[0].indexOf("=")+1);  
  84.                     String filename = items[1].substring(items[1].indexOf("=")+1);  
  85.                     String sourceid = items[2].substring(items[2].indexOf("=")+1);        
  86.                     //生产资源id,如果需要唯一性,可以采用UUID  
  87.                     long id = System.currentTimeMillis();  
  88.                     FileLog log = null;  
  89.                     if(sourceid!=null && !"".equals(sourceid))  
  90.                     {  
  91.                         id = Long.valueOf(sourceid);  
  92.                         //查找上传的文件是否存在上传记录  
  93.                         log = find(id);  
  94.                     }  
  95.                     File file = null;  
  96.                     int position = 0;  
  97.                     //如果上传的文件不存在上传记录,为文件添加跟踪记录  
  98.                     if(log==null)  
  99.                     {  
  100.                         String path = new SimpleDateFormat("yyyy/MM/dd/HH/mm").format(new Date());  
  101.                         //设置存放的位置与当前应用的位置有关  
  102.                         File dir = new File("file/"+ path);  
  103.                         if(!dir.exists()) dir.mkdirs();  
  104.                         file = new File(dir, filename);  
  105.                         //如果上传的文件发生重名,然后进行改名  
  106.                         if(file.exists())  
  107.                         {  
  108.                             filename = filename.substring(0, filename.indexOf(".")-1)+ dir.listFiles().length+ filename.substring(filename.indexOf("."));  
  109.                             file = new File(dir, filename);  
  110.                         }  
  111.                         save(id, file);  
  112.                     }  
  113.                     // 如果上传的文件存在上传记录,读取上次的断点位置  
  114.                     else  
  115.                     {  
  116.                         //从上传记录中得到文件的路径  
  117.                         file = new File(log.getPath());  
  118.                         if(file.exists())  
  119.                         {  
  120.                             File logFile = new File(file.getParentFile(), file.getName()+".log");  
  121.                             if(logFile.exists())  
  122.                             {  
  123.                                 Properties properties = new Properties();  
  124.                                 properties.load(new FileInputStream(logFile));  
  125.                                 //读取断点位置  
  126.                                 position = Integer.valueOf(properties.getProperty("length"));  
  127.                             }  
  128.                         }  
  129.                     }  
  130.                       
  131.                     OutputStream outStream = socket.getOutputStream();  
  132.                     String response = "sourceid="+ id+ ";position="+ position+ "/r/n";  
  133.                     //服务器收到客户端的请求信息后,给客户端返回响应信息:sourceid=1274773833264;position=0  
  134.                     //sourceid由服务生成,唯一标识上传的文件,position指示客户端从文件的什么位置开始上传  
  135.                     outStream.write(response.getBytes());  
  136.                     //  
  137.                     RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd");  
  138.                     //设置文件长度  
  139.                     if(position==0) fileOutStream.setLength(Integer.valueOf(filelength));  
  140.                     //移动文件指定的位置开始写入数据  
  141.                     fileOutStream.seek(position);  
  142.                     byte[] buffer = new byte[1024];  
  143.                     int len = -1;  
  144.                     int length = position;  
  145.                     //从输入流中读取数据写入到文件中  
  146.                     while( (len=inStream.read(buffer)) != -1)  
  147.                     {  
  148.                         fileOutStream.write(buffer, 0, len);  
  149.                         length += len;  
  150.                         Properties properties = new Properties();  
  151.                         properties.put("length", String.valueOf(length));  
  152.                         FileOutputStream logFile = new FileOutputStream(new File(file.getParentFile(), file.getName()+".log"));  
  153.                         //实时记录文件的最后保存位置  
  154.                         properties.store(logFile, null);  
  155.                         logFile.close();  
  156.                     }  
  157.                     //如果长传长度等于实际长度则表示长传成功  
  158.                     if(length==fileOutStream.length()) delete(id);  
  159.                     fileOutStream.close();                    
  160.                     inStream.close();  
  161.                     outStream.close();  
  162.                     file = null;  
  163.                       
  164.                 }  
  165.             }  
  166.             catch (Exception e)   
  167.             {  
  168.                 e.printStackTrace();  
  169.             }  
  170.             finally  
  171.             {  
  172.                 try  
  173.                 {  
  174.                     if(socket!=null && !socket.isClosed()) socket.close();  
  175.                 }   
  176.                 catch (IOException e)  
  177.                 {  
  178.                     e.printStackTrace();  
  179.                 }  
  180.             }  
  181.         }  
  182.      }  
  183.        
  184.      public FileLog find(Long sourceid)  
  185.      {  
  186.          return datas.get(sourceid);  
  187.      }  
  188.      //保存上传记录  
  189.      public void save(Long id, File saveFile)  
  190.      {  
  191.          //日后可以改成通过数据库存放  
  192.          datas.put(id, new FileLog(id, saveFile.getAbsolutePath()));  
  193.      }  
  194.      //当文件上传完毕,删除记录  
  195.      public void delete(long sourceid)  
  196.      {  
  197.          if(datas.containsKey(sourceid)) datas.remove(sourceid);  
  198.      }  
  199.        
  200.      private class FileLog{  
  201.         private Long id;  
  202.         private String path;  
  203.         public Long getId() {  
  204.             return id;  
  205.         }  
  206.         public void setId(Long id) {  
  207.             this.id = id;  
  208.         }  
  209.         public String getPath() {  
  210.             return path;  
  211.         }  
  212.         public void setPath(String path) {  
  213.             this.path = path;  
  214.         }  
  215.         public FileLog(Long id, String path) {  
  216.             this.id = id;  
  217.             this.path = path;  
  218.         }     
  219.      }  
  220. }  
 

  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. }  
 

(1)Android客户端代码:

  1. public class UploadActivity extends Activity   
  2. {  
  3.     private EditText filenameText;  
  4.     private TextView resulView;  
  5.     private ProgressBar uploadbar;  
  6.     private UploadLogService logService;  
  7.       
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.           
  14.         logService = new UploadLogService(this);  
  15.         filenameText = (EditText)this.findViewById(R.id.filename);  
  16.         uploadbar = (ProgressBar) this.findViewById(R.id.uploadbar);  
  17.         resulView = (TextView)this.findViewById(R.id.result);  
  18.         Button button =(Button)this.findViewById(R.id.button);  
  19.         button.setOnClickListener(new View.OnClickListener()   
  20.         {  
  21.             @Override  
  22.             public void onClick(View v)   
  23.             {  
  24.                 String filename = filenameText.getText().toString();  
  25.                 //判断SDCard是否存在  
  26.                 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))  
  27.                 {  
  28.                     //取得SDCard的目录  
  29.                     File uploadFile = new File(Environment.getExternalStorageDirectory(), filename);  
  30.                     if(uploadFile.exists())  
  31.                     {  
  32.                         uploadFile(uploadFile);  
  33.                     }  
  34.                     else  
  35.                     {  
  36.                         Toast.makeText(UploadActivity.this, R.string.filenotexsit, 1).show();  
  37.                     }  
  38.                 }  
  39.                 else  
  40.                 {  
  41.                     Toast.makeText(UploadActivity.this, R.string.sdcarderror, 1).show();  
  42.                 }  
  43.             }  
  44.         });  
  45.     }  
  46.     /** 
  47.      * 使用Handler给创建他的线程发送消息, 
  48.      * 匿名内部类 
  49.      */  
  50.     private Handler handler = new Handler()  
  51.     {  
  52.         @Override  
  53.         public void handleMessage(Message msg)   
  54.         {  
  55.             //获得上传长度的进度  
  56.             int length = msg.getData().getInt("size");  
  57.             uploadbar.setProgress(length);  
  58.             float num = (float)uploadbar.getProgress()/(float)uploadbar.getMax();  
  59.             int result = (int)(num * 100);  
  60.             //设置显示结果  
  61.             resulView.setText(result+ "%");  
  62.             //上传成功  
  63.             if(uploadbar.getProgress()==uploadbar.getMax())  
  64.             {  
  65.                 Toast.makeText(UploadActivity.this, R.string.success, 1).show();  
  66.             }  
  67.         }  
  68.     };  
  69.       
  70.     /** 
  71.      * 上传文件,应该启动一个线程,使用Handler来避免UI线程ANR错误 
  72.      * @param final uploadFile 
  73.      */  
  74.     private void uploadFile(final File uploadFile)   
  75.     {  
  76.         new Thread(new Runnable()   
  77.         {             
  78.             @Override  
  79.             public void run()   
  80.             {  
  81.                 try   
  82.                 {  
  83.                     //设置长传文件的最大刻度  
  84.                     uploadbar.setMax((int)uploadFile.length());  
  85.                     //判断文件是否已有上传记录  
  86.                     String souceid = logService.getBindId(uploadFile);  
  87.                     //构造拼接协议  
  88.                     String head = "Content-Length="+ uploadFile.length() + ";filename="+ uploadFile.getName() + ";sourceid="+  
  89.                         (souceid==null"" : souceid)+"/r/n";  
  90.                     //通过Socket取得输出流  
  91.                     Socket socket = new Socket("192.168.1.100"7878);  
  92.                     OutputStream outStream = socket.getOutputStream();  
  93.                     outStream.write(head.getBytes());  
  94.                       
  95.                     PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());      
  96.                     //获取到字符流的id与位置  
  97.                     String response = StreamTool.readLine(inStream);  
  98.                     String[] items = response.split(";");  
  99.                     String responseid = items[0].substring(items[0].indexOf("=")+1);  
  100.                     String position = items[1].substring(items[1].indexOf("=")+1);  
  101.                     //代表原来没有上传过此文件,往数据库添加一条绑定记录  
  102.                     if(souceid==null)  
  103.                     {  
  104.                         logService.save(responseid, uploadFile);  
  105.                     }  
  106.                     RandomAccessFile fileOutStream = new RandomAccessFile(uploadFile, "r");  
  107.                     fileOutStream.seek(Integer.valueOf(position));  
  108.                     byte[] buffer = new byte[1024];  
  109.                     int len = -1;  
  110.                     //初始化长传的数据长度  
  111.                     int length = Integer.valueOf(position);  
  112.                     while( (len = fileOutStream.read(buffer)) != -1)  
  113.                     {  
  114.                         outStream.write(buffer, 0, len);  
  115.                         //设置长传数据长度  
  116.                         length += len;  
  117.                         Message msg = new Message();  
  118.                         msg.getData().putInt("size", length);  
  119.                         handler.sendMessage(msg);  
  120.                     }  
  121.                     fileOutStream.close();  
  122.                     outStream.close();  
  123.                     inStream.close();  
  124.                     socket.close();  
  125.                     //判断上传完则删除数据  
  126.                     if(length==uploadFile.length())   
  127.                         logService.delete(uploadFile);  
  128.                 }   
  129.                 catch (Exception e)  
  130.                 {  
  131.                     e.printStackTrace();  
  132.                 }  
  133.             }  
  134.         }).start();  
  135.     }  
  136. }  
 

  1. public class DBOpenHelper extends SQLiteOpenHelper   
  2. {  
  3.     public DBOpenHelper(Context context)   
  4.     {  
  5.         super(context, "upload.db"null1);  
  6.     }  
  7.     @Override  
  8.     public void onCreate(SQLiteDatabase db)   
  9.     {  
  10.         db.execSQL("CREATE TABLE uploadlog (_id integer primary key autoincrement, uploadfilepath varchar(100), sourceid varchar(10))");  
  11.     }  
  12.     @Override  
  13.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)   
  14.     {  
  15.         db.execSQL("DROP TABLE IF EXISTS uploadlog");  
  16.         onCreate(db);         
  17.     }  
  18. }  
 

  1. public class UploadLogService   
  2. {  
  3.     private DBOpenHelper dbOpenHelper;  
  4.     //给出上下文对象  
  5.     public UploadLogService(Context context)  
  6.     {  
  7.         this.dbOpenHelper = new DBOpenHelper(context);  
  8.     }  
  9.     //保存上传文件断点数据  
  10.     public void save(String sourceid, File uploadFile)  
  11.     {  
  12.         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();  
  13.         db.execSQL("insert into uploadlog(uploadfilepath, sourceid) values(?,?)",  
  14.                 new Object[]{uploadFile.getAbsolutePath(),sourceid});  
  15.     }  
  16.     //删除上传文件断点数据  
  17.     public void delete(File uploadFile)  
  18.     {  
  19.         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();  
  20.         db.execSQL("delete from uploadlog where uploadfilepath=?"new Object[]{uploadFile.getAbsolutePath()});  
  21.     }  
  22.     //根据文件的上传路径得到绑定的id  
  23.     public String getBindId(File uploadFile)  
  24.     {  
  25.         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();  
  26.         Cursor cursor = db.rawQuery("select sourceid from uploadlog where uploadfilepath=?",   
  27.                 new String[]{uploadFile.getAbsolutePath()});  
  28.         if(cursor.moveToFirst())  
  29.         {  
  30.             return cursor.getString(0);  
  31.         }  
  32.         return null;  
  33.     }  
  34. }  
 

  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.                     case -1:  
  20.                     case '/n':  
  21.                         break loop;  
  22.                     case '/r':  
  23.                         int c2 = in.read();  
  24.                         if ((c2 != '/n') && (c2 != -1)) in.unread(c2);  
  25.                         break loop;  
  26.                     default:  
  27.                         if (--room < 0) {  
  28.                             char[] lineBuffer = buf;  
  29.                             buf = new char[offset + 128];  
  30.                             room = buf.length - offset - 1;  
  31.                             System.arraycopy(lineBuffer, 0, buf, 0, offset);  
  32.                              
  33.                         }  
  34.                         buf[offset++] = (char) c;  
  35.                         break;  
  36.                 }  
  37.             }  
  38.             if ((c == -1) && (offset == 0)) return null;  
  39.             return String.copyValueOf(buf, 0, offset);  
  40.     }  
  41.        
  42.     /** 
  43.     * 读取流 
  44.     * @param inStream 
  45.     * @return 字节数组 
  46.     * @throws Exception 
  47.     */  
  48.     public static byte[] readStream(InputStream inStream) throws Exception  
  49.     {  
  50.             ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  51.             byte[] buffer = new byte[1024];  
  52.             int len = -1;  
  53.             while( (len=inStream.read(buffer)) != -1)  
  54.             {  
  55.                 outSteam.write(buffer, 0, len);  
  56.             }  
  57.             outSteam.close();  
  58.             inStream.close();  
  59.             return outSteam.toByteArray();  
  60.     }  


你可能感兴趣的:(Android下使用TCP/IP协议实现断点上传)