This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateprofile.jsp
184 lines (164 loc) · 6.29 KB
/
updateprofile.jsp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<%@page import="java.nio.file.Files"%>
<%@page import="java.util.Date"%>
<%@page import="com.sun.mail.smtp.SMTPTransport"%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.Message"%>
<%@page import="javax.mail.Session"%>
<%@page import="java.util.Properties"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.security.cert.CertPathValidatorException.Reason"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page import = "java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import = "javax.servlet.http.*" %>
<%@ page import = "org.apache.commons.fileupload.*" %>
<%@ page import = "org.apache.commons.fileupload.disk.*" %>
<%@ page import = "org.apache.commons.fileupload.servlet.*" %>
<%@ page import = "org.apache.commons.io.output.*" %>
<%@ include file="jspConfig.jsp" %>
<%@ include file="mail.jsp" %>
<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
try {
if(session.getAttribute("user") == null)
{
%>
<script>document.location = "/hostelFeedback/"</script>
<%
}
else
{
String roll = (String)session.getAttribute("user");
String pname = "";
int room = 0;
String hostel = "";
String alert = "";
String contentType1 = request.getContentType();
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
if ((contentType1.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory1 = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory1.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
//factory1.setRepository(new File("c:\\temp"));
// Create a new file upload handler
ServletFileUpload upload1 = new ServletFileUpload(factory1);
// maximum file size to be uploaded.
upload1.setSizeMax( maxFileSize );
// Parse the request to get file items.
List fileItems1 = upload1.parseRequest(request);
// Process the uploaded file items
Iterator i1 = fileItems1.iterator();
FileItem fi1;
String name = "";
String param = "";
while ( i1.hasNext () ) {
fi1 = (FileItem)i1.next();
if ( fi1.isFormField () ) {
// Get the uploaded file parameters
name = fi1.getFieldName();
param = fi1.getString();
if(name.equals("name"))
pname = param;
else if(name.equals("roomNo"))
room = Integer.parseInt(param);
else if(name.equals("hostel"))
hostel = param;
}
if(name.equals("hostel"))
break;
}
//
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(mysqlURL,mysqlUser,mysqlPass);
PreparedStatement ps;
ResultSet rs;
ps = conn.prepareStatement("select data from student where email = ?");
ps.setString(1, roll);
rs = ps.executeQuery();
rs.next();
String data = rs.getString(1);
ps = conn.prepareStatement("update student set name = ?,hostel = ?,roomNo = ? where email = ?");
ps.setString(1, pname);
ps.setString(2, hostel);
ps.setInt(3, room);
ps.setString(4, roll);
ps.executeUpdate();
ps.close();
File file ;
File newPath = new File(storageURL + data + "/");
if(newPath.isDirectory() == false)
newPath.mkdir();
String filePath = storageURL + data + "/";
// delete other profile image before uploading a new one
String filepath = storageURL + data + "/";
File profilePath = new File(filepath);
File profileImage = new File("");
if(profilePath.isDirectory())
{
for(String each:profilePath.list())
{
profileImage = new File(filePath + each);
if(profileImage.isFile())
{
if(each.substring(0,each.lastIndexOf(".")).equals("profile"))
{
Files.delete(profileImage.toPath());
}
}
}
}
//if ((contentType1.indexOf("multipart/form-data") >= 0))
DiskFileItemFactory factory = new DiskFileItemFactory();
try {
// Parse the request to get file items.
//List fileItems = upload.parseRequest(request);
// Process the uploaded file items
//Iterator i = fileItems.iterator();
while(i1.hasNext()) {
fi1 = (FileItem)i1.next();
if ( !fi1.isFormField () ) {
// Get the uploaded file parameters
String fieldName = fi1.getFieldName();
String fileName = fi1.getName();
fileName = fileName.replace(" ", "");
if(fileName.equals("") == false )
{
String ext = fileName.substring(fileName.lastIndexOf("."));
fileName = "profile" + ext;
boolean isInMemory = fi1.isInMemory();
long sizeInBytes = fi1.getSize();
// Write the file
if( fileName.lastIndexOf("/") >= 0 ) {
file = new File( filePath +
fileName.substring( fileName.lastIndexOf("/"))) ;
} else {
file = new File( filePath +
fileName.substring(fileName.lastIndexOf("/")+1)) ;
}
fi1.write( file ) ;
}
}
}
} catch(Exception ex) {
}
} else {
}
alert = "Profile Updated...";
session.setAttribute("alert", alert);
%>
<script>document.location = "/hostelFeedback/"</script>
<%
}
}
catch(Exception e) {
%>
<jsp:forward page="/error/"></jsp:forward>
<%
}
%>