- 浏览: 78482 次
最新评论
-
zhangjie3140:
感谢分享
tabhost里解决按键事件捕获的问题 -
honghe:
请问我重写了 onCreateWindow,但处理window ...
在webview中访问互联网应用,碰到弹出窗口怎么办? -
肖默it:
解决了我的问题,感谢楼主的分享!
tabhost里解决按键事件捕获的问题 -
hecal:
太感谢了~~
tabhost里解决按键事件捕获的问题 -
guogzhao:
注意在if判断中要加一个event.getAction() = ...
tabhost里解决按键事件捕获的问题
最近做一个文件上传和下载的应用对文件上传和下载进行了一个完整的流程分析
以来是方便自己对文件上传和下载的理解,而来便于团队内部的分享
故而做了几张图,将整体的流程都画下来,便于大家的理解和分析,如果有不完善的地方希望
大家多提意见,
由于参考了网上许多的资料,特此感谢
首先是文件上传,这个要用到服务器
7 天前 上传
下载附件 (40.37 KB)
关键代码:
FileServer.java
Java代码
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- 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.Set;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import util.FileLogInfo;
- import util.StreamTool;
- public class FileServer {
- private ExecutorService executorService;//线程池
- private int port;//监听端口
- private boolean quit = false;//退出
- private ServerSocket server;
- private Map<Long, FileLogInfo> datas = new HashMap<Long, FileLogInfo>();//存放断点数据,以后改为数据库存放
- 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)
- {
- e.printStackTrace();
- }
- }
-
- /**
- * 启动服务
- * @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;
- }
- @Override
- public void run()
- {
- try
- {
- System.out.println("FileServer accepted connection "+ socket.getInetAddress()+ ":"+ socket.getPort());
- //得到客户端发来的第一行协议数据:Content-Length=143253434;filename=xxx.3gp;sourceid=
- //如果用户初次上传文件,sourceid的值为空。
- InputStream inStream = socket.getInputStream();
- String head = StreamTool.readLine(inStream);
- System.out.println("FileServer head:"+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);
- //生成资源id,如果需要唯一性,可以采用UUID
- long id = System.currentTimeMillis();
- FileLogInfo log = null;
- if(sourceid!=null && !"".equals(sourceid))
- {
- id = Long.valueOf(sourceid);
- //查找上传的文件是否存在上传记录
- log = find(id);
- }
- File file = null;
- int position = 0;
- //如果上传的文件不存在上传记录,为文件添加跟踪记录
- if(log==null)
- {
- //设置存放的位置与当前应用的位置有关
- File dir = new File("c:/temp/");
- 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
- {
- System.out.println("FileServer have exits log not null");
- //从上传记录中得到文件的路径
- 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+ "%";
- //服务器收到客户端的请求信息后,给客户端返回响应信息:sourceid=1274773833264;position=position
- //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)
- {
- e.printStackTrace();
- }
- }
- }
- }
-
- /**
- * 查找在记录中是否有sourceid的文件
- * @param sourceid
- * @return
- */
- public FileLogInfo find(Long sourceid)
- {
- return datas.get(sourceid);
- }
-
- /**
- * 保存上传记录,日后可以改成通过数据库存放
- * @param id
- * @param saveFile
- */
- public void save(Long id, File saveFile)
- {
- System.out.println("save logfile "+id);
- datas.put(id, new FileLogInfo(id, saveFile.getAbsolutePath()));
- }
-
- /**
- * 当文件上传完毕,删除记录
- * @param sourceid
- */
- public void delete(long sourceid)
- {
- System.out.println("delete logfile "+sourceid);
- if(datas.containsKey(sourceid)) datas.remove(sourceid);
- }
-
- }
由于在上面的流程图中已经进行了详细的分析,我在这儿就不讲了,只是在存储数据的时候服务器没有用数据库去存储,这儿只是为了方便,所以要想测试断点上传,服务器是不能停的,否则数据就没有了,在以后改进的时候应该用数据库去存储数据。
文件上传客户端:
7 天前 上传
下载附件 (38.92 KB)
关键代码:
UploadActivity.java
Java代码
- package com.hao;
- import java.io.File;
- import java.util.List;
- import com.hao.upload.UploadThread;
- import com.hao.upload.UploadThread.UploadProgressListener;
- import com.hao.util.ConstantValues;
- import com.hao.util.FileBrowserActivity;
- import android.app.Activity;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.res.Resources;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- *
- * @author Administrator
- *
- */
- public class UploadActivity extends Activity implements OnClickListener{
- private static final String TAG = "SiteFileFetchActivity";
- private Button download, upload, select_file;
- private TextView info;
- private static final int PROGRESS_DIALOG = 0;
- private ProgressDialog progressDialog;
- private UploadThread uploadThread;
- private String uploadFilePath = null;
- private String fileName;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.upload);
- initView();
- }
-
- private void initView(){
- download = (Button) findViewById(R.id.download);
- download.setOnClickListener(this);
- upload = (Button) findViewById(R.id.upload);
- upload.setOnClickListener(this);
- info = (TextView) findViewById(R.id.info);
- select_file = (Button) findViewById(R.id.select_file);
- select_file.setOnClickListener(this);
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK) {
- if (requestCode == 1) {
- Uri uri = data.getData(); // 接收用户所选文件的路径
- info.setText("select: " + uri); // 在界面上显示路径
- uploadFilePath = uri.getPath();
- int last = uploadFilePath.lastIndexOf("/");
- uploadFilePath = uri.getPath().substring(0, last+1);
- fileName = uri.getLastPathSegment();
- }
- }
- }
-
- protected Dialog onCreateDialog(int id) {
- switch(id) {
- case PROGRESS_DIALOG:
- progressDialog = new ProgressDialog(UploadActivity.this);
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progressDialog.setButton("暂停", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- uploadThread.closeLink();
- dialog.dismiss();
- }
- });
- progressDialog.setMessage("正在上传...");
- progressDialog.setMax(100);
- return progressDialog;
- default:
- return null;
- }
- }
-
- /**
- * 使用Handler给创建他的线程发送消息,
- * 匿名内部类
- */
- private Handler handler = new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- //获得上传长度的进度
- int length = msg.getData().getInt("size");
- progressDialog.setProgress(length);
- if(progressDialog.getProgress()==progressDialog.getMax())//上传成功
- {
- progressDialog.dismiss();
- Toast.makeText(UploadActivity.this, getResources().getString(R.string.upload_over), 1).show();
- }
- }
- };
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Resources r = getResources();
- switch(v.getId()){
- case R.id.select_file:
- Intent intent = new Intent();
- //设置起始目录和查找的类型
- intent.setDataAndType(Uri.fromFile(new File("/sdcard")), "*/*");//"*/*"表示所有类型,设置起始文件夹和文件类型
- intent.setClass(UploadActivity.this, FileBrowserActivity.class);
- startActivityForResult(intent, 1);
- break;
- case R.id.download:
- startActivity(new Intent(UploadActivity.this, SmartDownloadActivity.class));
- break;
- case R.id.upload:
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))//判断SDCard是否存在
- {
- if(uploadFilePath == null){
- Toast.makeText(UploadActivity.this, "还没设置上传文件", 1).show();
- }
- System.out.println("uploadFilePath:"+uploadFilePath+" "+fileName);
- //取得SDCard的目录
- File uploadFile = new File(new File(uploadFilePath), fileName);
- Log.i(TAG, "filePath:"+uploadFile.toString());
- if(uploadFile.exists())
- {
- showDialog(PROGRESS_DIALOG);
- info.setText(uploadFile+" "+ConstantValues.HOST+":"+ConstantValues.PORT);
- progressDialog.setMax((int) uploadFile.length());//设置长传文件的最大刻度
- uploadThread = new UploadThread(UploadActivity.this, uploadFile, ConstantValues.HOST, ConstantValues.PORT);
- uploadThread.setListener(new UploadProgressListener() {
-
- @Override
- public void onUploadSize(int size) {
- // TODO Auto-generated method stub
- Message msg = new Message();
- msg.getData().putInt("size", size);
- handler.sendMessage(msg);
- }
- });
- uploadThread.start();
- }
- else
- {
- Toast.makeText(UploadActivity.this, "文件不存在", 1).show();
- }
- }
- else
- {
- Toast.makeText(UploadActivity.this, "SDCard不存在!", 1).show();
- }
- break;
- }
-
- }
-
-
- }
UploadThread.java
Java代码
- package com.hao.upload;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.RandomAccessFile;
- import java.net.Socket;
- import android.content.Context;
- import android.util.Log;
- import com.hao.db.UploadLogService;
- import com.hao.util.StreamTool;
- public class UploadThread extends Thread {
- private static final String TAG = "UploadThread";
- /*需要上传文件的路径*/
- private File uploadFile;
- /*上传文件服务器的IP地址*/
- private String dstName;
- /*上传服务器端口号*/
- private int dstPort;
- /*上传socket链接*/
- private Socket socket;
- /*存储上传的数据库*/
- private UploadLogService logService;
- private UploadProgressListener listener;
- public UploadThread(Context context, File uploadFile, final String dstName,final int dstPort){
- this.uploadFile = uploadFile;
- this.dstName = dstName;
- this.dstPort = dstPort;
- logService = new UploadLogService(context);
- }
-
- public void setListener(UploadProgressListener listener) {
- this.listener = listener;
- }
- /**
- * 模拟断开连接
- */
- public void closeLink(){
- try{
- if(socket != null) socket.close();
- }catch(IOException e){
- e.printStackTrace();
- Log.e(TAG, "close socket fail");
- }
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- // 判断文件是否已有上传记录
- String souceid = logService.getBindId(uploadFile);
- // 构造拼接协议
- String head = "Content-Length=" + uploadFile.length()
- + ";filename=" + uploadFile.getName() + ";sourceid="
- + (souceid == null ? "" : souceid) + "%";
- // 通过Socket取得输出流
- socket = new Socket(dstName, dstPort);
- OutputStream outStream = socket.getOutputStream();
- outStream.write(head.getBytes());
- Log.i(TAG, "write to outStream");
- InputStream inStream = socket.getInputStream();
- // 获取到字符流的id与位置
- String response = StreamTool.readLine(inStream);
- Log.i(TAG, "response:" + response);
- 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 ((len = fileOutStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- // 设置长传数据长度
- length += len;
- listener.onUploadSize(length);
- }
- fileOutStream.close();
- outStream.close();
- inStream.close();
- socket.close();
- // 判断上传完则删除数据
- if (length == uploadFile.length())
- logService.delete(uploadFile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public interface UploadProgressListener{
- void onUploadSize(int size);
- }
- }
下面是多线程下载
7 天前 上传
下载附件 (34.38 KB)
SmartDownloadActivity.java
Java代码
- package com.hao;
- import java.io.File;
- import com.hao.R;
- import com.hao.R.id;
- import com.hao.R.layout;
- import com.hao.download.SmartFileDownloader;
- import com.hao.download.SmartFileDownloader.SmartDownloadProgressListener;
- import com.hao.util.ConstantValues;
- 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.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- *
- * @author Administrator
- *
- */
- public class SmartDownloadActivity extends Activity {
- private ProgressBar downloadbar;
- private TextView resultView;
- private String path = ConstantValues.DOWNLOAD_URL;
- SmartFileDownloader loader;
- private Handler handler = new Handler() {
- @Override
- // 信息
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- int size = msg.getData().getInt("size");
- downloadbar.setProgress(size);
- float result = (float) downloadbar.getProgress() / (float) downloadbar.getMax();
- int p = (int) (result * 100);
- resultView.setText(p + "%");
- if (downloadbar.getProgress() == downloadbar.getMax())
- Toast.makeText(SmartDownloadActivity.this, "下载成功", 1).show();
- break;
- case -1:
- Toast.makeText(SmartDownloadActivity.this, msg.getData().getString("error"), 1).show();
- break;
- }
- }
- };
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.download);
- Button button = (Button) this.findViewById(R.id.button);
- Button closeConn = (Button) findViewById(R.id.closeConn);
- closeConn.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(loader != null){
- finish();
- }else{
- Toast.makeText(SmartDownloadActivity.this, "还没有开始下载,不能暂停", 1).show();
- }
- }
- });
- downloadbar = (ProgressBar) this.findViewById(R.id.downloadbar);
- resultView = (TextView) this.findViewById(R.id.result);
- resultView.setText(path);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- download(path, ConstantValues.FILE_PATH);
- } else {
- Toast.makeText(SmartDownloadActivity.this, "没有SDCard", 1).show();
- }
- }
- });
- }
- // 对于UI控件的更新只能由主线程(UI线程)负责,如果在非UI线程更新UI控件,更新的结果不会反映在屏幕上,某些控件还会出错
- private void download(final String path, final File dir) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- loader = new SmartFileDownloader(SmartDownloadActivity.this, path, dir, 3);
- int length = loader.getFileSize();// 获取文件的长度
- downloadbar.setMax(length);
- loader.download(new SmartDownloadProgressListener() {
- @Override
- public void onDownloadSize(int size) {// 可以实时得到文件下载的长度
- Message msg = new Message();
- msg.what = 1;
- msg.getData().putInt("size", size);
- handler.sendMessage(msg);
- }
- });
- } catch (Exception e) {
- Message msg = new Message();// 信息提示
- msg.what = -1;
- msg.getData().putString("error", "下载失败");// 如果下载错误,显示提示失败!
- handler.sendMessage(msg);
- }
- }
- }).start();// 开始
- }
- }
这个单个的下载线程
SmartDownloadThread.java
Java代码
- package com.hao.download;
- import java.io.File;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.util.Log;
- /**
- * 线程下载
- * @author Administrator
- *
- */
- public class SmartDownloadThread extends Thread {
- private static final String TAG = "SmartDownloadThread";
- private File saveFile;
- private URL downUrl;
- private int block;
- /* *下载开始位置 */
- private int threadId = -1;
- private int downLength;
- private boolean finish = false;
- private SmartFileDownloader downloader;
- public SmartDownloadThread(SmartFileDownloader downloader, URL downUrl,
- File saveFile, int block, int downLength, int threadId) {
- this.downUrl = downUrl;
- this.saveFile = saveFile;
- this.block = block;
- this.downloader = downloader;
- this.threadId = threadId;
- this.downLength = downLength;
- }
- @Override
- public void run() {
- if (downLength < block) {// 未下载完成
- try {
- HttpURLConnection http = (HttpURLConnection) downUrl
- .openConnection();
- http.setConnectTimeout(5 * 1000);
- http.setRequestMethod("GET");
- http.setRequestProperty("Accept","image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
- http.setRequestProperty("Accept-Language", "zh-CN");
- http.setRequestProperty("Referer", downUrl.toString());
- http.setRequestProperty("Charset", "UTF-8");
- int startPos = block * (threadId - 1) + downLength;// 开始位置
- int endPos = block * threadId - 1;// 结束位置
- http.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);// 设置获取实体数据的范围
- http.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
- http.setRequestProperty("Connection", "Keep-Alive");
- InputStream inStream = http.getInputStream();
- byte[] buffer = new byte[1024];
- int offset = 0;
- print("Thread " + this.threadId + " start download from position " + startPos);
- RandomAccessFile threadfile = new RandomAccessFile(this.saveFile, "rwd");
- threadfile.seek(startPos);
- while ((offset = inStream.read(buffer, 0, 1024)) != -1) {
- threadfile.write(buffer, 0, offset);
- downLength += offset;
- downloader.update(this.threadId, downLength);
- downloader.saveLogFile();
- downloader.append(offset);
- }
- threadfile.close();
- inStream.close();
- print("Thread " + this.threadId + " download finish");
- this.finish = true;
- } catch (Exception e) {
- this.downLength = -1;
- print("Thread " + this.threadId + ":" + e);
- }
- }
- }
- private static void print(String msg) {
- Log.i(TAG, msg);
- }
- /**
- * 下载是否完成
- * @return
- */
- public boolean isFinish() {
- return finish;
- }
- /**
- * 已经下载的内容大小
- * @return 如果返回值为-1,代表下载失败
- */
- public long getDownLength() {
- return downLength;
- }
- }
总得下载线程
SmartFileDownloader.java
Java代码
- package com.hao.download;
- import java.io.File;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.UUID;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import com.hao.db.DownloadFileService;
- import android.content.Context;
- import android.util.Log;
- /**
- * 文件下载主程序
- * @author Administrator
- *
- */
- public class SmartFileDownloader {
- private static final String TAG = "SmartFileDownloader";
- private Context context;
- private DownloadFileService fileService;
- /* 已下载文件长度 */
- private int downloadSize = 0;
- /* 原始文件长度 */
- private int fileSize = 0;
- /*原始文件名*/
- private String fileName;
- /* 线程数 */
- private SmartDownloadThread[] threads;
- /* 本地保存文件 */
- private File saveFile;
- /* 缓存各线程下载的长度 */
- private Map<Integer, Integer> data = new ConcurrentHashMap<Integer, Integer>();
- /* 每条线程下载的长度 */
- private int block;
- /* 下载路径 */
- private String downloadUrl;
- /**
- * 获取文件名
- */
- public String getFileName(){
- return this.fileName;
- }
- /**
- * 获取线程数
- */
- public int getThreadSize() {
- return threads.length;
- }
- /**
- * 获取文件大小
- * @return
- */
- public int getFileSize() {
- return fileSize;
- }
- /**
- * 累计已下载大小
- * @param size
- */
- protected synchronized void append(int size) {
- downloadSize += size;
- }
- /**
- * 更新指定线程最后下载的位置
- * @param threadId 线程id
- * @param pos 最后下载的位置
- */
- protected void update(int threadId, int pos) {
- this.data.put(threadId, pos);
- }
- /**
- * 保存记录文件
- */
- protected synchronized void saveLogFile() {
- this.fileService.update(this.downloadUrl, this.data);
- }
- /**
- * 构建文件下载器
- * @param downloadUrl 下载路径
- * @param fileSaveDir 文件保存目录
- * @param threadNum 下载线程数
- */
- public SmartFileDownloader(Context context, String downloadUrl,
- File fileSaveDir, int threadNum) {
- try {
- this.context = context;
- this.downloadUrl = downloadUrl;
- fileService = new DownloadFileService(this.context);
- URL url = new URL(this.downloadUrl);
- if (!fileSaveDir.exists()) fileSaveDir.mkdirs();
- this.threads = new SmartDownloadThread[threadNum];
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5 * 1000);
- conn.setRequestMethod("GET");
- conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
- conn.setRequestProperty("Accept-Language", "zh-CN");
- conn.setRequestProperty("Referer", downloadUrl);
- conn.setRequestProperty("Charset", "UTF-8");
- conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.connect();
- printResponseHeader(conn);
- if (conn.getResponseCode() == 200) {
- this.fileSize = conn.getContentLength();// 根据响应获取文件大小
- if (this.fileSize <= 0)
- throw new RuntimeException("Unkown file size ");
- fileName = getFileName(conn);
- this.saveFile = new File(fileSaveDir, fileName);/* 保存文件 */
- Map<Integer, Integer> logdata = fileService.getData(downloadUrl);
- if (logdata.size() > 0) {
- for (Map.Entry<Integer, Integer> entry : logdata.entrySet())
- data.put(entry.getKey(), entry.getValue());
- }
- //划分每个线程下载文件长度
- this.block = (this.fileSize % this.threads.length) == 0 ? this.fileSize / this.threads.length
- : this.fileSize / this.threads.length + 1;
- if (this.data.size() == this.threads.length) {
- for (int i = 0; i < this.threads.length; i++) {
- this.downloadSize += this.data.get(i + 1);
- }
- print("已经下载的长度" + this.downloadSize);
- }
- } else {
- throw new RuntimeException("server no response ");
- }
- } catch (Exception e) {
- print(e.toString());
- throw new RuntimeException("don't connection this url");
- }
- }
- /**
- * 获取文件名
- */
- private String getFileName(HttpURLConnection conn) {
- String filename = this.downloadUrl.substring(this.downloadUrl.lastIndexOf('/') + 1);//链接的最后一个/就是文件名
- if (filename == null || "".equals(filename.trim())) {// 如果获取不到文件名称
- for (int i = 0;; i++) {
- String mine = conn.getHeaderField(i);
- print("ConnHeader:"+mine+" ");
- if (mine == null)
- break;
- if ("content-disposition".equals(conn.getHeaderFieldKey(i).toLowerCase())) {
- Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase());
- if (m.find())
- return m.group(1);
- }
- }
- filename = UUID.randomUUID() + ".tmp";// 默认取一个文件名
- }
- return filename;
- }
- /**
- * 开始下载文件
- *
- * @param listener
- * 监听下载数量的变化,如果不需要了解实时下载的数量,可以设置为null
- * @return 已下载文件大小
- * @throws Exception
- */
- public int download(SmartDownloadProgressListener listener)
- throws Exception {
- try {
- RandomAccessFile randOut = new RandomAccessFile(this.saveFile, "rw");
- if (this.fileSize > 0)
- randOut.setLength(this.fileSize);
- randOut.close();
- URL url = new URL(this.downloadUrl);
- if (this.data.size() != this.threads.length) {
- this.data.clear();// 清除数据
- for (int i = 0; i < this.threads.length; i++) {
- this.data.put(i + 1, 0);
- }
- }
- for (int i = 0; i < this.threads.length; i++) {
- int downLength = this.data.get(i + 1);
- if (downLength < this.block && this.downloadSize < this.fileSize) { // 该线程未完成下载时,继续下载
- this.threads[i] = new SmartDownloadThread(this, url,
- this.saveFile, this.block, this.data.get(i + 1), i + 1);
- this.threads[i].setPriority(7);
- this.threads[i].start();
- } else {
- this.threads[i] = null;
- }
- }
- this.fileService.save(this.downloadUrl, this.data);
- boolean notFinish = true;// 下载未完成
- while (notFinish) {// 循环判断是否下载完毕
- Thread.sleep(900);
- notFinish = false;// 假定下载完成
- for (int i = 0; i < this.threads.length; i++) {
- if (this.threads[i] != null && !this.threads[i].isFinish()) {
- notFinish = true;// 下载没有完成
- if (this.threads[i].getDownLength() == -1) {// 如果下载失败,再重新下载
- this.threads[i] = new SmartDownloadThread(this,
- url, this.saveFile, this.block, this.data.get(i + 1), i + 1);
- this.threads[i].setPriority(7);
- this.threads[i].start();
- }
- }
- }
- if (listener != null)
- listener.onDownloadSize(this.downloadSize);
- }
- fileService.delete(this.downloadUrl);
- } catch (Exception e) {
- print(e.toString());
- throw new Exception("file download fail");
- }
- return this.downloadSize;
- }
- /**
- * 获取Http响应头字段
- *
- * @param http
- * @return
- */
- public static Map<String, String> getHttpResponseHeader(
- HttpURLConnection http) {
- Map<String, String> header = new LinkedHashMap<String, String>();
- for (int i = 0;; i++) {
- String mine = http.getHeaderField(i);
- if (mine == null)
- break;
- header.put(http.getHeaderFieldKey(i), mine);
- }
- return header;
- }
- /**
- * 打印Http头字段
- *
- * @param http
- */
- public static void printResponseHeader(HttpURLConnection http) {
- Map<String, String> header = getHttpResponseHeader(http);
- for (Map.Entry<String, String> entry : header.entrySet()) {
- String key = entry.getKey() != null ? entry.getKey() + ":" : "";
- print(key + entry.getValue());
- }
- }
- // 打印日志
- private static void print(String msg) {
- Log.i(TAG, msg);
- }
- public interface SmartDownloadProgressListener {
- public void onDownloadSize(int size);
- }
- }
好了这里只是将主要的代码分享出来,主要是为了了解他的基本流程,然后自己可以扩展,和优化
发表评论
-
<转载>实现android的AES加密技术
2012-01-06 17:29 661原文地址:实现android的AES加密技术作者:大牛 ... -
致国内提供安卓应用内容服务商的一封信
2011-12-31 11:07 161安卓应用内容服务商是指提供安卓客户端下载,通过安卓客户端可以访 ... -
致国内1000万中小安卓应用开发者的一封信
2011-12-30 14:53 258基于安卓应用持久健康的发展,以及保护依赖广告嵌入来获得收入的广 ... -
致国内移动广告平台、广告聚合平台及统计平台的一封信
2011-12-30 14:49 1014基于安卓应用持久健康的发展,以及保护依赖广告嵌入来获得收入的广 ... -
致国内提供安卓应用内容服务商的一封信
2011-12-30 14:29 623安卓应用内容服务商是指提供安卓客户端下载,通过安卓客户端可以访 ... -
致国内1000万中小安卓应用开发者的一封信
2011-12-30 14:18 4基于安卓应用持久健康的发展,以及保护依赖广告嵌入来获得收入的广 ... -
致国内移动广告平台、广告聚合平台及统计平台的一封信
2011-12-30 14:13 3基于安卓应用持久健康的发展,以及保护依赖广告嵌入来获得收入的广 ... -
《转载》Android大TXT文本文档读取(续)
2011-12-20 10:32 1162原文地址:http://wangliping.net/read ... -
《转载》Android大TXT文本文档读取(1)
2011-12-20 10:30 3570原文地址:http://wangliping.net/read ... -
文件 批量重命名
2011-12-19 20:59 924前几天要处理一些图片资料,从看到有人提供了一个批处理的方法,但 ... -
发布应用的一点小感想(一)
2011-12-09 18:08 772<!-- @pa ... -
博客文章搬家成功
2011-12-08 11:33 730博客文章从csdn上搬家成功了~~ 不过博客分类没有导入进来 ... -
android中嵌入广告的小发现
2011-12-07 18:03 1012虽是个新手,前段时间 ... -
android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
2011-12-07 17:26 2171以下代码未经测试。但是有我想找的,先贴过来,慢慢了解~~ i ... -
移动互联网个体户
2011-12-06 11:07 1127在网上查了查,现在 还没有人提出过移动互联网个体户这个概念,以 ... -
tabhost里解决按键事件捕获的问题
2011-12-03 16:58 3940这几天参照好多人的代码,改造了一个小程序,咱也使用一下TabH ... -
Android中Fragment的应用
2011-11-21 18:11 805http://blog.sina.com.cn/s/blog_ ... -
在webview中访问互联网应用,碰到弹出窗口怎么办?
2011-11-21 15:12 8736webview加载了一个页面,页面里点某一个button时候弹 ...
相关推荐
在Android开发中,实现断点续传功能是一项重要的技术,特别是在处理大文件下载时,能够提高用户体验并节约网络资源。断点续传是指在下载文件过程中,如果因为网络中断或其他原因导致下载暂停,用户可以在稍后的时间...
在实际开发中,还可以考虑使用第三方库如Volley、OkHttp或Retrofit等,它们提供了更方便的网络请求和下载管理功能,可以简化多线程和断点续传的实现。 综合以上知识点,我们可以构建一个功能完善的多线程断点续传...
基于andriod多线程断点续传下载.zip 功能介绍: **1、**下拉刷新发送下载请求给service端,等待接收到应答后,解析返回的json数据并显示在Listview中; **2、**点击【下载】下载该文件,点击【暂停】暂停下载; **3...
本项目"安卓Andriod源码——多线程断点续传下载+在线播放音乐.zip"提供了一个完整的示例,它涵盖了两个关键的技术:多线程断点续传下载和在线播放音乐。下面我们将深入探讨这两个主题。 1. 多线程断点续传下载: ...
"安卓Andriod源码——一个支持暂停下载断点续传的源码.zip"这个资源提供了一个实现这种功能的示例代码,即在下载过程中支持暂停和断点续传。 首先,我们来理解一下“断点续传”这一概念。断点续传是文件下载的一种...
List<HashMap> datas = new ArrayList<>(); // 这里应该有逻辑处理,比如数据库查询等 return datas; } ``` #### 1.2 WSDL配置 服务端还需要通过WSDL(Web Services Description Language)文件来描述其接口,...
<br> Parameters parameters = camera.getParameters();<br> parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);<br> camera.setParameters(parameters);<br> camera.startPreview();<br> 关闭闪光灯 ...
通过阅读和分析源代码,开发者可以深入理解断点续传的实现细节,从而在自己的项目中应用类似的技术。 总的来说,断点续传是Android应用程序中一个实用且复杂的特性,涉及网络请求、文件操作、多线程和状态管理等多...
用andriod实现的查询天气预报 package cn.Wongxming.weather.utils; import java.util.ArrayList; public class WeatherSet { private WeatherCurrentCondition myCurrentCondition = null; private ...
<name>Hello, Andriod</name> <price>100.00</price> </book> </books> ``` 为了解析这个文件,我们可以创建一个`Book`类,包含`id`、`name`和`price`字段: ```java public class Book { private int id; ...
jSerialComm Java的独立于平台的串行端口访问库。 用法 有关用法示例,请参阅“ 。 ... 。 另外,您可以自动将jSerialComm作为Maven Central ... <artifactId>jSerialComm</artifactId> <version>[2.0.0,3.0.0
在实际开发中,我们可以使用Android提供的DownloadManager服务,它已经内置了多线程和断点续传功能,但若需要自定义更复杂的逻辑,如限制并发线程数、动态调整下载速度等,就需要自己实现多线程断点下载逻辑。...
- `mv <源文件> <目标文件>`:移动或重命名文件,`mv -r <源目录> <目标目录>`:移动目录。 3. **进程和权限管理**: - `ps`:查看当前运行的进程。 - `kill <进程号>`:杀死指定进程。 - `su`:切换到root用户...
本资源提供对于ViewGroup下所以子view的遍历,以及调用Views都过tag或者Widget的类查找返回一个List<View> 使用方法可见示例: 子View遍历 ViewGroup root = (ViewGroup) findViewById(android.R.id.content); ...
通过这个Andriod Retrofit demo,开发者可以深入理解Retrofit的工作原理,学习如何构建RESTful API客户端,以及如何处理网络请求的各个方面。这不仅有助于提高开发效率,还能让代码更加整洁、易于维护。
叉编译好的ARM 板 redis-server 5.0,可直接放板上linux/andriod跑 编译对高手不难,但对于小白来讲还是挺麻烦的,一个小错误就会导致中断。今天我帮你编译好了,拿去吧。 运行: 放到系统中: chmod +x redis-...
- **数据备份**:`adb backup [-all] [-apk|-noapk] [-shared|-noshares] [-obb|-noobb] [-system|-nosystem] [-include <file-list>] [-f <backup-file>]`可以备份用户数据和系统设置。 - **数据恢复**:`adb ...
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { // 请求成功处理 } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { // 请求失败处理 } }...
<item name="android:windowAnimationStyle">@style/noAnimation</item> <item name="android:windowNoTitle">true</item> </style> <style name="noAnimation"> <item name="android:...
在这个例子中,`<LinearLayout>`被设置为垂直方向,其内部包含了一个`ImageView`和另一个`LinearLayout`,后者又包含了两个`TextView`。 在`RelativeLayout`中,我们可以看到`android:layout_width`和`android:...