简体   繁体   English

如何确定谁在Java中将文件上传到ftp?

[英]How to determine who uploaded file to ftp in java?

I have a ftp client that allows users to upload files. 我有一个ftp客户端,允许用户上传文件。 I want determine which user/host/... uploaded that file.All people using the same username to upload file.the only difference is that they use different computer. 我想确定哪个用户/主机/ ...上传了该文件。所有使用相同用户名的人都上传了文件。唯一的区别是他们使用不同的计算机。

Is there any way I can track which user uploaded that file? 有什么办法可以跟踪哪个用户上传了该文件?

public static void uploadFileToServerViaSunFtp(final String source,final JPanel panel, final JTextArea textArea)
{
    SwingWorker uploadWorker = new  SwingWorker<Boolean,String>()
    {
        @Override
        protected Boolean doInBackground() throws Exception
        {
            publish("File "+FilenameUtils.getName(source).concat(" starts uploading on ") + String.valueOf(Calendar.getInstance().getTime() + "\n"));
            boolean success = false;
            FtpClient client;
            try
            {
                client = new FtpClient(server.getFtpServer());
                client.login(server.getUsername(), server.getPassword());
                client.cd("PC");
                client.binary();
                int BUFFER_SIZE = 10240;
                byte[] buffer = new byte[BUFFER_SIZE];

                File f = new File(source);
                FileInputStream in = new FileInputStream(source);
                // *** If uploading take long time, progress bar will show up ***
                InputStream inputStream = new BufferedInputStream(
                        new ProgressMonitorInputStream(panel, "Uploading " + f.getName(), in));
                OutputStream out = client.put(f.getName());
                while (true) {
                    int bytes = inputStream.read(buffer);
                    if (bytes < 0)
                        break;
                    out.write(buffer, 0, bytes);
                }
                out.close();
                in.close();
                inputStream.close();
                client.closeServer();
                success = true;
            }
            catch (IOException e)
            {
                OeExceptionDialog.show(e);
            }
            return success;
        }

        @Override
        protected void done()
        {
            super.done();
            try {
                if(get())
                    textArea.append("File "+FilenameUtils.getName(source).concat(" Uploaded successfully.\n"));
        }
    };
    uploadWorker.execute();
}

Files stored on an FTP server have owners ( oid ) and groups ( gid ). FTP服务器上存储的文件具有所有者( oid )和组( gid )。

The purpose of logging into an FTP server is to have you both authenticated and authorized. 登录FTP服务器的目的是使您同时经过身份验证和授权。 Therefore you should have different users as in 'different usernames' for each user who will be uploading the file. 因此,对于将要上传文件的每个用户,您应该具有“不同用户名”中的不同用户。

It makes no sense to have the same username used by all your users, as this will make it impossible to make a distinction. 为所有用户使用相同的用户名是没有意义的,因为这将导致无法区分。

Checking the IP address is complete non-sense. 检查IP地址是完全没有意义的。 The reason I am saying this is that anyone can hijack an IP address, if they're on your local network. 我之所以这样说,是因为任何人都可以在您的本地网络上劫持IP地址。 There's little validation here and you can't trust your local DNS to do the job for you. 这里几乎没有验证,您不能相信您的本地DNS为您完成这项工作。

Simply use different usernames. 只需使用不同的用户名。

How is your client-side code related to the question? 您的客户端代码与该问题有什么关系? Of course the server knows the name of the user who logged in before uploading the file and from what IP address connection was made (except when IP address was masked by proxy/NAT). 当然,服务器会知道上传文件之前登录的用户名以及建立IP地址的连接(除非IP地址被代理/ NAT屏蔽)。 Moreover, properties of uploaded file are often set to have the owner and group on Unix or security attributes on Windows corresponding to owner and group of the user who uploaded the file. 此外,上载文件的属性通常设置为在Unix上具有所有者和组,在Windows上具有与上载文件的用户的所有者和组相对应的安全属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM