java文件共享应用程序,用户日志记录
好的,我对我的文件共享应用程序有很多疑问,我不知道从哪里开始。我的 Java 知识非常有限,我会对您为我提供的任何帮助感到满意。
话虽如此,问题来了。
首先,我正在开发一个用户登录方法,它需要看起来像这样:
import java.io.File;
import java.util.ArrayList;
public class User {
String username;
String IPAdresa;
public User(String username, String IPAdresa) {
this.username = username.toLowerCase();
this.IPAdresa = IPAdresa;
}
public void fileList() {
ArrayList<String> list = new ArrayList<String>();
File folder = new File("C:\\userfolder");
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
list.add(i, files[i].toString());
}
}
}
如您所见,我有一个用户类,其中包含有关用户的参数,例如用户名和 IP 地址,以及列出来自某个特定文件的 fileList 方法文件夹并创建包含这些文件名作为字符串的数组列表。
我要做的下一件事是创建一个类或方法,为客户端/用户提供搜索功能。例如,当用户登录到应用程序时,他会想要搜索某个文件,并且他会将其共享文件夹中的文件列表提供给其他用户。按照我对导师的理解,Request 类需要包含能够在用户各自的文件列表中搜索的每个循环。我不知道如何实现这一点,并且在使用数组列表方面遇到很多问题。
它应该看起来像这样:(到目前为止我正在使用某种伪代码)
public class RequestForFile {
ArrayList list = new ArrayList();
User user = new User("Slavisha","123.23.34.45");
public RequestForFile() {
list.add(user);
foreach (User user in userlist) {
foreach (String str in User.fileList()) {
if (str == request)
...
}
}
}
}
下一个问题: 用户如何登录Java应用程序?我一整天都在思考这个问题,并试图绕过它,但我失败了。我还没有 GUI/Swing,希望最终能做到。
我还有 3 个类,分别代表 Client、Server 和 HandleClient。
正如我所说,欢迎任何贡献。我肯定会带着更多问题回来。 谢谢
OK I have so much questions regarding my file sharing application that I don't know where to start. My Java knowledge is very limited and I will be satisfied with any help you provide me.
That being said, here come the questions.
Firstly, I'm working on a user login method that needs to look sort of like this:
import java.io.File;
import java.util.ArrayList;
public class User {
String username;
String IPAdresa;
public User(String username, String IPAdresa) {
this.username = username.toLowerCase();
this.IPAdresa = IPAdresa;
}
public void fileList() {
ArrayList<String> list = new ArrayList<String>();
File folder = new File("C:\\userfolder");
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
list.add(i, files[i].toString());
}
}
}
As you can see I have user class that contains parameters regarding user, such as username and IPAddress, and also fileList method that lists files from a certain folder and creates the arraylist containing those file names as strings.
The next thing I have to do is make a class or a method that provides search function to clients/users. For example, when users logs on to the application, he will want to search for a certain file and also he will provide the list of files from his shared folder to other users. The way I understood my menthor, the Request class needs to contain for each loops that are able to search within the users respective file lists. I'm not sure how to pull that off and I have a lot of problems regarding working with array lists.
This how it's supposed to look like approximately: (I'm using sort of pseudo-code for this one so far)
public class RequestForFile {
ArrayList list = new ArrayList();
User user = new User("Slavisha","123.23.34.45");
public RequestForFile() {
list.add(user);
foreach (User user in userlist) {
foreach (String str in User.fileList()) {
if (str == request)
...
}
}
}
}
Next question:
How do users log in to Java application? I've been thinking about it all day and tried to get around it but I just failed. I don't have GUI/Swing yet, hope to do it in the end.
I have 3 more classes that represent Client, Server and HandleClient.
As I said any contribution is welcome. I will be back with more questions for sure.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在一个问题中问了太多问题。一个问题涉及您的 RequestForFile 对象如何知道它正在与哪位用户打交道。这就是登录问题。建议您单独提出。
我们暂时假设我们知道是该 IP 地址上的 Slavisha 发出请求。这里有很多问题:
1)。被要求什么? RequestForFile() 不带任何参数来说明正在请求哪个文件。那么RequestForFile()的职责是什么?它应该代表一个请求吗?它负责实际查找文件吗?退货吗?
2)。您的 User.fileList() 方法实际上并没有做任何有用的事情。它不会返回任何内容,因此完成后它发现的所有内容都会丢失。
3)。无论如何,每个用户似乎都在同一个文件夹中查找:“C:\userfolder”您是否打算为每个用户拥有一个单独的目录。
4)。查看列表可能效率不高,您需要阅读有关集合和映射的内容。
我认为你需要进行非常详细的重新设计。您的用户类需要回答诸如“此用户是否有该文件”之类的问题。 REquest 类需要识别特定的用户并向其请求文件。我不清楚您打算如何将文件从客户端传输到服务器。
我应该说,将客户端/服务器编程作为早期教育练习是相当雄心勃勃的。
You're asking too many questions in one question. One question concerns how your RequestForFile object knows which user it's dealing with. That's the log-in question. Suggest you raise that separately.
Let's assume for the moment that we know it's Slavisha on that ipaddress who is asking. There's quite a few problems here:
1). What's being asked for? RequestForFile() doesn't take any parameter to say which file is being requested. So what's the resposbility of RequestForFile()? is it suppsed to represent one request? Is it responsible for actually finding the file? Returning it?
2). Your User.fileList() method doesn't actually do anything useful. It doesn't return anything, so on completion everything it has figured out it lost.
3). Anyway, every user seems to be looking in the same folder: "C:\userfolder" did you intend to have a separate directory for each user.
4). Looking through lists is probably not efficient, you need to read about Sets and Maps.
I think you need a pretty detailed redesign. Your user class needs to answer questions such as "does this user have that file". The REquest class needs to identify a particular User and ask it for the file. I'm not clear how you then intend to transfer the file from client to server.
I should say that doing Client/Server programming as an early education exercise is pretty ambitious.
我不太清楚你所说的“登录”是什么意思。如果(我相信)您想要识别 Java 应用程序中的用户,请使用:
它为您提供当前运行应用程序的用户的用户名。
I'm not quite sure what you mean by 'logging in'. If (as I believe) you want to identify the user in your Java application, use:
which gives you the user name of the user currently running your application.
GUI 将用于为用户提供命令行以外的其他内容,但 Swing 或 AWT 不提供您似乎将它们关联起来的功能 - 即连接服务器和客户端,允许用户登录服务器的运行实例。 GUI 将使这个过程更加用户友好,但从根本上讲,您正在谈论使用套接字让客户端连接到服务器(已知的 IP 地址和为连接设置的开放端口),发送一些用户的信息(名称共享文件),然后从服务器接收所有可能的共享文件的列表。然后,客户端可以在本地搜索该列表。
您可以以相反的方式执行此操作 - 服务器从客户端获取请求,进行搜索,然后将结果发送给客户端。取决于您想将大部分工作量放在哪里。无论哪种方式,您都在谈论通过套接字连接发送字符串。至少,直到您从查看文件名列表到实际将文件从一个客户端发送到另一个客户端为止。
因此,您可能想开始查看有关套接字的教程看看不同计算机上的两个独立程序如何相互发送信息。
A GUI would be used to give the user something other than the command line to look at, but Swing or AWT don't provide the functionality you seem to associate them with - namely, connecting the Server and Client, allowing the user to log into the running instance of the Server. The GUI would make this process more user-friendly, but fundamentally you're talking about using sockets to let the Client connect to the Server (known IP address and open port set for the connection), sends some of the User's information (the names of shared files) and then receives a list of all possible shared files from the Server. The Client can then locally search through this list.
You can do this the other way around - the Server gets a request from the Client, does the searching, and just sends a result to the Client. Depends on where you want to put most of your workload. Either way, you're talking about sending Strings through a socket connection. At least, until you go from looking at lists of file names to actually sending the files from one Client to another.
So you probably want to start looking at a tutorial on sockets to look at how two separate programs on different computers can send information to one another.