-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rename.java
76 lines (67 loc) · 2.58 KB
/
Rename.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package finalProj;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.FileUpload;
/**
* Servlet implementation class Rename
*/
@WebServlet("/Drive/Rename")
public class Rename extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/views/RenameView.jsp?name=" + request.getParameter("name")).forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String newName = request.getParameter("newName");
String oldName = request.getParameter("name");
String user = getUserName(request);
String fileDir = getServletContext().getRealPath("/WEB-INF/uploads/" + user);
File folder = new File(fileDir);
if(!newName.contains(".")) {
newName += oldName.substring(oldName.indexOf("."));
}
ArrayList<FileUpload> files = (ArrayList<FileUpload>) this.getServletContext().getAttribute("fileUploads");
if(newNameExists(newName, files))
{
doGet(request, response);
}
for(File file : folder.listFiles())
{
if(file.getName().equals(oldName))
{
File newFile = new File(folder + "/" + newName);
file.renameTo(newFile);
response.sendRedirect("/CS3220Project/Drive");
}
}
}
private String getUserName(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies) {
if(cookie.getName().equals("userName")) {
return cookie.getValue();
}
}
}
return null;
}
private boolean newNameExists(String newName, ArrayList<FileUpload> files)
{
for(FileUpload file : files) {
if(file.getFileName().equals(newName))
return true;
}
return false;
}
}