1、建立服务端,用于接收上传的文件。这里使用Socket,文件可能会比较大。采用多线程编程,防止并发。

  

package com.socket.service;

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.socket.service.util.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 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;
}
} }

2、通过swing编程启动服务:可以导出运行时jar文件。

package com.socket.service;

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 static final long serialVersionUID = -2723969590211090349L;
private FileServer s = new FileServer(7788);
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);
}
}

3、解析socket协议工具类:

package com.socket.service.util;

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

4、java客户端测试:

package com.socket.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.Socket; import com.socket.service.util.StreamTool; public class SocketClient {
/**
* @param args
*/
public static void main(String[] args) {
try {
String filename = "1.zip";
String dir=SocketClient.class.getClassLoader().getResource(filename).getPath();
System.out.println(dir);
Socket socket = new Socket("127.0.0.1", 7878);
OutputStream outStream = socket.getOutputStream();
File file = new File("src/"+filename);
System.out.println(file.exists());
String head = "Content-Length=" + file.length() + ";filename=" +filename + ";sourceid=\r\n";
outStream.write(head.getBytes()); PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
String response = StreamTool.readLine(inStream);
System.out.println(response);
String[] items = response.split(";");
String position = items[1].substring(items[1].indexOf("=") + 1);
RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
fileOutStream.seek(Integer.valueOf(position));
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fileOutStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
System.out.println(" finish .. ");
fileOutStream.close();
outStream.close();
inStream.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
} } /**
* 读取流
*
* @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客户端测试

----------------------------------------------------------------------------------------------------------

1、activity后台代码:

package com.example.fileupclient;

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.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast; import com.example.service.UploadLogService;
import com.example.util.StreamTool; 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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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,
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this, R.string.sdcarderror,
Toast.LENGTH_LONG).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.8.101", 7788);
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, Toast.LENGTH_LONG).show();
}
}
}).start();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

2、业务代码:

package com.example.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, "itcast.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) { } }
package com.example.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) {
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() });
}
}

3、布局界面:

<?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="5.jpg"
android:id="@+id/filename"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/> <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>

Android学习笔记_43_网络通信之文件断点上传的更多相关文章

  1. Android学习笔记_13_网络通信之多个上传文件

    一.获取HTTP协议: 建立一个Web项目,建立一个如下所示的jsp界面,用IE捕获表单提交信息. <%@ page language="java" contentType= ...

  2. Android学习笔记_15_网络通信之文件断点下载

    一.断点下载原理: 使用多线程下载文件可以更快完成文件的下载,多线程下载文件之所以快,是因为其抢占的服务器资源多.如:假设服务器同时最多服务100个用户,在服务器中一条线程对应一个用户,100条线程在 ...

  3. 后端springmvc,前端html5的FormData实现文件断点上传

    前言 最近项目中有使用到文件断点上传,得空便总结总结,顺便记录一下,毕竟“好记性不如烂笔头”. 后端代码: package com.test.controller; import java.io.Bu ...

  4. Android中Socket大文件断点上传

    原文:http://blog.csdn.net/shimiso/article/details/8529633 什么是Socket? 所谓Socket通常也称作“套接字”,用于描述IP地址和端口,是一 ...

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

    http://www.linuxidc.com/Linux/2012-03/55567.htm http://blog.csdn.net/shimiso/article/details/8529633 ...

  6. Web 在线文件管理器学习笔记与总结(19)上传文件

    dir.func.php 中添加方法: /* 上传文件 */ function uploadFile($fileInfo,$path,$allowExt = array('jpg','jpeg','p ...

  7. (转)Android学习-使用Async-Http实现图片压缩并上传功能

    (转)Android学习-使用Async-Http实现图片压缩并上传功能 文章转载自:作者:RyaneLee链接:http://www.jianshu.com/p/940fc7ba39e1 让我头疼一 ...

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

    断点上传能够防止意外情况导致上传一半的文件下次上传时还要从头下载,网上有很多关于断点的实现,这篇文章只是从前到后完整的记录下一个可用的实例,由于生产环境要求不高,而且就是提供给一两个人用,所以我简化了 ...

  9. java HTTP文件断点上传

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

随机推荐

  1. 阿里云Centos 7上面安装mysql教程

    1 软件的基本安装过程 1 卸载已有的mysql 1.查看系统是否安装了mysql软件 rpm -qa|grep -i mysql 2.将已经安装过的软件卸载掉.注意:这样的卸载是不彻底,不过这里够用 ...

  2. 解决 下载额外数据文件失败 以下软件包要求安装后下载附加数据,但其数据无法下载或无法处理 ttf-mscorefonts-installer

    ubuntu 14.04 今天安装完 wine,之后会出现这个问题 原因应该是需要的字体无法下载 那你需要手动下载, 到这个地址下载 http://sourceforge.net/projects/c ...

  3. topN问题

    topN问题:给出一个数组,找出前N个最大的元素. topN问题可以用分治法解决,这个问题与快速排序类似,快速排序是用一个数对数组进行划分,topN问题则不需完成排序,只需划分出前n个最大的数字即可. ...

  4. nyoj 269——VF——————【dp】

    VF 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Vasya is the beginning mathematician. He decided to make ...

  5. CSS3动画属性animation的基本用法

    CSS3新增了一个令人心动的属性:animation,尽管利用animation做出来的动画没有flash或者javascript做出的动画流畅绚丽,但是从代码量和浏览器性能上有着明显的优点. ani ...

  6. Ling之select

    select用法: 1.Dictionary<string, string>转json Newtonsoft.Json.JsonConvert.SerializeObject(dicSub ...

  7. 简单shell实现

    http://blog.csdn.net/lishuhuakai/article/details/11928055 #include <stdio.h> #include <unis ...

  8. PAT 1030 Travel Plan

    #include <cstdio> #include <cstdlib> #include <vector> #include <queue> #inc ...

  9. stark——pop功能(admin中添加功能)

    一.pop功能需要实现的功能和问题 1.如何在一对多和多对多字段后渲染 +2.+对应的跳转路径是什么3.保存添加记录同时,将原页面的对应的下拉菜单中添加该记录 二.window.open()方法详解 ...

  10. arcgis C#判断点在线段的左右侧

    要判断一个点在一条线段的左侧还是右侧,从网上查到了这样一个算法.其实本质上就是一个sin角度的计算问题. 设线段端点为从A(x1,y1)到B(x2,y2),线外一点P(x0,y0),判断该点位于有向线 ...