Commit bef7a9dc by Bogdan Andjelkovic

profile picture upload - solved

parent 71ac5d4c
...@@ -7,10 +7,8 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -7,10 +7,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@Controller @Controller
public class AppController public class AppController
...@@ -43,10 +41,10 @@ public class AppController ...@@ -43,10 +41,10 @@ public class AppController
} }
@PostMapping("/register-proccess") @PostMapping("/register-proccess")
public String postRegisterProccess(@ModelAttribute Korisnik korisnik) public String postRegisterProccess(@ModelAttribute Korisnik korisnik, @RequestParam("image") MultipartFile multipartFile)
{ {
System.out.println(korisnik); System.out.println(korisnik);
kRepo.insert( korisnik); kRepo.insert( korisnik, multipartFile);
return("redirect:/login"); return("redirect:/login");
} }
......
...@@ -18,15 +18,7 @@ public class Korisnik ...@@ -18,15 +18,7 @@ public class Korisnik
String sifra; String sifra;
String telefon; String telefon;
int tipId; int tipId;
String slika;
public Korisnik(String ime, String prezime, String email, String sifra, String telefon, int tipId) {
this.ime = ime;
this.prezime = prezime;
this.email = email;
this.sifra = sifra;
this.telefon = telefon;
this.tipId = tipId;
}
@Override @Override
public String toString() { public String toString() {
......
package com.example.SkuciSe.repository; package com.example.SkuciSe.repository;
import com.example.SkuciSe.model.korisnik.Korisnik; import com.example.SkuciSe.model.korisnik.Korisnik;
import com.example.SkuciSe.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.util.Optional; import java.util.Optional;
...@@ -23,9 +27,17 @@ public class KorisnikRepository ...@@ -23,9 +27,17 @@ public class KorisnikRepository
} }
} }
public void insert(Korisnik korisnik) public void insert( Korisnik korisnik, MultipartFile multipartFile)
{ {
String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+new BCryptPasswordEncoder().encode(korisnik.getSifra())+"',1)"; String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
String uploadDir = "user-photos";
try {
FileUploadUtil.saveFile(uploadDir, fileName, multipartFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId, slika) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+new BCryptPasswordEncoder().encode(korisnik.getSifra())+"',1, '"+fileName+"')";
System.out.println( sql);
try { try {
statement.executeUpdate( sql); statement.executeUpdate( sql);
} catch (SQLException e) { } catch (SQLException e) {
...@@ -41,7 +53,7 @@ public class KorisnikRepository ...@@ -41,7 +53,7 @@ public class KorisnikRepository
rs = statement.executeQuery( sql); rs = statement.executeQuery( sql);
while( rs.next()) while( rs.next())
{ {
return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid"))); return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid"), rs.getString("slika")));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -55,7 +67,7 @@ public class KorisnikRepository ...@@ -55,7 +67,7 @@ public class KorisnikRepository
rs = statement.executeQuery( sql); rs = statement.executeQuery( sql);
while( rs.next()) while( rs.next())
{ {
return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid"))); return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid"), rs.getString("slika")));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
</div> </div>
<div class="col-md-12 mt-2" style="color:white;"> <div class="col-md-12 mt-2" style="color:white;">
<input type="file" name="slika" accept="image/png, image/jpeg" /> <input type="file" name="image" accept="image/png, image/jpeg" />
<div class="invalid-feedback">Unesite sliku!</div> <div class="invalid-feedback">Unesite sliku!</div>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment