📄 filesend.java
字号:
package com.client;
import java.awt.BorderLayout;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.io.*;
import java.net.*;
public class FileSend extends JFrame implements Runnable
{
private static JTextField filePath;
private static OutputStream out ;
private static FileInputStream fin;
private static Socket socket;
private static int port;
private static String IP;
public FileSend(String IP,int port)
{
super();
setIconImage(new ImageIcon(this.getClass().getResource("image/bird.jpg")).getImage());
this.port=port;
this.IP=IP;
setVisible(true);
setTitle("传送文件");
getContentPane().setLayout(null);
setBounds(100, 100, 290, 195);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
filePath = new JTextField();
filePath.setBounds(10, 46, 184, 20);
getContentPane().add(filePath);
final JButton fileSelect = new JButton();
fileSelect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
FileSelect file=new FileSelect("load");
filePath.setText(file.getFile());
}
});
fileSelect.setText("浏览");
fileSelect.setBounds(200, 44, 73, 23);
getContentPane().add(fileSelect);
final JButton send = new JButton();
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
try
{
socket=new Socket(FileSend.IP,FileSend.port);
File file=new File(filePath.getText());
fin=new FileInputStream(file);
out=socket.getOutputStream();
byte[] b=new byte[10000];
while(fin.read(b)!=-1)
{
out.write(b);
}
out.flush();
out.close();
dispose();
MessageDialog dialog=new MessageDialog("文件发送完毕!");
dialog.setSize(331, 163);
dialog.setVisible(true);
}catch(IOException e){}
}
});
send.setText("发送");
send.setBounds(39, 114, 73, 23);
getContentPane().add(send);
final JButton close = new JButton();
close.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
dispose();
}
});
close.setText("取消");
close.setBounds(164, 114, 73, 23);
getContentPane().add(close);
}
public void run()
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -