Android 文件断点上传器[多用户并发访问]

通过TCP/IP(SOCKET)协议实现文件断点上传(实现多用户并发访问)。

HTTP不支持文件断点续传,所以无法使用HTTP协议。

场景:

1. 网络不稳定,导致上传失败,下次不是从头开始,而是从断点开始上传;

2. 上传大文件,无法http上传,因为web服务器考虑到安全因素,会限制文件大小,一般10+m。

此文件断点上传器使用自定义协议。

服务器为上传的文件在服务器端生成唯一的sourceid关联上传文件,当客户端上传文件时,首次的sourceid为空,服务端先判断sourceid是否为空,如果为空,生成sourceid和下载断点position=0返回给客户端,如果不为空,把之前记录的sourceid和上次记录的当前下载断点position返回给客户端,客户端指定从文件的position位置开始上传数据。当下一次传文件时,服务器由sourceid关联到文件,找到sourceid对应文件的当前下载断点position返回给客户端,客户端从指定位置position开始上传数据。

服务器端:

FileServer.java

package cn.itcast.net.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.itcast.utils.StreamTool;

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) {
        }
     }
     /**
      * 启动服务
      * @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;
        }

        public void run() {
            try {
                System.out.println("accepted connection "+ socket.getInetAddress()+ ":"+ socket.getPort());

                //创建回退流对象,将拆解的字节数组流传入
                //PushbackInputStream类继承了FilterInputStream类是iputStream类的修饰者。
                //提供可以将数据插入到输入流前端的能力。能够插入的最大字节数与推回缓冲区的大小相关。
                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);
                    long id = System.currentTimeMillis();//生产资源id,如果需要唯一性,可以采用UUID
                    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) {}
            }
        }
     }

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

}

ServerWindow.java

package cn.itcast.net.server;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class ServerWindow extends Frame{//服务端程序入口
    private FileServer s = new FileServer(7878);
    private Label label;

    public ServerWindow(String title){
        super(title);
        label = new Label();
        add(label, BorderLayout.PAGE_START);
        label.setText("服务器已经启动");
        this.addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            s.start();
                        } catch (Exception e) {
                            //e.printStackTrace();
                        }
                    }
                }).start();
            }

            public void windowIconified(WindowEvent e) {
            }

            public void windowDeiconified(WindowEvent e) {
            }

            public void windowDeactivated(WindowEvent e) {
            }

            public void windowClosing(WindowEvent e) {
                 s.quit();
                 System.exit(0);
            }

            public void windowClosed(WindowEvent e) {
            }

            public void windowActivated(WindowEvent e) {
            }
        });
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        ServerWindow window = new ServerWindow("文件上传服务端");
        window.setSize(300, 300);
        window.setVisible(true);

    }

}

工具类StreamTool.java作用获取第一行自定义协议内容

package cn.itcast.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

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();
    }
}

将服务器端转成可执行文件jar

运行启动服务器

Android客户端:

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/filename" />

    <EditText
        android:id="@+id/filename"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TeamTalk-Android.apk" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

    <ProgressBar
        android:id="@+id/uploadbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="20px" />

    <TextView
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />

</LinearLayout>

本地信息存储用sqlite数据库。

MainActivity.java

package cn.itcast.upload;

import java.io.File;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.Socket;

import cn.itcast.service.UploadLogService;
import cn.itcast.utils.StreamTool;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText filenameText;
    private TextView resultView;
    private ProgressBar uploadbar;
    private UploadLogService service;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            uploadbar.setProgress(msg.getData().getInt("length"));
            float num = (float)uploadbar.getProgress() / (float)uploadbar.getMax();
            int result = (int)(num * 100);
            resultView.setText(result + "%");
            if(uploadbar.getProgress() == uploadbar.getMax()){
                Toast.makeText(MainActivity.this, R.string.success, 1).show();
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        service =  new UploadLogService(this);
        filenameText = (EditText)findViewById(R.id.filename);
        resultView = (TextView)findViewById(R.id.result);
        uploadbar = (ProgressBar)findViewById(R.id.uploadbar);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String filename = filenameText.getText().toString();
                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    File file = new File(Environment.getExternalStorageDirectory(), filename);
                    if(file.exists()){
                        uploadbar.setMax((int)file.length());
                        uploadFile(file);
                    }else{
                        Toast.makeText(MainActivity.this, R.string.notexsit, 1).show();
                    }
                }else{
                    Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show();
                }
            }
        });
    }

    private void uploadFile(final File file) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    String sourceid = service.getBindId(file);
                    Socket socket = new Socket("192.168.0.83", 7878);
                    OutputStream outStream = socket.getOutputStream();
                    String head = "Content-Length="+ file.length() + ";filename="+ file.getName()
                        + ";sourceid="+(sourceid!=null ? sourceid : "")+"\r\n";
                    outStream.write(head.getBytes());

                    PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
                    String response = StreamTool.readLine(inStream);
                    String[] items = response.split(";");
                    String responseSourceid = items[0].substring(items[0].indexOf("=")+1);
                    String position = items[1].substring(items[1].indexOf("=")+1);
                    if(sourceid==null){//如果是第一次上传文件,在数据库中不存在该文件所绑定的资源id
                        service.save(responseSourceid, file);
                    }
                    RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
                    fileOutStream.seek(Integer.valueOf(position));
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    int length = Integer.valueOf(position);
                    while( (len = fileOutStream.read(buffer)) != -1){
                        outStream.write(buffer, 0, len);
                        length += len;//累加已经上传的数据长度
                        Message msg = new Message();
                        msg.getData().putInt("length", length);
                        handler.sendMessage(msg);
                    }
                    if(length == file.length()) service.delete(file);
                    fileOutStream.close();
                    outStream.close();
                    inStream.close();
                    socket.close();
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, R.string.error, 1).show();
                }
            }
        }).start();
    }
}

编译运行部署到模拟器上如下

将本地要上传的文件TeamTalk-Android.apk导入到模拟器的sdcard目录下

模拟器界面中点击上传即可在服务器端目录中生成文件

当下次上传的时候就会从前一次上传的断点出继续上传文件,当全部上传结束,会Toast显示上传成功。

具体项目代码demo点击此处下载。

时间: 2024-08-26 16:44:02

Android 文件断点上传器[多用户并发访问]的相关文章

Android中Socket大文件断点上传

什么是Socket? 所谓Socket通常也称作“套接字”,用于描述IP地址和端口,是一个通信连的句柄,应用程序通常通过“套接字”向网络发送请求或者应答网络请求,它就是网络通信过程中端点的抽象表示.它主要包括以下两个协议: TCP (Transmission Control Protocol 传输控制协议):传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供超时重发,丢弃重复数据,检验数据,流量控制等功

Android应用开发之使用Socket进行大文件断点上传续传

http://www.linuxidc.com/Linux/2012-03/55567.htm http://blog.csdn.net/shimiso/article/details/8529633/ 在Android中上传文件可以采用HTTP方式,也可以采用Socket方式,但是HTTP方式不能上传大文件,这里介绍一种通过Socket方式来进行断点续传的方式,服务端会记录下文件的上传进度,当某一次上传过程意外终止后,下一次可以继续上传,这里用到的其实还是J2SE里的知识. 这个上传程序的原理

文件断点上传,html5实现前端,java实现服务器

断点上传能够防止意外情况导致上传一半的文件下次上传时还要从头下载,网上有很多关于断点的实现,这篇文章只是从前到后完整的记录下一个可用的实例,由于生产环境要求不高,而且就是提供给一两个人用,所以我简化了诸多过程,不用flash,也不用applet,只是通过html5的新特性进行浏览器端的处理. 简单说下关键点 如果上次传到n字节,那么浏览器下次续传直接就是从文件的n字节开始向服务器传送数据,而不是都传过去,服务器从n字节开始接收. html5能给文件分片,所以每次上传完一块文件后,应该返回当前已经

jsp+servlet怎么实现文件断点上传下载

我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,ie8,ie9,Chrome,Firefox,360安全浏览器,并且刷新浏览器后仍然能够续传,重启浏览器(关闭浏览器后再打开)仍然能够继续上传,重启电脑后仍然能够上传 支持文件夹的上传,要求服务端能够保留层级结构,并且能够续传.需要支持10万个以上的文件夹上传. 支持低版本的系统和浏览器,因为这个项目

JSP如何实现文件断点上传和断点下载?

核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开始. 如何分,利用强大的js库,来减轻我们的工作,市场上已经能有关于大文件分块的轮子,虽然程序员的天性曾迫使我重新造轮子.但是因为时间的关系还有工作的关系,我只能罢休了.最后我选择了百度的WebUploader来实现前端所需. 如何合,在合之前,我们还得先解决一个问题,我们如何区分分块所属那个文件的

php中文件断点上传怎么实现?

1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使用PECL模块安装方法快速简捷,这里不说明 配置php.ini,设置参数 apc.rfc1867=1 ,使APC支持上传进度条功能,在APC源码说明文档里面有说明 代码范例: 大文件(50G)上传的实现细节: 服务端接收文件数据的处理逻辑代码: 2.使用PECL扩展模块uploadprogress实

Android文件的上传和下载

------------------------------------------------------------------多线程下载操作---------------------------------------------------------------------多线程下载.断点续传,进度可视化... 一.---框架--- 1.新建一个布局文件,输入我们想要使用的线程的个数,包括一个主布局文件和一个progressBar (1)一个包括三个控件的主布局 (2)一个只包含Pro

asp.net文件断点上传

HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx.cs"Inherits="up6.index" %> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/

ASP.NET大文件断点上传

HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx.cs"Inherits="up6.index" %> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/