<span style="font-size:24px;">客户端上传文件</span>
/**
*
* @author lijp8
* @input intent 待传输文件的地址
* @function 将文件传输到服务器,并返回传输状态
*/
class MyThread extends Thread{
private Intent intentThread;
private Socket socket = null;
private ArrayList<AttendencePerson> personArray =new ArrayList<AttendencePerson>();
//选择要传输的文件
//private String filePath= null;
File fi= new File(DateBaseActivity.dbFilePath);
public MyThread(Intent intent){
this.intentThread= intent;
}
@Override
public void run(){
//定义消息
Message msg = new Message();
msg.what = 0x11;
Bundle bundle = new Bundle();
bundle.clear();
try {
//连接服务器 并设置连接超时为5秒
socket = new Socket();
socket.connect(new InetSocketAddress(DateBaseActivity.IP, 8888), 5000);
//获取输出流 , 向服务器发送信息
DataOutputStream out=new DataOutputStream(socket.getOutputStream()); //向服务器发送消息
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(DateBaseActivity.dbFilePath)));
out.writeUTF(fi.getName());
out.flush();
out.writeLong((long)fi.length());
out.flush();
byte[] buf= new byte[(int)fi.length()];
while(true){
int read =0;
if(fis!=null)
{
read=fis.read(buf);
}else{
System.out.println("文件传输错误");
break;
}
if(read ==-1){
break;
}
out.write(buf, 0, read);
}
out.flush();
System.out.println("文件传输完成");
//获取输入流 , 接受服务器的信息
DataInputStream in = new DataInputStream(socket.getInputStream()); // 接收来自服务器的消息
String readMsg = in.readUTF();
bundle.putString("msg", readMsg);
msg.setData(bundle);
//发送消息 修改UI线程中的组件
myHandler.sendMessage(msg);
//关闭各种输入输出流
in.close();
out.close();
socket.close();
} catch (SocketTimeoutException aa) {
//连接超时 在UI界面显示消息
bundle.putString("msg", "服务器连接失败!请检查网络是否打开");
msg.setData(bundle);
//发送消息 修改UI线程中的组件
myHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
Log.w("ljp", e.getClass().getName() + ": " + e.getMessage());
//连接超时 在UI界面显示消息
bundle.putString("msg", e.getClass().getName() + ": " + e.getMessage());
msg.setData(bundle);
//发送消息 修改UI线程中的组件
myHandler.sendMessage(msg);
}
}
}
}
<span style="font-size:18px;">服务端下载程序</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;"></span><pre name="code" class="java"> class ServerDatabaseThread extends Thread{
public void run() {
try {
//当前时间
Date day=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
ServerSocket ss=new ServerSocket(8888); 创建一个ServerSocket对象,并让这个ServerSocket在8888端口监听
int i=1;
while(true){
Socket socket=ss.accept(); //调用ServerSocket的accept()方法,接受客户端所发送的请求,如果客户端没有发送数据,那么该线程就停滞不继续
try {
//ObjectInputStream in=new ObjectInputStream(socket.getInputStream());//接收客户端信息
DataInputStream in=new DataInputStream(socket.getInputStream());//接收客户端信息
DataOutputStream out=new DataOutputStream(socket.getOutputStream()); //向客户端发送消息
String savePath= "f:/test/"+df.format(day);
long fileLen=0;
int passedLen =0; // 传输进度
savePath += in.readUTF(); //读取数据库文件名
fileLen = in.readLong(); //文件长度
System.out.println("数据库文件大小:"+fileLen);
byte[] buf= new byte[(int)fileLen];
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
while(true){
int read =0;
if(passedLen>=fileLen)
break;
if(in!=null)
{
read=in.read(buf);
}else{
System.out.println("文件传输错误");
//out.writeUTF("文件传输错误");
//out.flush();
break;
}
passedLen+=read;
if(read ==-1){
break;
}
System.out.println( "文件传输进度为"+(passedLen*100/fileLen)+ "%");
fileOut.write(buf, 0, read);
}
System.out.println("文件传输完成");
passedLen=0;
fileOut.close();
out.writeUTF("数据库上传完成");
out.flush();
in.close(); //关闭流
out.close();//关闭流
socket.close();//关闭打开的socket
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}