I have a form to update the information of the profile, in this case a new password and the image. The fact is that changing the password works but upload the image to the database no, I already have a medium blob column in my database for the image. I leave the code here.
First my form
<form name="form1" method="post" action="actualizar"
enctype="multipart/form-data">
<input type="password" name="psw-antigua"><br><br>
<input type="password" name="psw-repeat"><br><br>
<input type="password" name="psw"><br><br>
<input type="file" name="foto"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
And now my java code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.security.*;
public class Actualizar extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e){
System.out.println(e);
return;
}
try{
Connection con;
Statement st,st2;
ResultSet rs;
String SQL,SQL2,Nickname,Contraseña;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Nickname=req.getParameter("nickname");
Contraseña=req.getParameter("psw");
String ContraseñaVieja=req.getParameter("psw-antigua");
InputStream inputStream = null;
Part filePart = req.getPart("foto");
if (filePart != null) {
inputStream = filePart.getInputStream();
}
con = DriverManager.getConnection("jdbc:mysql://localhost/ajedrez", "root", "");
st = con.createStatement();
st2 = con.createStatement();
MessageDigest sha256=MessageDigest.getInstance("SHA-256");
sha256.update(ContraseñaVieja.getBytes("UTF-8"));
byte digest[] = sha256.digest();
StringBuffer sb=new StringBuffer();
for(int i = 0; i < digest.length; i++){
sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
}
String HashViejo=sb.toString();
SQL="SELECT * FROM usuarios WHERE usuarios.Nickname='"+Nickname+"'AND usuarios.Hash='"+HashViejo+"'";
rs=st.executeQuery(SQL);
out.println("<HTML><BODY>");
if (!rs.next())
{
out.println("Contraseña actual incorrecta,<a href=inicio>Vuelve logearte de nuevo.</a>");
}else{
sha256=null;
sha256=MessageDigest.getInstance("SHA-256");
sha256.update(Contraseña.getBytes("UTF-8"));
byte digest2[] = sha256.digest();
StringBuffer sb2=new StringBuffer();
for(int i = 0; i < digest2.length; i++){
sb2.append(Integer.toString((digest2[i] & 0xff) + 0x100, 16).substring(1));
}
String Hash=sb2.toString();
SQL2="UPDATE usuarios SET usuarios.Hash='"+Hash+"' WHERE usuarios.Nickname='"+Nickname+"'";
int u = st2.executeUpdate(SQL2);
if (inputStream != null) {
String sql ="UPDATE usuarios SET usuarios.Imagen=? WHERE usuarios.Nickname='"+Nickname+"'";
PreparedStatement pstmtSave = con.prepareStatement(sql);
pstmtSave.setBlob(1, inputStream);
int row = pstmtSave.executeUpdate();
}
out.println("Cambios en tu perfil realizados con exito. ");
out.println("<a href=inicio>Puedes iniciar sesión con tu cuenta en esta página.</a>");
}
out.println("</BODY></HTML>");
st.close();
st2.close();
con.close();
out.close();
}catch(Exception e){
System.out.println(e);
return;
}
}
}
I get NullPointerException and I've already reviewed it several times.