安卓MVC框架中M层是负责什么操作的
获取数据
安卓中MVC框架中V层是负责什么操作的
更新UI
安卓中MVC框架中C层是什么
控制器
package com.example.administrator.myapplication.day1459;
import android.os.Handler;
import android.os.Message;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadThread extends Thread {
private String url;
private String path;
private Handler handler;
public DownloadThread(String url, String path, Handler handler) {
this.url = url;
this.path = path;
this.handler = handler;
}
@Override
public void run() {
super.run();
//网络下载
FileOutputStream fileOutputStream = null;
InputStream inputStream=null;
try {
URL url1 = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
if(urlConnection.getResponseCode()==200){
int max=urlConnection.getContentLength();
Message obtain1 = Message.obtain();
obtain1.what= FileNetModel.DOWNLOAD_MAX;
obtain1.obj=max;
handler.sendMessage(obtain1);
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream(path);
//边读边写
byte[] bytes=new byte[1024];
int len=0;
int count=0;//下载进度
while((len=inputStream.read(bytes))!=-1){
Thread.sleep(50);
count+=len;
fileOutputStream.write(bytes,0,len);
Message obtain = Message.obtain();
obtain.what= FileNetModel.DOWNLOAD_PROGRESS;
obtain.obj=count;
handler.sendMessage(obtain);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {//关流
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.example.administrator.myapplication.day1459;
import android.os.Handler;
import android.os.Message;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DuanDianThread extends Thread {
private String url;
private String path;
private Handler handler;
int max;
int start;
int end;
public DuanDianThread(String url, String path, Handler handler) {
this.url = url;
this.path = path;
this.handler = handler;
}
@Override
public void run() {
super.run();
//TODO 1:要总大小
try {
URL url1 = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
max= urlConnection.getContentLength();
Message obtain1 = Message.obtain();
obtain1.what= FileNetModel.DUANDIAN_MAX;
obtain1.obj=max;
handler.sendMessage(obtain1);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
File file = new File(path);
if(file.exists()){
start= (int) file.length();
}
end=max;
URL url1 = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
//设置Range:bytes=0-100
urlConnection.setRequestProperty("Range","bytes="+start+"-"+end+"");
if(urlConnection.getResponseCode()==206){
InputStream inputStream = urlConnection.getInputStream();
//随机访问流
RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
randomAccessFile.seek(start);
byte[] bytes=new byte[1024];
int len=0;
int count=start;
while((len=inputStream.read(bytes))!=-1){
Thread.sleep(10);
count+=len;
randomAccessFile.write(bytes,0,len);
Message obtain1 = Message.obtain();
obtain1.what= FileNetModel.DUANDIAN_PROGRESS;
obtain1.obj=count;
handler.sendMessage(obtain1);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.example.administrator.myapplication.day1459;
import android.os.Handler;
public interface FileNetInterface {
public void download(String url, String path, Handler handler);
public void duandian(String url, String path, Handler handler);
public void upload(String url,String path,Handler handler,String serverName);
}
package com.example.administrator.myapplication.day1459;
import android.os.Handler;
class FileNetModel {
public static final int DOWNLOAD_MAX=101;//下载最大值
public static final int DOWNLOAD_PROGRESS=102;//下载进度
public static final int UPLOAD_MAX=201;//上传最大值
public static final int UPLOAD_PROGRESS=202;//上传进度
public static final int DUANDIAN_MAX=301;//上传最大值
public static final int DUANDIAN_PROGRESS=302;//上传进度
public void download(String url, String path, Handler handler) {
//开启线程下载数据,更新进度条
new DownloadThread(url,path,handler).start();
}
public void duandian(String url, String path, Handler handler) {
new DuanDianThread(url,path,handler).start();
}
public void upload(String url, String path, android.os.Handler handler, String serverName) {
new UploadThread(url,path,handler,serverName).start();
}
}
package com.example.administrator.myapplication.day1459;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import com.example.administrator.myapplication.R;
public class Main2Activity extends AppCompatActivity {
Button bt_download,bt_upload,bt_duandian;
ProgressBar bar1,bar2,bar3;
FileNetModel model=new FileNetModel();
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==FileNetModel.DOWNLOAD_MAX){
bar1.setMax((Integer) msg.obj);
}else if(msg.what==FileNetModel.DOWNLOAD_PROGRESS){
bar1.setProgress((Integer) msg.obj);
}else if(msg.what==FileNetModel.UPLOAD_MAX){
bar2.setMax((Integer) msg.obj);
}else if(msg.what==FileNetModel.UPLOAD_PROGRESS){
bar2.setProgress((Integer) msg.obj);
}else if(msg.what==FileNetModel.DUANDIAN_MAX){
bar3.setMax((Integer) msg.obj);
}else if(msg.what==FileNetModel.DUANDIAN_PROGRESS){
bar3.setProgress((Integer) msg.obj);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bt_download=findViewById(R.id.download);
bt_upload=findViewById(R.id.upload);
bt_duandian=findViewById(R.id.duandian);
bar1=findViewById(R.id.bar1);
bar2=findViewById(R.id.bar2);
bar3=findViewById(R.id.bar3);
bt_download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.download:
model.download("http://172.21.79.88/hfs/a.jpg","/sdcard/zhuzhu.jpg",handler);
break;
case R.id.upload:
model.upload("http://172.21.79.88/hfs","/sdcard/a.mp4",handler,"demoo.mp4");
break;
case R.id.duandian:
model.duandian("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4","/sdcard/duandian.mp4",handler);
break;
}
}
});
bt_upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.download:
model.download("http://172.21.79.88/hfs/a.jpg","/sdcard/zhuzhu.jpg",handler);
break;
case R.id.upload:
model.upload("http://172.21.79.88/hfs","/sdcard/a.mp4",handler,"demoo.mp4");
break;
case R.id.duandian:
model.duandian("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4","/sdcard/duandian.mp4",handler);
break;
}
}
});
bt_duandian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// case R.id.download:
// model.download(“http://172.21.79.88/hfs/a.jpg","/sdcard/zhuzhu.jpg”,handler);
// break;
// case R.id.upload:
// model.upload(“http://172.21.79.88/hfs","/sdcard/a.mp4",handler,"demoo.mp4”);
// break;
// case R.id.duandian:
// model.duandian(“http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4","/sdcard/duandian.mp4”,handler);
// break;
}
});
}
}
package com.example.administrator.myapplication.day1459;
import android.os.Handler;
import android.os.Message;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class UploadThread extends Thread {
private String url;
private String path;
private Handler handler;
private String serverName;
public UploadThread(String url, String path, Handler handler, String serverName) {
this.url = url;
this.path = path;
this.handler = handler;
this.serverName = serverName;
}
@Override
public void run() {
super.run();
URL url1 = null;
try {
//总大小
Message obtain1 = Message.obtain();
obtain1.what= FileNetModel.UPLOAD_MAX;
obtain1.obj=(int)new File(path).length();
handler.sendMessage(obtain1);
url1 = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
StringBuffer sb = new StringBuffer();
sb.append("-----------------------------7e324741816d4"+"\r\n");
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\""+serverName+"\""+"\r\n");
sb.append("Content-Type: media/mp4" + "\r\n");
sb.append("\r\n");
byte[] bytes = sb.toString().getBytes("UTF-8");
urlConnection.setRequestProperty("Content-Length",bytes.length+new File(path).length()+"");
urlConnection.setRequestProperty("Content-Type","multipart/form-data; boundary=7e324741816d4");
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
OutputStream outputStream = urlConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(path);
outputStream.write(bytes);
byte[] bytes1=new byte[1024];
int len=0;
int count=0;
while((len=fileInputStream.read(bytes1))!=-1){
Thread.sleep(10);
count+=len;
Message obtain = Message.obtain();
obtain.what= FileNetModel.UPLOAD_PROGRESS;
obtain.obj=count;
handler.sendMessage(obtain);
outputStream.write(bytes1,0,len);
}
if (urlConnection.getResponseCode() == 200) {
System.out.println("上传成功");
}
} catch (MalformedURLException e) {
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}