安卓课程设计--利用socket在电脑和手机之间实现简单云盘
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓课程设计--利用socket在电脑和手机之间实现简单云盘相关的知识,希望对你有一定的参考价值。
下面是服务端的代码
package lyj.srever;
public class Server {
public static void main(String[] args) {
new ServerThread().start();
}
}
package lyj.srever;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class ServerThread extends Thread{
public static ArrayList<Socket> sockets=new ArrayList<Socket>();
public ServerThread(){};
private ArrayList<MyFile> data=new ArrayList<MyFile>();
private File[] files;
private int size;
public ArrayList<Socket> getsSockets(){return sockets;}
@Override
public void run() {
try {
getFile();
//服务端开启
ServerSocket server=new ServerSocket(9999);
System.out.println("服务端开启");
while(true){
//接收客户端
Socket socket=server.accept();
//将socket传递给一个线程处理
if(sockets.size()==0){
new SocketThread(socket, data, size);
sockets.add(0,socket);
}else{
sockets.add(1,socket);
new SocketManager(socket, data);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 服务器端的文件
* @return
*/
public void getFile(){
File folder=new File("E:\\云盘");
files=folder.listFiles();
size=files.length;
for(int i=0;i<size;i++){
File file=files[i];
MyFile myfile=new MyFile();
myfile.setName(file.getName());
myfile.setSize(file.length());
myfile.setUri(file.getPath());
data.add(myfile);
}
}
}
package lyj.srever;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
public class SocketThread extends Thread{
private Socket socket;
private File[] files;
private int size;
private ArrayList<MyFile> data;
private OutputStream ops;
public SocketThread(Socket socket,ArrayList<MyFile> data,int size){
this.socket=socket;
this.data=data;
this.size=size;
start();
}
@Override
public void run() {
try {
ops=socket.getOutputStream();
String temp1="";
for(int i=0;i<size;i++){
String str=data.get(i).getName()+"]"+data.get(i).getSize()+"\r\n";
temp1=temp1+str;
}
ops.write(temp1.getBytes("utf-8"));
ops.flush();
System.out.println("数据发送完毕");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
package lyj.srever;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class SocketManager extends Thread{
private Socket socket;
private ArrayList<MyFile> data;
public SocketManager(Socket socket,ArrayList<MyFile> data){
this.socket=socket;
this.data=data;
start();
}
@Override
public void run() {
BufferedInputStream bis;
try {
bis = new BufferedInputStream(socket.getInputStream());
InputStreamReader isr=new InputStreamReader(bis,"utf-8");
BufferedReader br=new BufferedReader(isr);
String temp="";
try {
while((temp=br.readLine())!=null){
String str=temp.substring(0,2);
int index=Integer.parseInt(temp.substring(2));
System.out.println(index);
if(str.equals("下载")){
// ArrayList<Socket> sockets=ServerThread.sockets;
// System.out.println(sockets.size());
// System.out.println(sockets.get(0).getPort());
new DownLoadThread(socket, index, data);
}else if(str.equals("上传")){
new UpLoadThread();
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package lyj.srever;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
public class DownLoadThread extends Thread {
private Socket socket;
private int index;
private ArrayList<MyFile> data;
public DownLoadThread(Socket socket, int index, ArrayList<MyFile> data) {
this.index = index;
this.socket = socket;
this.data = data;
start();
}
@Override
public void run() {
try {
File file = new File(data.get(index).getUri());
System.out.println(file.getAbsolutePath());
FileInputStream fis = new FileInputStream(file);
System.out.println("开始传输" + data.get(index).getName());
try {
BufferedOutputStream ops = new BufferedOutputStream(
socket.getOutputStream());
byte[] buf = new byte[100];
int num = 0;
while ((num = fis.read(buf)) != -1) {
ops.write(buf, 0, num);
}
ServerThread.sockets.remove(socket);
System.out.println("关闭");
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
以上是关于安卓课程设计--利用socket在电脑和手机之间实现简单云盘的主要内容,如果未能解决你的问题,请参考以下文章
百度云课程 安卓手机Autojs自动化脚本开发教程 网课资料大全