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

RackNerd 2022春节促销提供三款年付套餐 低至年付10.88美元

RackNerd 商家我们应该是比较熟悉的商家,速度一般,但是人家便宜且可选机房也是比较多的,较多集中在美国机房。包括前面的新年元旦促销的时候有提供年付10美元左右的方案,实际上RackNerd商家的营销策略也是如此,每逢节日都有活动,配置简单变化,价格基本差不多,所以我们网友看到没有必要囤货,有需要就选择。RackNerd 商家这次2022农历新年也是有几款年付套餐。低至RackNerd VPS...

LayerStack$10.04/月(可选中国香港、日本、新加坡和洛杉矶)高性能AMD EPYC (霄龙)云服务器,

LayerStack(成立于2017年),当前正在9折促销旗下的云服务器,LayerStack的云服务器采用第 3 代 AMD EPYC™ (霄龙) 处理器,DDR4内存和企业级 PCIe Gen 4 NVMe SSD。数据中心可选中国香港、日本、新加坡和洛杉矶!其中中国香港、日本和新加坡分为国际线路和CN2线路,如果选择CN2线路,价格每月要+3.2美元,付款支持paypal,支付宝,信用卡等!...

JustHost:俄罗斯/新西伯利亚vps,512MB内存/5GB空间/不限流量/200Mbps/KVM/自由更换IP,$1.57/月

justhost怎么样?justhost是一家俄罗斯主机商,2006年成立,提供各种主机服务,vps基于kvm,有HDD和SSD硬盘两种,特色是200Mbps不限流量(之前是100Mbps,现在升级为200Mbps)。下面是HDD硬盘的KVM VPS,性价比最高,此外还有SSD硬盘的KVM VPS,价格略高。支持Paypal付款。国内建议选择新西伯利亚或者莫斯科DataLine。支持Paypal付...

java文件传输为你推荐
at89s52单片机AT89S52单片机和AT89C51单片机的区别华为总裁女儿为啥姓孟孟晚舟姓孟,任正非姓任,孟晚舟怎么是任正非的女?awv请问awv是什么样的格式啊?模式识别算法算法是编程么?考研学模式识别方向,编程多么?qq网络硬盘我QQ的网络硬盘怎么啦?腾讯技术腾讯QQ是谁研发的?在那一年上市的?微软操作系统下载微软原版xp系统下载网址是哪个啊?有没有免费就可以下载的?网站建立需要多少钱创立网站要多少钱activitygroupactivityGroup子activity跳转的问题腾讯贴吧QQ应该有贴吧的吧?那怎样进QQ贴吧?在(=@__@=)哪里?
瓦工 bluehost webhosting rak机房 回程路由 青果网 jsp空间 爱奇艺vip免费试用7天 中国网通测速 息壤代理 昆明蜗牛家 如何注册阿里云邮箱 支持外链的相册 吉林铁通 华为云盘 路由跟踪 我的世界服务器ip smtp服务器地址 电信网络测速器 谷歌台湾 更多