Android中Socket大文件斷點上傳
什麽是Socket?
所謂Socket通常也稱作“套接字”,用於描述IP地址和端口,是一個通信連的句柄,應用程序通常通過“套接字”向網絡發送請求或者應答網絡請求,它就是網絡通信過程中端點的抽象表示。它主要包括以下兩個協議:
TCP (Transmission Control Protocol 傳輸控制協議):傳輸控制協議,提供的是面向連接、可靠的字節流服務。當客戶和服務器彼此交換數據前,必須先在雙方之間建立一個TCP連接,之後才能傳輸數據。TCP提供超時重發,丟棄重復數據,檢驗數據,流量控制等功能,保證數據能從一端傳到另一端。
詳細解說如下:
TCP傳輸和UDP不一樣,TCP傳輸是流式的,必須先建立連接,然後數據流沿已連接的線路(虛電路)傳輸。因此TCP的數據流不會像UDP數據報一樣,每個數據報都要包含目標地址和端口,因為每個數據報要單獨路由。TCP傳輸則只需要在建立連接時指定目標地址和端口就可以了。
形象的講,TCP就像打電話,UDP就像發電報。宏觀上來看UDP是不分客戶端和服務端的。通信雙方是平等的。微觀上來講只相對一個報文,發送端是客戶端,監聽端是服務端。發送端把數據報發給路由器就像把電報發給了郵局,後面的事情就是發送者無法控制,也無從知曉的了。所以說是不可靠的,可能會出現報文丟失而無從知曉。就像每張電報都要有收件人一樣,每個數據報都要有目的地址和端口。
而TCP每次連接都是分客戶端和服務端的。連接的發起者(相當與撥號打電話的人)是客戶端,監聽者(相當於在電話邊等著接電話的人)是服務端。發起者指定要連接的服務器地址和端口(相當於撥號),監聽者通過和發起者三次握手建立連接(相當於聽到電話響去接電話)。建立連接後雙方可以互相發送和接受數據(打電話)。
Java如何操作Socket?
值得一提的是,Java分別為TCP和UDP提供了相應的類,TCP是java.NET中提供了兩個類Socket和ServerSocket,分別用來表示雙向連接的客戶端和服務端。這是兩個封裝得非常好的類,使用起來很方便!UDP是java.net.DatagramSocket.
127.0.0.1是回路地址,用於測試,相當於localhost本機地址,沒有網卡,不設DNS都可以訪問,端口地址在0~65535之間,其中0~1023之間的端口是用於一些知名的網絡服務和應用,用戶的普通網絡應用程序應該使用1024以上的端口.
Socket通信模型如下:
如果大家對Java Socket編程還有模糊的地方抓緊溫習(http://blog.csdn.net/shimiso/article/details/8529941),本文不在此贅述,下面我們以最常用的TCP協議舉例:
服務器,使用ServerSocket監聽指定的端口,端口可以隨意指定(由於1024以下的端口通常屬於保留端口,在一些操作系統中不可以隨意使用,所以建議使用大於1024的端口),等待客戶連接請求,客戶連接後,會話產生;在完成會話後,關閉連接。
客戶端,使用java socket通信對網絡上某一個服務器的某一個端口發出連接請求,一旦連接成功,打開會話;會話完成後,關閉Socket。客戶端不需要指定打開的端口,通常臨時的、動態的分配一個1024以上的端口。
TCP網絡連接模型:
Android客戶端程序代分析:
[java] view plain copy- UploadActivity.java
- package com.android.upload;
- import java.io.File;
- import java.io.OutputStream;
- import java.io.PushbackInputStream;
- import java.io.RandomAccessFile;
- import java.net.Socket;
- 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.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.android.service.UploadLogService;
- import com.android.socket.utils.StreamTool;
- public class UploadActivity extends Activity {
- private EditText filenameText;
- private TextView resulView;
- private ProgressBar uploadbar;
- private UploadLogService logService;
- private boolean start=true;
- private Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- int length = msg.getData().getInt("size");
- uploadbar.setProgress(length);
- float num = (float)uploadbar.getProgress()/(float)uploadbar.getMax();
- int result = (int)(num * 100);
- resulView.setText(result+ "%");
- if(uploadbar.getProgress()==uploadbar.getMax()){
- Toast.makeText(UploadActivity.this, R.string.success, 1).show();
- }
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- logService = new UploadLogService(this);
- filenameText = (EditText)this.findViewById(R.id.filename);
- uploadbar = (ProgressBar) this.findViewById(R.id.uploadbar);
- resulView = (TextView)this.findViewById(R.id.result);
- Button button =(Button)this.findViewById(R.id.button);
- Button button1 =(Button)this.findViewById(R.id.stop);
- button1 .setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- start=false;
- }
- });
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- start=true;
- String filename = filenameText.getText().toString();
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- File uploadFile = new File(Environment.getExternalStorageDirectory(), filename);
- if(uploadFile.exists()){
- uploadFile(uploadFile);
- }else{
- Toast.makeText(UploadActivity.this, R.string.filenotexsit, 1).show();
- }
- }else{
- Toast.makeText(UploadActivity.this, R.string.sdcarderror, 1).show();
- }
- }
- });
- }
- /**
- * 上傳文件
- * @param uploadFile
- */
- private void uploadFile(final File uploadFile) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- uploadbar.setMax((int)uploadFile.length());
- String souceid = logService.getBindId(uploadFile);
- String head = "Content-Length="+ uploadFile.length() + ";filename="+ uploadFile.getName() + ";sourceid="+
- (souceid==null? "" : souceid)+"\r\n";
- Socket socket = new Socket("192.168.1.78",7878);
- OutputStream outStream = socket.getOutputStream();
- outStream.write(head.getBytes());
- PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
- String response = StreamTool.readLine(inStream);
- String[] items = response.split(";");
- String responseid = items[0].substring(items[0].indexOf("=")+1);
- String position = items[1].substring(items[1].indexOf("=")+1);
- if(souceid==null){//代表原來沒有上傳過此文件,往數據庫添加一條綁定記錄
- logService.save(responseid, uploadFile);
- }
- RandomAccessFile fileOutStream = new RandomAccessFile(uploadFile, "r");
- fileOutStream.seek(Integer.valueOf(position));
- byte[] buffer = new byte[1024];
- int len = -1;
- int length = Integer.valueOf(position);
- while(start&&(len = fileOutStream.read(buffer)) != -1){
- outStream.write(buffer, 0, len);
- length += len;
- Message msg = new Message();
- msg.getData().putInt("size", length);
- handler.sendMessage(msg);
- }
- fileOutStream.close();
- outStream.close();
- inStream.close();
- socket.close();
- if(length==uploadFile.length()) logService.delete(uploadFile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- }
- StreamTool.java
- package com.android.socket.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();
- }
- }
- UploadLogService.java
- package com.android.service;
- import java.io.File;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- public class UploadLogService {
- private DBOpenHelper dbOpenHelper;
- public UploadLogService(Context context){
- this.dbOpenHelper = new DBOpenHelper(context);
- }
- public void save(String sourceid, File uploadFile){
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- db.execSQL("insert into uploadlog(uploadfilepath, sourceid) values(?,?)",
- new Object[]{uploadFile.getAbsolutePath(),sourceid});
- }
- public void delete(File uploadFile){
- SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
- db.execSQL("delete from uploadlog where uploadfilepath=?", new Object[]{uploadFile.getAbsolutePath()});
- }
- public String getBindId(File uploadFile){
- SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
- Cursor cursor = db.rawQuery("select sourceid from uploadlog where uploadfilepath=?",
- new String[]{uploadFile.getAbsolutePath()});
- if(cursor.moveToFirst()){
- return cursor.getString(0);
- }
- return null;
- }
- }
- DBOpenHelper.java
- package com.android.service;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DBOpenHelper extends SQLiteOpenHelper {
- public DBOpenHelper(Context context) {
- super(context, "upload.db", null, 1);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE uploadlog (_id integer primary key autoincrement, uploadfilepath varchar(100), sourceid varchar(10))");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS uploadlog");
- onCreate(db);
- }
- }
- main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/filename"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="022.jpg"
- android:id="@+id/filename"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button"
- android:id="@+id/button"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="暫停"
- android:id="@+id/stop"
- />
- <ProgressBar
- android:layout_width="fill_parent"
- android:layout_height="20px"
- style="?android:attr/progressBarStyleHorizontal"
- android:id="@+id/uploadbar"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:id="@+id/result"
- />
- </LinearLayout>
- AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.upload"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".UploadActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <!-- 訪問網絡的權限 -->
- <uses-permission android:name="android.permission.INTERNET"/>
- <!-- 在SDCard中創建與刪除文件權限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <!-- 往SDCard寫入數據權限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- </manifest>
Java服務端:
[java] view plain copy- SocketServer.javapackage com.android.socket.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 com.android.socket.utils.StreamTool;
- public class SocketServer {
- private String uploadPath="D:/uploadFile/";
- private ExecutorService executorService;// 線程池
- private ServerSocket ss = null;
- private int port;// 監聽端口
- private boolean quit;// 是否退出
- private Map<Long, FileLog> datas = new HashMap<Long, FileLog>();// 存放斷點數據,最好改為數據庫存放
- public SocketServer(int port) {
- this.port = port;
- // 初始化線程池
- executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
- .availableProcessors() * 50);
- }
- // 啟動服務
- public void start() throws Exception {
- ss = new ServerSocket(port);
- while (!quit) {
- Socket socket = ss.accept();// 接受客戶端的請求
- // 為支持多用戶並發訪問,采用線程池管理每一個用戶的連接請求
- executorService.execute(new SocketTask(socket));// 啟動一個線程來處理請求
- }
- }
- // 退出
- public void quit() {
- this.quit = true;
- try {
- ss.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws Exception {
- SocketServer server = new SocketServer(7878);
- server.start();
- }
- private class SocketTask implements Runnable {
- private Socket socket;
- public SocketTask(Socket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- System.out.println("accepted connenction from "
- + socket.getInetAddress() + " @ " + socket.getPort());
- 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();
- FileLog log = null;
- if (null != sourceid && !"".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(uploadPath+ 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 FileLog(Long id, String path) {
- super();
- this.id = id;
- this.path = 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;
- }
- }
- }
- ServerWindow.javapackage com.android.socket.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 SocketServer server;
- private Label label;
- public ServerWindow(String title){
- super(title);
- server = new SocketServer(7878);
- label = new Label();
- add(label, BorderLayout.PAGE_START);
- label.setText("服務器已經啟動");
- this.addWindowListener(new WindowListener() {
- @Override
- public void windowOpened(WindowEvent e) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- server.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- @Override
- public void windowIconified(WindowEvent e) {
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
- }
- @Override
- public void windowDeactivated(WindowEvent e) {
- }
- @Override
- public void windowClosing(WindowEvent e) {
- server.quit();
- System.exit(0);
- }
- @Override
- public void windowClosed(WindowEvent e) {
- }
- @Override
- 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.javapackage com.android.socket.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();
- }
- }
運行效果如下:
android前端控制:
後臺監控日誌:
下載後的文件路徑:
源碼下載地址
Android中Socket大文件斷點上傳