用Java编写创建一对客户端/服务器程序,利用数据报将一个文件从一台主机传送到另一
展开全部
下面是我自己写的一个读取并显示txt文件的demo,希望对您有帮助。
public class Client {
public static void main(String[] args) {
ClientFrame f = new ClientFrame();
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ClientFrame extends JFrame implements ActionListener, ListSelectionListener{
private JList list = null;
private JButton sbtn = null;
private JButton cbtn = null;
private Vector v = null;
private JTextArea txt = null;
private Container control = null;
private Container btn = null;
private Socket client = null;
private DataInputStream reader = null;
private DataOutputStream writer = null;
public ClientFrame(){
this.list = new JList();
this.list.setBorder(BorderFactory.createTitledBorder("文件列表"));
this.list.addListSelectionListener(this);
this.sbtn = new JButton("显示");
this.sbtn.addActionListener(this);
this.cbtn = new JButton("清除");
this.cbtn.addActionListener(this);
this.control = new Container();
this.control.setPreferredSize(new Dimension(150, 400));
this.control.setLayout(new BorderLayout());
this.control.add(new JScrollPane(this.list),BorderLayout.CENTER);
this.btn = new Container();
this.btn.setLayout(new GridLayout(1,2));
btn.add(sbtn);
btn.add(cbtn);
this.control.add(this.btn,BorderLayout.SOUTH);
this.txt = new JTextArea();
this.txt.setEditable(false);
this.txt.setSize(350, 400);
this.setTitle("客户端");
this.setSize(500, 400);
this.setVisible(true);
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((displaySize.width - this.getWidth()) / 2, (displaySize.height - this.getHeight()) / 2);
this.setLayout(new BorderLayout());
this.add(this.control,BorderLayout.WEST);
this.add(new JScrollPane(this.txt),BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
//this.client = new Socket("192.168.32.34",6666);
this.client = new Socket("192.168.1.100",6666);
this.reader = new DataInputStream(client.getInputStream());
this.writer = new DataOutputStream(client.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == sbtn){
if(v == null){
v = new Vector();
}
else{
v.clear();
}
try {
writer.writeUTF("getfilelist");
writer.flush();
String t = reader.readUTF();
while( t != null && !t.equals("")){
v.add(t);
t = reader.readUTF();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.list.setListData(v);
}
if(event.getSource() == cbtn){
this.txt.setText("");
}
}
public void valueChanged(ListSelectionEvent e) {
int i = this.list.getSelectedIndex();
if (! this.list.getValueIsAdjusting() && i != -1) {
try {
writer.writeUTF("getfilecontent_" + i);
writer.flush();
String tmp = reader.readUTF();
this.txt.setText(tmp);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
public class Server {
static ArrayList<File> fileArray = new ArrayList<File>();
public static void main(String args[]) {
ServerSocket server = null;
Socket client = null;
String cmd = "";
try {
server = new ServerSocket(6666);
client = server.accept();
DataInputStream reader = new DataInputStream(client.getInputStream());
DataOutputStream writer = new DataOutputStream(client.getOutputStream());
while(true){
cmd = reader.readUTF();
System.out.println(cmd);
if(cmd.equals("getfilelist")){
fileArray.clear();
//fileArray = getFile(new File("D:/tmp"));
fileArray = getFile(new File("D:/学习/教程/学习笔记"));
String fn = "";
for(int k = 0; k < fileArray.size(); k ++){
fn = fileArray.get(k).getName();
writer.writeUTF(fn);
writer.flush();
}
writer.writeUTF("");
}
if(cmd.startsWith("getfilecontent_")){
int i = Integer.parseInt(cmd.split("_")[1]);
File f = fileArray.get(i);
Reader in = new InputStreamReader(new FileInputStream(f));
int tempbyte;
String str = "";
while ((tempbyte = in.read()) != -1) {
str += (char)tempbyte;
//System.out.println(str);
}
in.close();
writer.writeUTF(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static ArrayList<File> getFile(File f) {
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
getFile(child);
} else {
fileArray.add(child);
}
}
return fileArray;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询