Socket通信学习系列
Socket网络编程学习基础
Socket通信学习实例一之TCP通信
Socket通信学习实例二之即时通信
Socket通信学习实例三之断点上传
Socket通信学习实例四之UDP通信
Socket通信学习实例五之模拟Http
本篇文章介绍如何通过Socket通信来实现文件向服务器断点续传
RandomAccessFile类
RandomAccessFile是Java中输入,输出流体系中功能最丰富的文件内容访问类,它提供很多方法来操作文件,包括读写支持,与普通的IO流相比,它最大的特别之处就是支持任意访问的方式,程序可以直接跳到任意地方来读写数据
主要用到这个方法
Properties类
主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置
我们会用到这两个方法
PushbackInputStream类
PushbackInputStream存在的意义就是允许我试探性的读取数据流,如果不是我们想要的则返还回去,之所以能够这样,因为其内部维护了一个pushback buffer缓冲区
然后我们来分析下应该如何实现文件断点上传
在本实例中客户端每次连接服务端的时候都会发送”Content-Length=XXXXX;filename=XXXXXX;sourceid=XXXXX”格式的字符串,在第一次连接服务端时,sourceid传空值,然后服务端接收后根据sourceid进行判断,如果为空则是第一次上传,就会为文件添加跟踪记录,如果不为空则查找文件的上传记录,最后实时记录文件的长度后放入输出流中,发送给客户端
通过初始化FileServer对象后调用start()方法开启服务端上传功能
public class SocketUpload {
public SocketUpload() throws Exception {
System.out.println("服务端 " + SocketTool.getIP() + " 运行中...\n");
FileServer s = new FileServer(SocketTool.PORT);
s.start();
}
}
FileServer类
public class FileServer {
private ExecutorService executorService;//线程池
private int port;//监听端口
private boolean quit = false;//退出
private ServerSocket server;
private Map datas = new HashMap();//存放断点数据
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();
}
}
}
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);
}
}
代码都有注释,基本都很好理解
在构造函数中使用ExecutorService创建一个具有固定个数线程池,每个线程对应一个用户的连接请求来支持并发操作,在start()方法内通过不停循环来等待客户端连接,如果有新用户连接则开启一个新线程
内部类SocketTask实现了Runnable接口
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 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();
//使用Hashtable的put方法可用与properties.setProperty()方法相同
//作用是存储键值对
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) {
}
}
}
}
具体可以看注释,这里大概解释下代码流程:
服务端对客户端发过来的自定义协议进行解析(Content-Length=XXXXX;filename=XXXXXX;sourceid=XXXXX),根据sourceid判断后查找文件上传记录,如果是第一次上传就执行创建文件、log日志、初始化position等操作,然后将输入流中的数据保存在成文件并生成文件上传记录;如果之前已经存在上传记录则读取记录文件并继续进行输入流的写入操作和实时保存文件上传记录,最后将目前上传进度的配置信息(responsesourceid=xxxxxx;position=xx)写入输出流发送给客户端
保存log文件信息的bean类
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;
}
}
首先是界面部分
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文件名"
android:textSize="18sp"/>
<EditText
android:id="@+id/filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="music.mp3"/>
<Button
android:id="@+id/upload_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上传"/>
<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"/>
<ProgressBar
android:id="@+id/upload_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
LinearLayout>
UI逻辑实现
private EditText editText;
private Button btnUpload;
private Button btnStop;
private ProgressBar progressBar;
private TextView tvResult;
private UploadHelper uploadHelper;
private boolean flag = true;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
progressBar.setProgress(msg.getData().getInt("length"));
float num = (float) progressBar.getProgress() / (float) progressBar.getMax();
int result = (int) (num * 100);
tvResult.setText(result + "%");
if (progressBar.getProgress() == progressBar.getMax()) {
Toast.makeText(SocketUploadActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.socket_upload_activity);
initView();
uploadHelper = new UploadHelper(this);
}
private void initView() {
editText = (EditText) findViewById(R.id.filename);
btnUpload = (Button) findViewById(R.id.upload_button);
btnStop = (Button) findViewById(R.id.stop_button);
progressBar = (ProgressBar) findViewById(R.id.upload_bar);
tvResult = (TextView) findViewById(R.id.result);
btnUpload.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.upload_button:
String filename = editText.getText().toString();
flag = true;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(), filename);
if (file.exists()) {
progressBar.setMax((int) file.length());
uploadFile(file);
} else {
Toast.makeText(SocketUploadActivity.this, "文件并不存在~", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(SocketUploadActivity.this, "SD卡不存在或者不可用", Toast.LENGTH_SHORT).show();
}
break;
case R.id.stop_button:
flag = false;
break;
}
}
比较简单不详细说明了,通过handler来刷新进度
Socket通信部分
private void uploadFile(final File uploadFile) {
new Thread(new Runnable() {
public void run() {
try {
String sourceid = uploadHelper.getBindId(uploadFile);
Socket socket = new Socket(SocketTool.HOST, SocketTool.PORT);
OutputStream outStream = socket.getOutputStream();
String head = "Content-Length=" + uploadFile.length() + ";filename=" + uploadFile.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) {//代表原来没有上传过此文件,往数据库添加一条绑定记录
uploadHelper.save(responseSourceid, 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 (flag && (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 == uploadFile.length()) uploadHelper.delete(uploadFile);
fileOutStream.close();
outStream.close();
inStream.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Socket通信部分和服务端的逻辑都差不多,区别在于Android客户端生成自定义协议(Content-Length=XXXXX;filename=XXXXXX;sourceid=XXXXX)后写入输出流发送给服务端;Android客户端使用SQLite来保存服务器返回的信息(responsesourceid=xxxxxx;position=xx),并且将上传进度通过handler进行刷新
SQLite类,创建数据库
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "jay.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS uploadlog (_id integer primary key autoincrement, path varchar(20), sourceid varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
SQLite操作类,进行增删查改相关操作
public class UploadHelper {
private DBOpenHelper dbOpenHelper;
public UploadHelper(Context context) {
dbOpenHelper = new DBOpenHelper(context);
}
public String getBindId(File file) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select sourceid from uploadlog where path=?", new String[]{file.getAbsolutePath()});
if (cursor.moveToFirst()) {
return cursor.getString(0);
}
return null;
}
public void save(String sourceid, File file) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("insert into uploadlog(path,sourceid) values(?,?)",
new Object[]{file.getAbsolutePath(), sourceid});
}
public void delete(File file) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("delete from uploadlog where path=?", new Object[]{file.getAbsolutePath()});
}
}
流处理工具类
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();
}
}
完整示例:
SocketSamples
本实例代码参考:
http://blog.csdn.net/shimiso/article/details/8529633/