java tcp網路通訊 傳輸檔案
初學java 實現tcp網路通訊 介面使用windowBuilder,介面如下:
首先服務端監聽埠號8005,啟動服務後,等待客戶端連線。
客戶端首先選擇需要傳輸的檔案,然後啟動連線,就開始傳輸檔案了。
服務端的原始碼如下:
package transfServer;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.*;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FileTransfServer extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField ipAddress;
private JTextField Port;
private Socket socket; //accept 後sock
private String filePath="E:\\testJava";
private boolean bStartFlag=false;
//IP地址
private String strIp="";
private int nPort=0;
public void ReadData(Socket sock) {
try{
InputStream input = sock.getInputStream(); //開啟 inputStream資源
byte[] fileNameByte = new byte[1024]; //存放fileName
int fileNameLenth; //讀取的字元長度
fileNameLenth = input.read(fileNameByte);
String fileName = new String(fileNameByte,0,fileNameLenth); //還原fileName
System.out.println(Thread.currentThread().getName() + fileName);
//下面是接受檔案
BufferedOutputStream output =
new BufferedOutputStream(new FileOutputStream(new File(filePath, fileName)));
byte[] buff = new byte[1024*1024*5];
int len;
while ( (len = input.read(buff))!=-1){
output.write(buff, 0, len);
}
System.out.println("receive finished!");
output.close();
sock.close();
}catch(Exception e){
System.out.println(e);
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
FileTransfServer dialog = new FileTransfServer();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public FileTransfServer() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel lblIpAddress = new JLabel("Ip Address:");
lblIpAddress.setBounds(14, 23, 91, 18);
contentPanel.add(lblIpAddress);
{
JLabel lblPort = new JLabel("Port:");
lblPort.setBounds(14, 61, 91, 18);
contentPanel.add(lblPort);
}
//ip地址
ipAddress = new JTextField("127.0.0.1");
ipAddress.setBounds(119, 20, 86, 24);
contentPanel.add(ipAddress);
ipAddress.setColumns(10);
//埠號
Port = new JTextField("8005");
Port.setColumns(10);
Port.setBounds(119, 58, 86, 24);
contentPanel.add(Port);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton btnStartServer = new JButton("啟動服務");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//啟動服務新增的程式碼
//獲取ip地址和埠號
strIp=ipAddress.getText(); //伺服器IP,此處沒有用到
String strPort=Port.getText();
//String to int
nPort=Integer.parseInt(strPort); //伺服器埠號
//啟動服務函式
try{
ServerSocket server = new ServerSocket(nPort);
bStartFlag=true;
while(bStartFlag)
{
Socket socket = server.accept();
ReadData(socket);
server.close();Thread.sleep(100);
}
}catch(Exception e)
{
System.out.println(e);
}
}
});
btnStartServer.setActionCommand("StartServer");
buttonPane.add(btnStartServer);
getRootPane().setDefaultButton(btnStartServer);
}
{
JButton cancelButton = new JButton("停止服務");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//新增取消命令的程式碼
bStartFlag=false;
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
package transfClient;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.awt.event.ActionEvent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.net.*;
public class FileTransfClient extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField IpAddress;
private JTextField Port;
private JTextField FileDir;
private String filePath="";
private String strIp="";
private int nPort=0;
//傳送檔案程式碼
public void SendFile(String strIp,int nPort)
{
//
try
{
//不處理異常編譯不過
InetAddress address=InetAddress.getByName(strIp);
Socket servSock=new Socket(address,nPort);
File file = new File (filePath);
String fileName = file.getName();
OutputStream out = servSock.getOutputStream();
//傳輸檔名
out.write(fileName.getBytes());
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
byte[] buff = new byte[1024*1024];
int len;
while (( len=input.read(buff) )!=-1){
out.write(buff,0,len);
}
servSock.close();
}catch(Exception e)
{
System.out.println(e);
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
FileTransfClient dialog = new FileTransfClient();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setTitle("檔案傳輸客戶端");
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public FileTransfClient() {
setBounds(100, 100, 708, 362);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblNewLabel = new JLabel("Ip Address:");
lblNewLabel.setBounds(14, 27, 91, 18);
contentPanel.add(lblNewLabel);
}
{
JLabel lblPort = new JLabel("Port:");
lblPort.setBounds(14, 69, 91, 18);
contentPanel.add(lblPort);
}
{
//IP 地址
IpAddress = new JTextField("127.0.0.1");
IpAddress.setBounds(119, 24, 86, 24);
contentPanel.add(IpAddress);
IpAddress.setColumns(10);
}
{
//埠號
Port = new JTextField("8005");
Port.setColumns(10);
Port.setBounds(119, 66, 86, 24);
contentPanel.add(Port);
}
{
JLabel label = new JLabel("檔案路徑:");
label.setBounds(14, 112, 91, 18);
contentPanel.add(label);
}
{
FileDir = new JTextField("");
FileDir.setColumns(10);
FileDir.setBounds(119, 109, 373, 24);
contentPanel.add(FileDir);
}
JButton browseBtn = new JButton("瀏覽");
browseBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//瀏覽Btn訊息相應函式
JFileChooser jf=new JFileChooser();
jf.setDialogTitle("請選擇需要傳送的檔案");
FileNameExtensionFilter filter=new FileNameExtensionFilter("文字檔案(*.txt;*.kcd)","txt","kcd");
jf.setFileFilter(filter);
int returnVal=jf.showOpenDialog(null);
if(returnVal ==JFileChooser.APPROVE_OPTION)
{
filePath=jf.getSelectedFile().getPath();
FileDir.setText(filePath);
}
}
});
browseBtn.setActionCommand("Browse");
browseBtn.setBounds(506, 108, 63, 27);
contentPanel.add(browseBtn);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton StartConnectBtn = new JButton("啟動連線");
StartConnectBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//連線伺服器所需程式碼
//獲取ip地址和埠號
strIp=IpAddress.getText(); //伺服器IP
String strPort=Port.getText();
//String to int
nPort=Integer.parseInt(strPort); //伺服器埠號
//傳輸檔案
SendFile(strIp,nPort);
}
});
StartConnectBtn.setActionCommand("StartConnect");
buttonPane.add(StartConnectBtn);
getRootPane().setDefaultButton(StartConnectBtn);
}
{
JButton SendBtn = new JButton("傳送");
SendBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//往伺服器傳送檔案所需程式碼
}
});
SendBtn.setActionCommand("Send");
buttonPane.add(SendBtn);
}
}
}
}
相關文章
- QT從入門到入土(九)——TCP/IP網路通訊(以及檔案傳輸)QTTCP
- TCP/IP 通訊傳輸流TCP
- Python 基於 TCP 傳輸協議的網路通訊實現PythonTCP協議
- 網路通訊3:TCP互動通訊TCP
- 網路通訊2:TCP簡單通訊TCP
- 網路通訊2:TCP通訊實現TCP
- 網際網路大檔案的傳輸方式
- 網路通訊3:HTTP實現文字傳輸HTTP
- 網路通訊4:TCP廣播TCP
- 計算機網路之TCP可靠傳輸計算機網路TCP
- 資料檔案在網路“裸奔”,如何在網際網路中進行檔案傳輸?
- Android程式設計師必知必會的網路通訊傳輸層協議——UDP和TCPAndroid程式設計師協議UDPTCP
- Qt - TCP網路傳輸如何傳送結構體型別QTTCP結構體型別
- 網路通訊4:HTTP實現二進位制傳輸HTTP
- 網路學習筆記(二):TCP可靠傳輸原理筆記TCP
- win10系統區域網傳輸檔案操作方法 win10怎麼通過區域網傳輸檔案Win10
- 網際網路使用者之間如何傳輸大檔案
- Java傳輸檔案使用Base64優化傳輸速率。Java優化
- C++ - tcp網路傳輸如何傳送結構體型別C++TCP結構體型別
- Python 網路資料傳輸協議 TCP 程式設計Python協議TCP程式設計
- Java進階:基於TCP通訊的網路實時聊天室JavaTCP
- 針對IT網際網路行業的檔案傳輸解決方案行業
- 網路通訊協議-TCP協議詳解!協議TCP
- 什麼是極速檔案傳輸,極速檔案傳輸如何進行大檔案傳輸
- liunx通過TCP傳送資訊TCP
- 快速傳輸大檔案,怎麼透過網路傳大檔案給對方(1G以上)
- Linux 檔案傳輸Linux
- sftp 傳輸檔案FTP
- scp 傳輸檔案
- 計算機網路之八:TCP協議(2) TCP可靠傳輸的實現計算機網路TCP協議
- Java實現TCP通訊程式JavaTCP
- 大檔案如何傳輸,大檔案的傳輸方式有哪些?
- 資料通訊與網路 第五版第24章 傳輸層協議-TCP協議部分要點協議TCP
- RTN實時音視訊傳輸網路
- 【多檔案自平衡雲傳輸】使用展示 —— 檔案傳輸系統
- 檔案傳輸協議的五種安全檔案傳輸替代方案協議
- 網路犯罪分子瞄準關鍵的IBM檔案傳輸漏洞IBM
- Java:基於TCP協議網路socket程式設計(實現C/S通訊)JavaTCP協議程式設計
- java多執行緒實現TCP網路Socket程式設計(C/S通訊)Java執行緒TCP程式設計