http://www.linuxidc.com/Linux/2012-03/55567.htm
http://blog.csdn.net/shimiso/article/details/8529633/
在Android中上传文件可以采用HTTP方式,也可以采用Socket方式,但是HTTP方式不能上传大文件,这里介绍一种通过Socket方式来进行断点续传的方式,服务端会记录下文件的上传进度,当某一次上传过程意外终止后,下一次可以继续上传,这里用到的其实还是J2SE里的知识。
这个上传程序的原理是:客户端第一次上传时向服务端发送“Content-Length=35;filename=WinRAR_3.90_SC.exe;sourceid=“这种格式的字符串,服务端收到后会查找该文件是否有上传记录,如果有就返回已经上传的位置,否则返回新生成的sourceid以及position为0,类似”sourceid=2324838389;position=0“这样的字符串,客户端收到返回后的字符串后再从指定的位置开始上传文件。
首先是服务端代码:
SocketServer.java
- package 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 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(8787);
- 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());
-
-
- 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("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";
-
-
- 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.java
- package 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(8787);
- label = new Label();
- add(label, BorderLayout.PAGE_START);
- label.setText("服务器已经启动www.linuxidc.com");
- 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) {
- }
- });
- }
-
- public static void main(String[] args) {
- ServerWindow window = new ServerWindow("文件上传服务端");
- window.setSize(300, 300);
- window.setVisible(true);
- }
-
- }
工具类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);
- }
-
-
- 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进行大文件断点上传续传
原文:http://www.cnblogs.com/tc310/p/5243004.html