0

Can't find out how to add a profile picture to client when regstration in data base in web application using JSP, Registration Servlet and Entity Manager JPA for Mysql. For now I did this:

Registration Servlet

        final String username = request.getParameter("username");
    final String pass = request.getParameter("password");
    final String email = request.getParameter("email");
    String message ="";

    Part filePart = request.getPart("photo"); // Retrieves <input type="file" name="file">
    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
    InputStream fileContent = filePart.getInputStream();

User Entity

    @Id
@Column(nullable = false)
private String username;

@Column(nullable = false)
private String password;


@Column(nullable = false)
private String email;

@Lob
@Column(name="PROFILE_PIC")
private byte[] image;

But I can't figure out how to correctly add the User in database which his profile picture to display it after when he logs in. The add without image in my UserDAO works fine but I don't know how to deal with images? MY DAO

    @Override
public void add(String username,String password, String email, byte[] image) {
    User user = new User(username,password,email);
    //em.createQuery( "INSERT INTO user (username, email,password, image) VALUES (?, ?, ?)");
    em.persist(user);
K.mady
  • 13
  • 6
  • Just convert `InputStream` to `byte[]` and you're done. This is basic Java. See abovelinked duplicate for different ways. – BalusC Apr 11 '20 at 11:56
  • There's a download link in the answer you have read. The duplicate shows many other ways. Try reading text in answers as well instead of only copypasting code blocks. – BalusC Apr 11 '20 at 12:05
  • but I can't use IOUtils in my servlet, the imports aren't accepted, is it a previous version which is no more working or what? I should use this import org.apache.commons.io.IOUtils? all the imports suggested by EclipseIDE Doesn't provide the toByteArray (inputstream in) so I'm getting error ? – K.mady Apr 11 '20 at 12:11
  • If you don't have IOUtils, just use plain Java? Try reading beyond the first answer for many other ways. – BalusC Apr 11 '20 at 12:32
  • Ok, thnaks for your help! – K.mady Apr 11 '20 at 12:42

0 Answers0