upload
This commit is contained in:
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
14
src/main/resources/templates/Upload.html
Normal file
14
src/main/resources/templates/Upload.html
Normal 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>
|
||||||
Reference in New Issue
Block a user