diff --git a/src/main/java/com/example/server_site_api/config/SecurityConfig.java b/src/main/java/com/example/server_site_api/config/SecurityConfig.java index 53a2920..c548455 100644 --- a/src/main/java/com/example/server_site_api/config/SecurityConfig.java +++ b/src/main/java/com/example/server_site_api/config/SecurityConfig.java @@ -24,7 +24,7 @@ public class SecurityConfig { http // 1. Authorize HTTP requests based on URL patterns .authorizeHttpRequests(auth -> auth - .requestMatchers("/", "/public/**", "/auth/**").permitAll() // all bat dau voi public se mo + .requestMatchers("/", "/public/**", "/auth/**", "/uploads/**").permitAll() // all bat dau voi public se mo .requestMatchers("/users/create-new").hasRole("ADMIN") // phai co role admin moi co the truy cap .anyRequest().authenticated() // tat ca api con lai deu require login ) diff --git a/src/main/java/com/example/server_site_api/controllers/UploadController.java b/src/main/java/com/example/server_site_api/controllers/UploadController.java new file mode 100644 index 0000000..79f2112 --- /dev/null +++ b/src/main/java/com/example/server_site_api/controllers/UploadController.java @@ -0,0 +1,37 @@ +package com.example.server_site_api.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +@Controller +@RequestMapping("uploads") +public class UploadController { + @GetMapping() + public String uploadPage() { + return "Upload"; + } + + @PostMapping + @ResponseBody + public String save(@RequestParam("avatar") MultipartFile file) throws IOException { + String dirSave = "C:/server_file/"; + + if(!file.isEmpty()) { + File dir = new File(dirSave); + String fileName = "/a.png"; + Path path = Paths.get(dir + fileName); + Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING); + } + + System.out.println("file" + file); + return "thanh cong"; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 85ed006..0817ebe 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -6,4 +6,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/26_05_db?createDatabaseIfNotEx spring.datasource.username=root spring.datasource.password= -spring.jpa.hibernate.ddl-auto=update \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update + +spring.servlet.multipart.max-file-size=10MB diff --git a/src/main/resources/templates/Upload.html b/src/main/resources/templates/Upload.html new file mode 100644 index 0000000..ad0a51c --- /dev/null +++ b/src/main/resources/templates/Upload.html @@ -0,0 +1,14 @@ + + +
+ +