This commit is contained in:
2026-06-02 21:12:15 +07:00
parent 5c9e366bab
commit 7d310a41a0
4 changed files with 55 additions and 2 deletions

View File

@@ -24,7 +24,7 @@ public class SecurityConfig {
http http
// 1. Authorize HTTP requests based on URL patterns // 1. Authorize HTTP requests based on URL patterns
.authorizeHttpRequests(auth -> auth .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 .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 .anyRequest().authenticated() // tat ca api con lai deu require login
) )

View File

@@ -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";
}
}

View File

@@ -6,4 +6,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/26_05_db?createDatabaseIfNotEx
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password= spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
spring.servlet.multipart.max-file-size=10MB

View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>Upload Data</h2>
<form th:action="@{/uploads}" method="post" enctype="multipart/form-data">
<input type="file" name="avatar">
<button>Upload</button>
</form>
</body>
</html>