java文件传输帮忙编写一个文件传输的JAVA程序

java文件传输  时间:2021-06-11  阅读:()

基于Java Socket实现文件传输

import java.io.*; import .*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Client1 extends JFrame { private JTextField enter,Iptext; private JTextArea display; private JLabel Iplabel; private JPanel upPanel; JButton clientMessageButton,connectButton,DisConnectButton; ObjectOutputStream output; ObjectInputStream input; Socket socket1; String message=" "; public Client1() { super("客户端"); Container c=getContentPane(); clientMessageButton=new JButton("发送"); c.add(clientMessageButton,BorderLayout.EAST); clientMessageButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {sendData(enter.getText()); }}); DisConnectButton=new JButton("断开连接"); DisConnectButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {DisConnect(); }}); Iptext=new JTextField(10); Iptext.setText(""); Iptext.setEnabled(true); Iptext.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {connect(); }}); Iplabel=new JLabel("服务器IP:"); connectButton=new JButton("连接"); connectButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {connect(); }}); upPanel=new JPanel(); upPanel.add(connectButton); upPanel.add(DisConnectButton); upPanel.add(Iplabel); upPanel.add(Iptext); enter=new JTextField(); enter.setEnabled(true); enter.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {sendData(enter.getText()); }}); c.add(enter,BorderLayout.SOUTH); c.add(upPanel,BorderLayout.NORTH); display=new JTextArea(); c.add(new JScrollPane(display),BorderLayout.CENTER); setSize(300,450); show(); } public void connect() { try { display.setText("准备连接... "); String s=Iptext.getText(); socket1=new Socket(Address.getByName(s),8888); display.append("连接到: "+Address().getHostName()); display.append(" 主机IP为:"+Address().toString()); output=new ObjectOutputStream(socket1.getOutputStream()); output.flush(); do{ try{ message=(String)input.readObject(); display.append(" "+message); } catch(IOException e) { display.append(" 无法获得信息"); } catch(ClassNotFoundException e) {} }while(!message.equals("end")); display.append(" 关闭连接"); output.close(); input.close(); socket1.close(); } catch(EOFException eof) { System.out.println(eof); } catch(IOException e) { e.printStackTrace(); } } public void DisConnect() { try { display.append(" 关闭连接"); output.close(); input.close(); socket1.close(); } catch(Exception e) {} } private void sendData(String s) { try { message=s; output.writeObject("客户消息: "+s); display.append(" 客户:"+s); output.flush(); enter.setText(" "); } catch(Exception e) { display.append(" 发生错误"); } } public static void main(String args[])//throws NullPointerException { Client1 app=new Client1(); app.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) {System.exit(0);}}); //app.connect(); } } //服务端 2007-12-30 17:33 import java.io.*; import .*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Server1 extends JFrame { private JTextArea display; private JTextField enter; JButton sysMessageButton; JButton MessageButton,startServerButton; JPanel upPanel; ServerSocket server; Socket socket1; ObjectOutputStream output; ObjectInputStream input; public Server1() { super("服务器端"); Container c=getContentPane(); enter=new JTextField(); setSize(50,20); upPanel=new JPanel(); sysMessageButton=new JButton("发送"); c.add(sysMessageButton,BorderLayout.EAST); setSize(50,20); MessageButton=new JButton("断开连接"); c.add(MessageButton,BorderLayout.WEST); MessageButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Service();}});

用java怎么传送文件

传文件和传字符的不同之处是将文件当做二进制字节流,并且需要服务器、客户端两端根据一定的规则持续发接数据,就像持续的一问一答那样的流程。

我之前写了一个文件传输的封装类,你看看里面的函数的内部实现就能了解文件传输的过程了。

地址是 /ftn_handler/37c7d84fc1c71f4d8240efbc54b465eb128c89654d501f368e3f57488f40e651292638fb0f06848e7430c35824781b2bd78dc9d65152db51d1b5a20d28951faa/file.rar

java 文件传输用什么流

FileInputStream fis = new FileInputStream("d://xxx"); FileOutputStream fos = new FileOutputStream(fis); fos.write; fos.close;

帮忙编写一个文件传输的JAVA程序

如果文件太大不能一次全读入!! JAVA里关于文件读写的有几十个类,不知道你想要如何实现, 以下是读文件的一个程序,如果有问题,发信息给我吧........ import java.io.*; import java.nio.*; import java.nio.channels.FileChannel; public class javaTest { public static void main(String[] args) { String file1=System.getProperty("user.dir")+"/1.txt";//文件,自己修改 FileInputStream myFile = null; try { myFile = new FileInputStream(file1); // } catch(FileNotFoundException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel myChannel = myFile.getChannel(); //这里定义缓冲区大小,每次读入字节数 ByteBuffer mybuf = ByteBuffer.allocate(1024); try { while(myChannel.read(mybuf) != -1) { byte[] mybytes = mybuf.array();//读入的文件转为字节数组 mybuf.clear(); /** * 在这里进行比较 * 可以通过字节对比 * 也可以把字节转成字符串再对比 * */ } myFile.close(); }catch(IOException e) { e.printStackTrace(System.err); System.exit(1); }

青云互联:美国洛杉矶CN2弹性云限时八折,15元/月起,可选Windows/可自定义配置

青云互联怎么样?青云互联是一家成立于2020年6月的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,目前提供有美国免费主机、香港主机、香港服务器、美国云服务器,让您的网站高速、稳定运行。美国cn2弹性云主机限时8折起,可选1-20个IP,仅15元/月起,附8折优惠码使用!点击进入:青云互联官方网站地址青云互联优惠码:八折优惠码:ltY8sHMh (续费同价)青云互联活动方案:美国洛杉矶...

御云(RoyalYun):香港CN2 GIA VPS仅7.9元每月起,美国vps仅8.9/月,续费同价,可叠加优惠

御云怎么样?炎炎暑期即将来临,御云(royalyun)香港、美国服务器开启大特惠模式。御云是新成立的云服务提供商,主要提供香港、美国的云服务器,不久将开启虚拟主机业务。我们的香港和美国主机采用CN2 GIA线路。目前,香港cn2 gia vps仅7.9元每月起,美国vps仅8.9/月,续费同价,可叠加优惠,香港云服务器国内延迟一般在50ms左右,是搭建网站的最佳选择,但是请不要用于违法用途。点击进...

DMIT:美国cn2 gia线路vps,高性能 AMD EPYC/不限流量(Premium Unmetered),$179.99/月起

DMIT怎么样?DMIT最近动作频繁,前几天刚刚上架了日本lite版VPS,正在酝酿上线日本高级网络VPS,又差不多在同一时间推出了美国cn2 gia线路不限流量的美国云服务器,不过价格太过昂贵。丐版只有30M带宽,月付179.99美元 !!目前,美国云服务器已经有个4个套餐,分别是,Premium(cn2 gia线路)、Lite(普通直连)、Premium Secure(带高防的cn2 gia线...

java文件传输为你推荐
在线识别图片来源! ! SOS ! !怎样可以求出电脑自定义图片的来源,在哪可以找到!?iso20000认证为什么ISO20000认证能够风靡全球?它对整个企业的发展有什么好处?人肉搜索引擎怎样使用人肉搜索引擎?js后退在全局js中屏蔽了后退功能,但是想让自己定义的后退有用assemblyinfo什么是GAC鄂n鄂N的车牌是那里的?jstz谁有101网校的账号?qq网络硬盘如何使用QQ网络硬盘腾讯技术腾讯是什么东西?assemblyinfoCSE和CS有什么区别
.net虚拟主机 awardspace cloudstack 外国空间 日志分析软件 警告本网站 ibrs 合肥鹏博士 最好的空间 警告本网站美国保护 anylink 最好的免费空间 徐正曦 国外代理服务器软件 中国网通测速 中国电信宽带测速网 电信主机 多线空间 新世界服务器 域名dns 更多