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

GreenCloudVPS$20/年多国机房可选,1核@Ryzen 3950x/1GB内存/30GB NVMe/10Gbps端口月流量2TB

GreencloudVPS此次在四个机房都上线10Gbps大带宽VPS,并且全部采用AMD处理器,其中美国芝加哥机房采用Ryzen 3950x处理器,新加坡、荷兰阿姆斯特丹、美国杰克逊维尔机房采用Ryzen 3960x处理器,全部都是RAID-1 NVMe硬盘、DDR4 2666Mhz内存,GreenCloudVPS本次促销的便宜VPS最低仅需20美元/年,支持支付宝、银联和paypal。Gree...

DogYun春节优惠:动态云7折,经典云8折,独立服务器月省100元,充100送10元

传统农历新年将至,国人主机商DogYun(狗云)发来了虎年春节优惠活动,1月31日-2月6日活动期间使用优惠码新开动态云7折,经典云8折,新开独立服务器可立减100元/月;使用优惠码新开香港独立服务器优惠100元,并次月免费;活动期间单笔充值每满100元赠送10元,还可以参与幸运大转盘每日抽取5折码,流量,余额等奖品;商家限量推出一款年付特价套餐,共100台,每个用户限1台,香港VPS年付199元...

两款半月湾 HMBcloud 春节88折日本和美国CN2 VPS主机套餐

春节期间我们很多朋友都在忙着吃好喝好,当然有时候也会偶然的上网看看。对于我们站长用户来说,基本上需要等到初八之后才会开工,现在有空就看看是否有商家的促销。这里看到来自HMBcloud半月湾服务商有提供两款春节机房方案的VPS主机88折促销活动,分别是来自洛杉矶CN2 GIA和日本CN2的方案。八八折优惠码:CNY-GIA第一、洛杉矶CN2 GIA美国原生IP地址、72小时退款保障、三网回程CN2 ...

java文件传输为你推荐
腾讯举报中心腾讯的投诉电话是多少啊?cpu监控CPU检测软件有哪些 5款CPU检测工具介绍 (svn服务器搭建如何在本机搭建SVN服务器oa办公系统下载oa办公软件哪里可以下载?搜索引擎的概念搜索引擎的工作原理是什么及发展历史awvawv转换器哪里下?12种颜色12种颜色的英语怎么写,用中文怎么读jstz泰州哪里有民工市场什么是生态系统什么是生态环境?微信智能机器人有一个人加我微信,他说他自己是图灵机器人,我想问一下这是啥软件怎么可以自动回复微信?
郑州服务器租用 中国万网域名 com域名抢注 qq云存储 fastdomain 美国翻墙 bluehost wordpress技巧 鲜果阅读 日本空间 发包服务器 panel1 anylink 泉州电信 中国电信测网速 广州服务器 空间合租 带宽租赁 云营销系统 服务器维护 更多