java文件网络传输Word下载.docx

上传人:b****5 文档编号:18076473 上传时间:2022-12-13 格式:DOCX 页数:17 大小:145.03KB
下载 相关 举报
java文件网络传输Word下载.docx_第1页
第1页 / 共17页
java文件网络传输Word下载.docx_第2页
第2页 / 共17页
java文件网络传输Word下载.docx_第3页
第3页 / 共17页
java文件网络传输Word下载.docx_第4页
第4页 / 共17页
java文件网络传输Word下载.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

java文件网络传输Word下载.docx

《java文件网络传输Word下载.docx》由会员分享,可在线阅读,更多相关《java文件网络传输Word下载.docx(17页珍藏版)》请在冰豆网上搜索。

java文件网络传输Word下载.docx

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地址"

else

{

try{

socket=newSocket(h,12394);

}catch(UnknownHostExceptione){

JOptionPane.showMessageDialog(null,"

非法的ip地址"

}catch(IOExceptione){

链接失败,对方可能未开启软件"

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;

jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

rec=jfc.getSelectedFile();

newjs(socket,rec).start();

publicstaticvoidmain(Stringargs[])throwsIOException

newzy();

publicstaticvoidjiance()

DataInputStreamin=null;

in=newDataInputStream(zy.socket.getInputStream());

if(in.readUTF().equals("

send"

mx.addElement("

对方请求向你发送文件,若接收请点击接收按钮"

}catch(IOExceptione){

staticJLabelgetNow(){

returnnow;

publicstaticvoidsetNow(JLabelnow){

zy.now=now;

}

ddjs.java:

publicclassddjsextendsThread{

publicvoidrun()

while(true)

zy.client=zy.server.accept();

}catch(IOExceptione){

if(zy.client.isClosed()==false)

已经与"

+zy.client.getInetAddress()+"

zy.now.setText("

+zy.client.getInetAddress());

zy.jiance();

publicddjs()

publicstaticvoidmain(Stringag[])

send.java

importjava.io.FileNotFoundException;

publicclasssendextendsThread{

privateFilefile;

intlength=0;

inta=0;

byte[]sendByte=null;

Socketsocket=null;

DataOutputStreamdout=null;

DataInputStreamin=null;

FileInputStreamfin=null;

try{

dout=newDataOutputStream(zy.client.getOutputStream());

}catch(IOExceptione2){

fin=newFileInputStream(file);

}catch(FileNotFoundExceptione2){

in=newDataInputStream(zy.client.getInputStream());

dout.writeUTF("

Strings=null;

while(a==0)

s=in.readUTF();

System.out.println(s);

if(s.equals("

OK"

)==true)

a=1;

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!

fin.close();

if(socket!

zy.client.close();

//zy.server.close();

}catch(Exceptione){

e.printStackTrace();

publicsend(Socketa,Filefile)

this.file=file;

publicstaticvoidmain(Strings[])

js.java:

importjava.io.FileOutputStream;

publicclassjsextendsThread{

privateSocketsocket;

byte[]inputByte=null;

intlength=0;

DataInputStreamdin=null;

DataOutputStreamo=null;

FileOutputStreamfout=null;

o=newDataOutputStream(zy.socket.getOutputStream());

//TODOAuto-generatedcatchblock

e1.printStackTrace();

o.writeUTF("

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();

完成接收"

JOptionPane.showMessageDialog(null,"

文件接收完成,链接已断开"

zy.now.setText("

当前暂无链接"

}catch(Exceptionex){

ex.printStackTrace();

finally{

if(fout!

fout.close();

//TODOAuto-generatedcatchblock

din.close();

if(zy.socket!

zy.socket.close();

//zy.server.close();

publicjs(Socketa,Filefile)

this.socket=a;

{

416353539

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1