java文件网络传输.docx
《java文件网络传输.docx》由会员分享,可在线阅读,更多相关《java文件网络传输.docx(17页珍藏版)》请在冰豆网上搜索。
![java文件网络传输.docx](https://file1.bdocx.com/fileroot1/2022-12/12/c6e26016-0414-4e95-ab68-43da27af89b1/c6e26016-0414-4e95-ab68-43da27af89b11.gif)
java文件网络传输
软件界面:
软件初始界面
由用户自行输入要链接的ip地址,这里我测试的是自己链接自己的ip地址
用户选择自己要发送的文件
显示对方要向我发送文件,提示用户接收文件
用户选择接收文件存放位置
接收完成,自动断开链接
软件简要:
源码分为1个包(包名为:
文件传输),4个java文件(zy.java、ddjs.java、send.java、js.java),zy.java为主界面,ddjs.java用于接收请求连接,send.java用于发送文件,zy.java用于接收文件。
zy.java:
package文件传输;
importjava.awt.Frame;
importjava.awt.Rectangle;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
import.ServerSocket;
import.Socket;
import.UnknownHostException;
importjavax.swing.DefaultListModel;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JLabel;
importjavax.swing.JList;
importjavax.swing.JOptionPane;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclasszyextendsFrameimplementsActionListener{
publicstaticJListyonghu=null;
publicstaticDefaultListModelmx;
privateJButtonlianjie,send,rece;
publicstaticJLabelnow,zdy;
publicstaticintend=0;
publicSocketa=null;
publicStringh;
publicstaticSocketclient=null;
publicstaticFilefile;
publicstaticintlength=0;
publicstaticbyte[]sendByte=null;
publicstaticSocketsocket=null;
publicstaticDataOutputStreamdout=null;
publicstaticFileInputStreamfin=null;
publicstaticServerSocketserver=null;
publicstaticintp=0;
publicstaticddjsas=newddjs();
publicJTextFieldip;
publiczy()throwsIOException
{
super("文件传输1.0By:
若离随风");
try{
server=newServerSocket(12394);
}catch(IOExceptione1){
System.out.println("端口建立失败");
JOptionPane.showMessageDialog(null,"开放连接失败,端口可能被其他软件占用","错误提示",JOptionPane.ERROR_MESSAGE);
}
as.start();
ImageIconimg=newImageIcon("img\\028.png");
ImageIconrec=newImageIcon("img\\029.png");
ip=newJTextField();
setNow(newJLabel("当前暂无连接"));
zdy=newJLabel("请输入对方ip:
");
lianjie=newJButton("建立连接");
send=newJButton(img);
rece=newJButton(rec);
mx=newDefaultListModel();
yonghu=newJList(mx);
yonghu.setEnabled(false);
yonghu.setVisibleRowCount(20);
JScrollPanejsp=newJScrollPane(yonghu);
lianjie.setBounds(newRectangle(630,38,120,25));
jsp.setBounds(newRectangle(20,70,730,400));
getNow().setBounds(newRectangle(20,30,400,40));
send.setBounds(newRectangle(20,500,40,40));
rece.setBounds(newRectangle(80,500,40,40));
zdy.setBounds(newRectangle(390,30,400,40));
ip.setBounds(newRectangle(470,38,150,25));
this.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
dispose();
}
publicvoidwindowClosed(WindowEvente)
{
try{
server.close();
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
});
lianjie.addActionListener(this);
send.addActionListener(this);
rece.addActionListener(this);
this.add(jsp);
this.add(lianjie);
this.add(getNow());
this.add(send);
this.add(rece);
this.add(zdy);
this.add(ip);
this.setLayout(null);
this.setSize(800,580);
this.setLocation(600,150);
this.setVisible(true);
}
publicvoidactionPerformed(ActionEventev)
{
if(ev.getSource()==lianjie)
{
h=ip.getText();
if(h.equals(""))
JOptionPane.showMessageDialog(null,"请输入ip地址","错误提示",JOptionPane.ERROR_MESSAGE);
else
{
try{
socket=newSocket(h,12394);
}catch(UnknownHostExceptione){
JOptionPane.showMessageDialog(null,"非法的ip地址","错误提示",JOptionPane.ERROR_MESSAGE);
}catch(IOExceptione){
JOptionPane.showMessageDialog(null,"链接失败,对方可能未开启软件","错误提示",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
if(socket.isClosed()==false&&socket.isConnected()==true)
{
now.setText("当前连接ip:
"+String.valueOf(yonghu.getSelectedValue()));
}
}
}
if(ev.getSource()==send)
{
file=null;
JFileChooserjfc=newJFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.showDialog(newJLabel(),"选择");
file=jfc.getSelectedFile();
newsend(client,file).start();
}
if(ev.getSource()==rece)
{
Filerec=null;
JFileChooserjfc=newJFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(newJLabel(),"选择");
rec=jfc.getSelectedFile();
newjs(socket,rec).start();
}
}
publicstaticvoidmain(Stringargs[])throwsIOException
{
newzy();
}
publicstaticvoidjiance()
{
DataInputStreamin=null;
try{
in=newDataInputStream(zy.socket.getInputStream());
}catch(IOExceptione1){
}
try{
if(in.readUTF().equals("send"))
mx.addElement("对方请求向你发送文件,若接收请点击接收按钮");
}catch(IOExceptione){
}
}
staticJLabelgetNow(){
returnnow;
}
publicstaticvoidsetNow(JLabelnow){
zy.now=now;
}
}
ddjs.java:
package文件传输;
importjava.io.IOException;
import.ServerSocket;
import.Socket;
importjavax.swing.JOptionPane;
publicclassddjsextendsThread{
publicvoidrun()
{
while(true)
{
try{
zy.client=zy.server.accept();
}catch(IOExceptione){
}
if(zy.client.isClosed()==false)
{
JOptionPane.showMessageDialog(null,"已经与"+zy.client.getInetAddress()+"建立连接");
zy.now.setText("当前连接ip:
"+zy.client.getInetAddress());
zy.jiance();
}
}
}
publicddjs()
{
}
publicstaticvoidmain(Stringag[])
{
}
}
send.java
package文件传输;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
import.ServerSocket;
import.Socket;
importjavax.swing.JOptionPane;
publicclasssendextendsThread{
privateFilefile;
publicvoidrun()
{
intlength=0;
inta=0;
byte[]sendByte=null;
Socketsocket=null;
DataOutputStreamdout=null;
DataInputStreamin=null;
FileInputStreamfin=null;
try{
dout=newDataOutputStream(zy.client.getOutputStream());
}catch(IOExceptione2){
}
try{
fin=newFileInputStream(file);
}catch(FileNotFoundExceptione2){
}
try{
in=newDataInputStream(zy.client.getInputStream());
}catch(IOExceptione1){
}
try{
dout.writeUTF("send");
}catch(IOExceptione2){
}
Strings=null;
while(a==0)
{
try{
s=in.readUTF();
}catch(IOExceptione1){
}
System.out.println(s);
if(s.equals("OK")==true)
{
a=1;
}
}
try{
try{
sendByte=newbyte[1024];
dout.writeUTF(file.getName());
while((length=fin.read(sendByte,0,sendByte.length))>0){
dout.write(sendByte,0,length);
dout.flush();
}
}catch(Exceptione){}
finally{
if(dout!
=null)
dout.close();
if(fin!
=null)
fin.close();
if(socket!
=null)
zy.client.close();
//zy.server.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
publicsend(Socketa,Filefile)
{
this.file=file;
}
publicstaticvoidmain(Strings[])
{
}
}
js.java:
package文件传输;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
import.ServerSocket;
import.Socket;
import.UnknownHostException;
importjavax.swing.JOptionPane;
publicclassjsextendsThread{
privateSocketsocket;
privateFilefile;
publicvoidrun()
{
byte[]inputByte=null;
intlength=0;
DataInputStreamdin=null;
DataOutputStreamo=null;
FileOutputStreamfout=null;
try{
o=newDataOutputStream(zy.socket.getOutputStream());
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
try{
o.writeUTF("OK");
}catch(IOExceptione1){
}
try{
din=newDataInputStream(zy.socket.getInputStream());
fout=newFileOutputStream(newFile(file.getCanonicalPath()+din.readUTF()));
inputByte=newbyte[1024];
System.out.println("开始接收数据...");
while(true){
if(din!
=null){
length=din.read(inputByte,0,inputByte.length);
}
if(length==-1){
break;
}
System.out.println(length);
fout.write(inputByte,0,length);
fout.flush();
}
System.out.println("完成接收");
JOptionPane.showMessageDialog(null,"文件接收完成,链接已断开");
zy.now.setText("当前暂无链接");
}catch(Exceptionex){
ex.printStackTrace();
}
finally{
if(fout!
=null)
try{
fout.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
if(din!
=null)
try{
din.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
if(zy.socket!
=null)
try{
zy.socket.close();
//zy.server.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
publicjs(Socketa,Filefile)
{
this.socket=a;
this.file=file;
}
publicstaticvoidmain(Strings[])
{
}
}
416353539