如何使GUI客户端在Java中上传文件 [英] How to make GUI Client to upload file in java

查看:66
本文介绍了如何使GUI客户端在Java中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,我想制作一个GUI应用程序,该程序将文件从客户端上传到服务器.当我单击浏览按钮时,文件将以字节形式复制,因为我们在网络上以字节为单位传输数据,但是当我单击上传按钮时,文件无法上传.

Please help me I want to make a GUI application which upload a file from client to server. when I click on browse button then file is copy in bytes form because we travel data in bytes on network but when I click on upload button then file cannot upload.

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

推荐答案

选择文件时,基本上是在读取文件的内容,丢弃内容,然后尝试发送最后的 int 值您从中读取到服务器.一个文件可能有很多很多很多的字节(想想千兆字节),一个 int 不会保存那么多的信息(可怜的 int ).

You are basically reading the contents of the file when you select it, discard the contents and then trying to send the last int value you read from it to the server. A file my be many, many, many bytes long (think giga bytes), an int ins't going to hold that much information (poor int).

相反,当用户选择一个 File 时,您应该保留对该文件的引用,直到您真正需要它为止.

Instead, when the user selects a File, you should maintain a reference to it until you actually need it...

public void actionPerformed(ActionEvent e) {
    try {
        if (e.getSource() == b) {
            int x = fc.showOpenDialog(null);
            if (x == JFileChooser.APPROVE_OPTION) {
                fileToBeSent = fc.getSelectedFile();
                tf.setText(f1.getAbsolutePath());
                b1.setEnabled(true);
            } else {
                fileToBeSent = null;
                tf.setText(null;);
                b1.setEnabled(false);
            }
        }
        if (e.getSource() == b1) {
            send();
        }
    } catch (Exception ex) {
    }
}

当用户按下发送"按钮时,您只需将文件输入流复制到套接字输出流...

When the user presses the "send" button, you would simply copy the file input stream to the sockets output stream...

FileInputStream in = null;
try {
    in = new FileInputStream(fileToBeSent);
    byte[] buffer = new byte[1024];
    int bytesIn = -1;
    while ((bytesIn = in.read(buffer)) != -1) {
        dout.write(buffer, 0, bytesIn);
    }
    dout.flush();
} finally {
    try {
        in.close();
    } catch (Exception exp) {
    }
}

那至少应该使您更近一步.

That should at least get you one step closer.

您可能遇到的下一个问题是,在传输进行期间,UI将停止响应.

The next problem you are likely to hit is the fact that the UI will stop responding while the transfer is underway.

在这种情况下,您应该看看 Swing中的并发

In this case, you should take a look at Concurrency in Swing.

现代UI需要处理客户端计算机的许多不同特征,包括不同的字体,字体渲染功能,DPI,屏幕分辨率等等.通过使用绝对布局,您将自己置于一个非常黑暗和深的洞中.

Modern UI's need to deal with many different characteristics of client machines, included different fonts, font rendering capabilities, DPI, screen resolutions and lots more. You're putting your self in a very dark and deep hole by using absolute layouts.

您应该利用布局管理器API,该API旨在缓解这些问题带来的压力,并使您的UI在不同的操作环境中更加适应.

You should take advantage of the layout manager API which is designed to easy the pressure of these concerns and make you UI more accommodating on different operating environments.

看看在容器内布置组件更多细节

您可能还想进一步了解基本I/O 有关套接字的所有信息以了解更多详细信息...

You may also want to take a closer look at Basic I/O and All about sockets for more details...

这篇关于如何使GUI客户端在Java中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆