问题描述: 这是一个多线程接受用户传输文件的小程序,下午写好之后,测试正常,但是因为不是图形化界面,就想着晚上改改,服务端不改变,我只把客户端添加在一个简单的图形化界面上,但是本人目前还是个奋斗中的小白,在客户端运行之后,报出一长串的异常,我也看不懂,还请大神帮我瞅瞅,(问题就出在客户端的那个事件处理那块,但是我不知道怎么改!).
服务端代码:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server1 extends Thread {
Socket socket;
public Server1(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
// 获取客户端IP
String s = socket.getInetAddress().getHostAddress();
String name=null;
if(s.endsWith("35")){
name="Day21作业xx";
}else if(s.endsWith("22")){
name="Day21作业yy";
}else if(s.endsWith("30")){
name="Day21作业zz";
}else if(s.endsWith("45")){
name="Day21作业cc";
}else if(s.endsWith("104")){
name="Day21作业dd";
}
// 通过Socket接受
try {
BufferedInputStream br = new BufferedInputStream(socket.getInputStream());
// 读取输出流对象
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(
"C:\\Users\\xiawei\\Desktop\\day21\\"+name+".docx"));
int length=0;
byte[] buf=new byte[1024*100];
while ((length = br.read(buf)) != -1) {
bw.write(buf,0,length);
}
System.out.println(name.replaceAll("Day21作业","") + "同学的作业已经提交!");
bw.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9000);
// 不断接受用户请求链接
while (true) {
Socket socket = serverSocket.accept();
new Server1(socket).start();
}
}
}
客户端代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Client1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
final JFrame frame = new JFrame("作业提交器");
JPanel jp = new JPanel();
frame.add(jp);
final JTextField jt = new JTextField("请输入你要提交的文件路径", 30);
jp.add(jt);
JButton jb = new JButton("提交");
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String path = jt.getText();
File file = new File(path);
if (!file.exists()) {
// 警告对话框
JOptionPane.showMessageDialog(frame, "不合法文件路径!", "警告",
JOptionPane.WARNING_MESSAGE);
} else if (file.isDirectory()) {
JOptionPane.showMessageDialog(frame, "您输入的路径不是一个文件", "警告",
JOptionPane.WARNING_MESSAGE);
} else if(file.isFile()){
try {
mainMethod(file);
// 消息对话框
JOptionPane.showMessageDialog(frame, "提交成功", "通知",
JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
jp.add(jb);
FrameUtil.initFrame(frame, 600, 400);
// ;
}
public static void mainMethod(File file) throws IOException {
Socket socket = new Socket(InetAddress.getByName("192.168.27.104"),
9000);
// 获取socket输出流对象
BufferedOutputStream bos = new BufferedOutputStream(
socket.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));// 此处路径可以修改为自己索要上传的文件路径名
int line = 0;
byte[] buf = new byte[1024 * 100];
while ((line = bis.read(buf)) != -1) {
bos.write(buf, 0, line);
}
bis.close();
socket.close();
}
}
客户端界面初始化
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class FrameUtil {
// 初始化窗体
public static void initFrame(JFrame frame, int width, int height) {
// TODO Auto-generated method stub
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension d = toolkit.getScreenSize();
int x = (int) d.getWidth();
int y = (int) d.getHeight();
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);
frame.setVisible(true);// 设置为显示;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭
}
}
全部代码就这些了{:2_43:} |
|