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

港云网络(¥1/月活动机器),香港CN2 4核4G 1元/月 美国CN2

港云网络官方网站商家简介港云网络成立于2016年,拥有IDC/ISP/云计算资质,是正规的IDC公司,我们采用优质硬件和网络,为客户提供高速、稳定的云计算服务。公司拥有一流的技术团队,提供7*24小时1对1售后服务,让您无后顾之忧。我们目前提供高防空间、云服务器、物理服务器,高防IP等众多产品,为您提供轻松上云、安全防护。点击进入港云网络官方网站港云网络中秋福利1元领【每人限量1台】,售完下架,活...

Gigsgigscloud($9.8)联通用户优选日本软银VPS

gigsgigsCloud日本东京软银VPS的大带宽配置有100Mbps、150Mbps和200Mbps三种,三网都走软银直连,售价最低9.8美元/月、年付98美元。gigsgigscloud带宽较大延迟低,联通用户的好选择!Gigsgigscloud 日本软银(BBTEC, SoftBank)线路,在速度/延迟/价格方面,是目前联通用户海外VPS的最佳选择,与美国VPS想比,日本软银VPS延迟更...

台湾CN2云服务器 2核2G 5M 5IP 台湾物理服务器 E5x2 64G 20M 5IP

提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...

java文件传输为你推荐
企鹅医生企鹅医生里的医生是真的么?可信么?chinapay什么是银联Chinapay CD卡?自动识别查询快递单号查看单号怎么知道是那个快递公司的csonline2反恐精英online2什么时候出视频压缩算法MP4视频压缩,比特率如何计算。企业资源管理系统企业管理系统有哪些?华为总裁女儿为啥姓孟孟姜女为什么不姓孟?vga接口定义vga线有几种vga接口定义主板VGA接口两排针脚的循序是怎么排列jstz举手望,草上马跑,打什么数字?
域名查询软件 linuxapache虚拟主机 购买域名和空间 海外服务器 gitcafe qq数据库 镇江联通宽带 免费网站申请 域名转向 789电视网 鲁诺 电信托管 沈阳主机托管 服务器维护 永久免费空间 浙江服务器 nnt 重庆联通服务器托管 windowsserverr2 阿里云宕机故障 更多