我计划用Java开发Web应用程序。我想要这些东西-首先,我必须在桌面上创建一个文件夹,并且在此文件夹内,假设我再创建5个文件夹,名称分别为A,B,C,D,E。现在,如果我要添加一些文本或图像文档,则需要在文件夹A上。那么只有通过用户名A和密码(某些密码)登录的人才能访问此特定文件。与其他文件夹相同。我的意思是,如果管理员创建5个用户,名称分别为A,B,C,D,E。每个用户都可以访问和下载它的特定文件。
并且如果管理员在A的文件夹中放置了一些新文件,则当用户A通过Web应用程序登录时,他只能访问文件夹A中的新添加的文件。
先生,这件事我想做,但是如何启动我不知道。请帮我。
谢谢。
您将必须在web.xml中提供根文件夹的路径,以便您的servlet可以访问它。该文件夹应位于服务器的安装文件夹之外
<servlet>
<servlet-name>CreateFolder</servlet-name>
<servlet-class>pckg.CreateFolder</servlet-class>
</servlet>
<context-param>
<param-name>ParentFolder</param-name>
<param-value>/z/y/x/users</param-value>
</context-param>
<context-param>
<param-name>FileName</param-name>
<param-value>xyz.png</param-value>
</context-param>
CreateFolder Servlet:-
private String userFolder;
public void init(ServletConfig config) {
userFolder= getServletContext().getInitParameter("ParentFolder");
}
public doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException){
String userName = request.getParameter("UserName");
String location=""
if(userName != null){
location +="/"+userName;
if (!location .exists()) {
location .mkdirs(); //If does not exists then create folder
}
}
}
UploadFile Servlet:-
private String userFolder;
private String fileName;
public void init(ServletConfig config) {
userFolder= getServletContext().getInitParameter("ParentFolder");
fileName= getServletContext().getInitParameter("FileName");
}
public doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException){
String userName = request.getParameter("UserName");
String location=""
if(userName != null){
location +="/"+userName;
if (!location .exists()) {
UploadeFile(location );
}
}
}
private void UploadeFile(String location ){
String file = location+"/"+fileName;
//Code to upload the file
MoveFile(file);//Move to different location
}
UserName
经过适当的验证后,首先获得。从中读取文件夹路径web.xml
。将追加UserName
到文件夹路径,检查路径是否存在。如果路径不存在,则创建路径。
在将文件上传给用户时。成功完成上传后,将文件移到其他位置。因此,下次用户登录系统时,他只会看到新的文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句