java 在gui中上传文件_如何使GUI Client在java中上传文件

请帮帮我,我想制作一个GUI文件,将文件从客户端上传到服务器.

当我点击浏览按钮然后文件是以字节形式复制,因为我们在网络上以字节为单位传输数据,但是当我点击上传按钮时,文件无法上传.

import javax.swing.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

class ClientUpload extends JFrame implements ActionListener {

JFileChooser fc;

JButton b, b1;

JTextField tf;

FileInputStream in;

Socket s;

DataOutputStream dout;

DataInputStream din;

int i;

ClientUpload() {

super("client");

tf = new JTextField();

tf.setBounds(20, 50, 190, 30);

add(tf);

b = new JButton("Browse");

b.setBounds(250, 50, 80, 30);

add(b);

b.addActionListener(this);

b1 = new JButton("Upload");

b1.setBounds(250, 100, 80, 30);

add(b1);

b1.addActionListener(this);

fc = new JFileChooser();

setLayout(null);

setSize(400, 300);

setVisible(true);

try {

s = new Socket("localhost", 10);

dout = new DataOutputStream(s.getOutputStream());

din = new DataInputStream(s.getInputStream());

send();

} catch (Exception e) {

}

}

public void actionPerformed(ActionEvent e) {

try {

if (e.getSource() == b) {

int x = fc.showOpenDialog(null);

if (x == JFileChooser.APPROVE_OPTION) {

copy();

}

}

if (e.getSource() == b1) {

send();

}

} catch (Exception ex) {

}

}

public void copy() throws IOException {

File f1 = fc.getSelectedFile();

tf.setText(f1.getAbsolutePath());

in = new FileInputStream(f1.getAbsolutePath());

while ((i = in.read()) != -1) {

System.out.print(i);

}

}

public void send() throws IOException {

dout.write(i);

dout.flush();

}

public static void main(String... d) {

new ClientUpload();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值