本站业务范围:1、PC端软件开发、网站开发 2、移动端APP、网站、微信接口、微商城开发 3、视频教程、课程设计和辅导 4、单片机开发 5、串口通讯调试
 当前位置:文章中心 >> Android/移动互联网/物联网/
立即购买视频教程 android端实现断点续传功能
夜鹰教程网 来源:www.yyjcw.com 日期:2018-7-1 10:00:22
android端实现断点续传功能

这篇文章不能解决你的问题?我们还有相关视频教程云课堂 全套前端开发工程师培训课程

微信号:yyjcw10000 QQ:1416759661  远程协助需要加QQ!

业务范围:视频教程|程序开发|在线解答|Demo制作|远程调试| 点击查看相关的视频教程

技术范围:全端开发/前端开发/webapp/web服务/接口开发/单片机/C#/java/node/sql server/mysql/mongodb/android/。 



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

1.上传原理:Android客户端发送上传文件头字段给服务器,服务器建立socket连接,监听一个端口(7878),然后建立一个outStream接收到客户端的字段信息,服务器判断文件是否在服务器上,文件是否有上传的记录,若是文件不存在,服务器则返回一个id(断点数据)通知客户端从什么位置开始上传,客户端通过inputStream获得服务器返回的字段,开始从获得的位置开始上传文件

2.实例演示

(0)服务器端代码

  • 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)   

  •         {  

  •             e.printStackTrace();  

  •         }  

  •      }  

  •        

  •      /** 

  •       * 启动服务 

  •       * @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可以回退到之前的某个点开始进行读取   

  •                 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);        

  •                     //生产资源id,如果需要唯一性,可以采用UUID   

  •                     long id = System.currentTimeMillis();  

  •                     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)  

  •                 {  

  •                     e.printStackTrace();  

  •                 }  

  •             }  

  •         }  

  •      }  

  •        

  •      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;  

  •         }     

  •      }  

  • }  

     

    (1)Android客户端代码:

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

  • public class StreamTool   

  • {  

  •        

  •      public static void save(File file, byte[] data) throws Exception   

  •      {  

  •          FileOutputStream outStream = new FileOutputStream(file);  

  •          outStream.write(data);  

  •          outStream.close();  

  •      }  

  •        

  •      public static String readLine(PushbackInputStream in) throws IOException   

  •      {  

  •             char buf[] = new char[128];  

  •             int room = buf.length;  

  •             int offset = 0;  

  •             int c;  

  • loop:       while (true) {  

  •                 switch (c = in.read()) {  

  •                     case -1:  

  •                     case '/n':  

  •                         break loop;  

  •                     case '/r':  

  •                         int c2 = in.read();  

  •                         if ((c2 != '/n') && (c2 != -1)) in.unread(c2);  

  •                         break loop;  

  •                     default:  

  •                         if (--room < 0) {  

  •                             char[] lineBuffer = buf;  

  •                             buf = new char[offset + 128];  

  •                             room = buf.length - offset - 1;  

  •                             System.arraycopy(lineBuffer, 0, buf, 0, offset);  

  •                              

  •                         }  

  •                         buf[offset++] = (char) c;  

  •                         break;  

  •                 }  

  •             }  

  •             if ((c == -1) && (offset == 0)) return null;  

  •             return String.copyValueOf(buf, 0, offset);  

  •     }  

  •        

  •     /** 

  •     * 读取流 

  •     * @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();  

  •     }  

  • }  

     


复制链接 网友评论 收藏本文 关闭此页
上一条: 安卓刷机术语  下一条: 利用TCP/IP实现Android客户端与服务端的…
夜鹰教程网成立于2008年,目前已经运营了将近 13 年,发布了大量关于 html5/css3/C#/asp.net/java/python/nodejs/mongodb/sql server/android/javascript/mysql/mvc/easyui/vue/echarts原创教程。 我们一直都在坚持的是:认证负责、一丝不苟、以工匠的精神来打磨每一套教程,让读者感受到作者的用心。我们默默投入的时间,确保每一套教程都是一件作品,而不是呆板的文字和视频! 目前我们推出在线辅导班试运营,模式为一对一辅导,教学工具为QQ。我们的辅导学科包括 java 、android原生开发、webapp开发、商城开发、C#和asp.net开发,winform和物联网开发、web前端开发,但不仅限于此。 普通班针对的是国内学员,例如想打好基础的大学生、想转行的有志青年、想深入学习的程序员、想开发软件的初学者或者业余爱好者等。 就业办针对即将毕业上岗的大四学生,或者打算转行的初级开发工程师。 留学生班针对的是在欧美、加拿大、澳洲、日本、韩国、新加坡等地留学的中国学子,目的是让大家熟练地掌握编程技能,按时完成老师布置的作业,并能顺利地通过考试。 详细咨询QQ:1416759661   夜鹰教程网  基于角色的权限管理系统(c-s/b-s)。
  夜鹰教程网  基于nodejs的聊天室开发视频教程
  夜鹰教程网  Git分布式版本管理视频教程
  夜鹰教程网  MVC+EasyUI视频教程
  夜鹰教程网  在线考试系统视频教程
  夜鹰教程网  MongoDB视频教程。
  夜鹰教程网 Canvas视频教程
  夜鹰教程网 报表开发视频教程
  推荐教程/优惠活动

  热门服务/教程目录

  夜鹰教程网  新手必看,详细又全面。
  夜鹰教程网  购买教程  夜鹰教程网  在线支付-方便
  夜鹰教程网  担保交易-快捷安全   夜鹰教程网  闪电发货
  夜鹰教程网  电话和QQ随时可以联系我们。
  夜鹰教程网 不会的功能都可以找我们,按工作量收费。

客服电话:153 9760 0032

购买教程QQ:1416759661  
  热点推荐
在Struts 2中使用JSON Ajax支持
解决JSP中使用request乱码问题
JSP+JavaScript打造二级级联下拉菜…
Tomcat中文手册(1)
JSP及Servlet中遇到的多线程同步
详解:JSP和Servlet中的绝对路径和…
JSP Struts之HTML标签库详解
自定义JSP标签
JSP中的include指令
errorPage设置方法--JSP
JSP 国际化-格式化货币和日期
九个隐含对象使用总结JSP的
Tomcat中文手册(2)
tomcat6关于EL表达式的一个错误
jsp分页技术代码
  尊贵服务
夜鹰教程网 承接业务:软件开发 网站开发 网页设计 .Net+C#+VS2008+MSsql+Jquery+ExtJs全套高清完整版视频教程
  最近更新
iOS 关于退出键盘两种方法和避免遮…
安卓apk签名常见命令
把activity当成dialog使用
如何选择APP开发框架
Layout _width ,Layout_height和…
Android调用百度地图使用时出现in…
安卓刷机术语
android端实现断点续传功能
利用TCP/IP实现Android客户端与服…
Invalid project description ove…
推荐使用的meta标签
手机网站用Bootstrap还是jQuery M…
如何让手机访问PC网站自动跳转到手…
提升HTML5的性能体验系列之一 避免…
Android和IOS 字体该做多大合适?…
  工具下载  需要远程协助? 

sql2008视频教程 c#视频教程

VIP服务:如果您的某个功能不会做,可以加我们QQ,给你做DEMO!

JQUERY  Asp.net教程

MVC视频教程  vs2012
.NET+sql开发
手机:15397600032 C#视频教程下载
微信小程序 vue.js高级实例视频教程

教程咨询QQ:1416759661


这篇文章不能解决你的问题?我们还有相关视频教程云课堂 全套前端开发工程师培训课程

微信号:yyjcw10000 QQ:1416759661  远程协助需要加QQ!

业务范围:视频教程|程序开发|在线解答|Demo制作|远程调试| 点击查看相关的视频教程

技术范围:全端开发/前端开发/webapp/web服务/接口开发/单片机/C#/java/node/sql server/mysql/mongodb/android/。 



关于我们 | 购买教程 | 网站建设 | 技术辅导 | 常见问题 | 联系我们 | 友情链接

夜鹰教程网 版权所有 www.yyjcw.com All rights reserved 备案号:蜀ICP备08011740号3