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

€4.99/月Contabo云服务器,美国高性价比VPS/4核8G内存200G SSD存储

Contabo是一家运营了20多年的欧洲老牌主机商,之前主要是运营德国数据中心,Contabo在今年4月份增设新加坡数据中心,近期同时新增了美国纽约和西雅图数据中心。全球布局基本完成,目前可选的数据中心包括:德国本土、美国东部(纽约)、美国西部(西雅图)、美国中部(圣路易斯)和亚洲的新加坡数据中心。Contabo的之前国外主机测评网站有多次介绍,他们家的特点就是性价比高,而且这个高不是一般的高,是...

Kinponet是谁?Kinponet前身公司叫金宝idc 成立于2013年 开始代理销售美国vps。

在2014年发现原来使用VPS的客户需求慢慢的在改版,VPS已经不能满足客户的需求。我们开始代理机房的独立服务器,主推和HS机房的独立服务器。经过一年多的发展,我们发现代理的服务器配置参差不齐,机房的售后服务也无法完全跟上,导致了很多问题发生,对使用体验带来了很多的不便,很多客户离开了我们。经过我们慎重的考虑和客户的建议。我们在2015开始了重大的改变, 2015年,我们开始计划托管自己...

日本美国站群服务器raksmart站群新增,限量低至月1.99美元

RAKsmart 商家八月份的促销活动今天更新。基本上和上个月的产品套餐活动差不多的,不过也是有简单的微调。对于RAKsmart商家还是比较了解的,他们家产品虽然这两年增加多个机房,以及在VPS主机方案上有丰富的机房和调整到一些自营机房,他们家的策划能力还是有限,基本上每个月的套餐活动都差不多。RAKsmart 在八月份看到有新增香港高防服务器可选,最高100GB防御。同时原来上个月缺货的日本独立...

java文件传输为你推荐
知识分享平台关于分享职场技能的知识付费平台,大家有什么好推荐的吗?chrome系统Chrome OS是什么系统?印度尼西亚国家代码手机上的国家代码是什么数据挖掘项目什么是数据挖掘?从事相关的工作有什么要求?qq网络硬盘我QQ的网络硬盘怎么啦?assemblyinfoCSE和CS有什么区别微软操作系统下载微软的系统到哪下载activitygroupAndroid中如何在ActivityGroup里面监听back按钮,使得可按要求实现哪个activity可返回,哪个不需要。。点心os什么是点心os?手机操作系统和手机区别么?一个是硬件一个是软件?听说炒的好火?软件开发技术文档软件开发技术的目录
青岛虚拟主机 免费二级域名申请 免费申请域名 东莞电信局 lunarpages 视频存储服务器 特价空间 permitrootlogin 12306抢票攻略 directadmin 火车票抢票攻略 标准机柜尺寸 卡巴斯基永久免费版 150邮箱 softbank邮箱 购买国外空间 无限流量 百度云空间 腾讯云平台 alertpay 更多