80,354
社区成员
发帖
与我相关
我的任务
分享
public class MainActivity extends Activity{
private Button button1 = null;
private Button button2 = null;
private Button button3 = null;
private EditText edit1 = null;
private EditText edit2 = null;
private static EditText edit3 = null;
private int ZT=0;
public Socket socket=null;
private String msg=null;
public PrintWriter out=null;
public BufferedReader br=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
edit1=(EditText)findViewById(R.id.edit1);
edit2=(EditText)findViewById(R.id.edit2);
edit3=(EditText)findViewById(R.id.edit3);
button1.setOnClickListener(new listener());
button2.setOnClickListener(new listener());
button3.setOnClickListener(new listener());
}
class listener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
//按钮1按下,启动通讯线程。
if(v.getId()==R.id.button1){
if(ZT==0){
ZT=1;
try
{
_thread.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//按钮2按下,发送信息。
else if(v.getId()==R.id.button2){
msg=edit3.getText().toString();
edit3.setText(null);
edit3.setText(null);
try
{
out.println(msg);
out.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//按钮3按下,关闭程序
else if(v.getId()==R.id.button3){
finish();
}
}
}
private Thread _thread = new Thread(){
public void run(){
try {
//获取控件里填写的IP地址和端口号,连接服务器
socket = new Socket(edit1.getText().toString(),Integer.parseInt(edit2.getText().toString()));
//获得输入输出流
out =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
String in=null;
Message message=null;
Bundle bundle=null;
message = new Message();
bundle = new Bundle();
while(true){
try {
in=br.readLine();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//判断是否有信息到达,如果有,取得信息,传递参数。
if(in!=null){
bundle.putString("mesg",in);
message.setData(bundle);
in=null;
handler.sendMessage(message);
}
}
}
};
public Handler handler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
try{
//更新控件
MainActivity.edit3.setText(msg.getData().getString("mesg"));
}
catch (Exception e)
{
}
}
};
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MyServer {
static int portNum=1089;
static String path;
public static void main(String args[]) throws IOException{
final ServerSocket ReceiverSocket = new ServerSocket(portNum);
System.out.println("Setting the local file location...");//设置本地文件夹路径
Scanner scan = new Scanner(System.in);
path = scan.next();
while(true)
{
System.out.println("Ready to accept....");
Socket socket =ReceiverSocket.accept();
Thread th =new Thread(new ClientThread(socket,path));
th.run();
}
}
}
class ClientThread implements Runnable
{
private Socket socket ;
private String path;
ClientThread(Socket socket,String path)
{
this.socket =socket;
this.path = path;
}
public void run() {
try {
System.out.println("A client connected...");
sendFileName(socket);//发送本地文件夹名中的所有文件夹名
InputStream is =socket.getInputStream();
byte buf []= new byte [1024];
is.read(buf,0,1024);
String tpath = new String(buf).trim();
String path =this.path +"\\"+tpath;
File file =new File(path);
System.out.println(path);
if(!file.exists())
{
OutputStream outStream = socket.getOutputStream();
PrintWriter output = new PrintWriter(new OutputStreamWriter(outStream));
output.print("File doesn't exist!");
}
else {
OutputStream outStream = socket.getOutputStream();
PrintWriter output = new PrintWriter(new OutputStreamWriter(outStream));
output.print("Transport is ready!"); }
sendFile(file,socket);
}catch (IOException e) {
e.printStackTrace();
}
}
private static void sendFile(File file ,Socket socket) throws IOException
{
byte []buf = new byte[1024];
FileInputStream in = new FileInputStream(file);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
System.out.println("Sending...");
int i =1;
while(in.read(buf,0,1024)>0)
{
dos.write(buf);
System.out.println("已发送 "+ (i++)+"kb");
dos.flush();
}
socket.close();
dos.close();
}
private void sendFileName(Socket socket) throws IOException
{
File f = new File(path);
String nameFile[] = f.list();
OutputStream outStream = socket.getOutputStream();
for(int i = 0;i<nameFile.length;i++)
{
outStream.write(nameFile[i].getBytes());
outStream.flush();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
outStream.flush();
outStream.write("EOF".getBytes());
}
}
Client
import java.io.*;
import java.net.*;
public class MyClient {
static int PortNum=1089;
static String hostName;
public static void main(String args[]) throws IOException
{
System.out.println("Input the host name...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
hostName = br.readLine();
InetAddress receiverHost = InetAddress.getByName(hostName);
Socket socket = new Socket();
socket.connect(new InetSocketAddress(receiverHost, 1089));
receieveFileName(socket) ;//接收文件名
System.out.println("Input the file name...");
String dir = br.readLine();
System.out.println("Input the local file directory...");
String local_dir = br.readLine().trim();
OutputStream output= socket.getOutputStream();
output.write(dir.getBytes());
InputStream ip = socket.getInputStream();
byte [] buf = new byte[1024];
ip.read(buf);
String reply=new String(buf);
if(reply.equals("File doesn't exist!"))
{
System.out.println(reply);
}
else {
File f = new File(local_dir);
if(!f.exists())
{
f.mkdir();
}
DataInputStream dis = new DataInputStream(socket.getInputStream());
// DataOutputStream fos = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(local_dir+"\\"+dir))));
FileOutputStream fos = new FileOutputStream(new File(local_dir+"\\"+dir));
while(dis.read(buf, 0, 1024)>0)
{
buf = new byte[1024];
fos.write(buf, 0, 1024);
fos.flush();
}
fos.close();
}
socket.close();
}
private static void receieveFileName(Socket socket) throws IOException
{
InputStream is =socket.getInputStream();
String msg ;
do{
byte buf [] = new byte [1024];
is.read(buf,0,1024);
msg = new String(buf).trim();
System.out.println(msg);
}
while(!msg.equals("EOF"));
}
}
你参考一下具体的操作是这样的
1.运行服务程序,设置服务程序的本地文件夹路径
2.运行客户端,设置服务器IP一般在同一PC上运行的话都是localhost
3.设置客户端文件下载地址
4.获取到服务器文件名列表
5.选择一个文件下载
private Thread _thread = new Thread(){
public void run(){
InputStream in=null;
String msg=null;
Message message=null;
message=new Message();
Bundle bundle=null;
bundle=new Bundle();
byte[] buf=new byte[1024];
try {
//获取控件里填写的IP地址和端口号,连接服务器
socket = new Socket(edit1.getText().toString(),Integer.parseInt(edit2.getText().toString()));
//获得输入输出流
out =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
//br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
in=socket.getInputStream();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
while(true){
try {
//读出输入流中的信息
in.read(buf,0,1024);
//将读出的信息转化为字符串
msg = new String(buf).trim();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//判断字符串是否为空
if(msg!=null){
//发送消息更新控件
bundle.putString("mesg",msg);
message.setData(bundle);
handler.sendMessage(message);
msg=null;
buf=null;
}
}
}
};