2021-08-02
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class MyController {
|
||||
@Autowired
|
||||
private StorageService storageService;
|
||||
|
||||
@GetMapping(path = {"/"})
|
||||
public String index(@NotNull Model model) {
|
||||
model.addAttribute("files", storageService.listFiles());
|
||||
return "main";
|
||||
}
|
||||
|
||||
/**
|
||||
* 파일 업로드
|
||||
* @param file
|
||||
* @param redirectAttributes
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(path = {"/upload"})
|
||||
public String upload(final @RequestParam("file") MultipartFile file,
|
||||
final @NotNull RedirectAttributes redirectAttributes) {
|
||||
storageService.store(file);
|
||||
|
||||
// 업로드 후 새로 고침
|
||||
redirectAttributes.addFlashAttribute("message",
|
||||
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
/**
|
||||
* 파일 다운로드
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/files/{filename:.+}")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
|
||||
|
||||
final Resource file = storageService.loadAsResource(filename);
|
||||
|
||||
final ContentDisposition contentDisposition = ContentDisposition.attachment()
|
||||
.filename(filename)
|
||||
.build();
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
contentDisposition.toString())
|
||||
.body(file);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class StorageService {
|
||||
/*
|
||||
테스트용 임시 파일 저장소
|
||||
실제로는 파일시스템에 저장하거나 DB에 저장해야 한다.
|
||||
*/
|
||||
private final Map<String, byte[]> storage = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 저장된 파일 목록
|
||||
* @return
|
||||
*/
|
||||
Set<String> listFiles(){
|
||||
return storage.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 파일을 저장
|
||||
* @param file
|
||||
*/
|
||||
void store(final @NotNull MultipartFile file) {
|
||||
try {
|
||||
storage.put(file.getOriginalFilename(), file.getBytes());
|
||||
log.info("File Uploaded... {}", file.getOriginalFilename());
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("Unable to save a file.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 저장된 파일을 불러옴
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
Resource loadAsResource(final String filename) {
|
||||
//return new FileSystemResource(new File(filename));
|
||||
return new ByteArrayResource(storage.get(filename));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
9
file-upload/src/main/resources/application.yaml
Normal file
9
file-upload/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
application:
|
||||
name: My spring-boot project
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 128KB
|
||||
max-request-size: 128KB
|
||||
server:
|
||||
port: 8080
|
||||
10
file-upload/src/main/resources/banner.txt
Normal file
10
file-upload/src/main/resources/banner.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
('-. ('-. ) (`-.
|
||||
_( OO) _( OO) ( OO ).
|
||||
(,------.,--. (,------.(_/. \_)-.
|
||||
| .---'| |.-') | .---' \ `.' /
|
||||
| | | | OO ) | | \ /\
|
||||
(| '--. | |`-' |(| '--. \ \ |
|
||||
| .--'(| '---.' | .--' .' \_)
|
||||
| `---.| | | `---. / .'. \
|
||||
`------'`------' `------''--' '--'
|
||||
powered by ELEX
|
||||
48
file-upload/src/main/resources/logback-spring.xml
Normal file
48
file-upload/src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
~ Spring-boot Examples
|
||||
~
|
||||
~ Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
~ https://www.elex-project.com/
|
||||
-->
|
||||
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
|
||||
|
||||
<springProperty name="LOG_DIR" source="logging.path"
|
||||
defaultValue="${user.home}/logs"/>
|
||||
<property name="LOG_PATH" value="${LOG_DIR}/stephanie.log"/>
|
||||
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<charset>UTF-8</charset>
|
||||
<pattern>${FILE_LOG_PATTERN}</pattern>
|
||||
</encoder>
|
||||
<file>${LOG_PATH}</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_DIR}/sebastian_%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<logger name="kr.pe.elex" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="ROLLING-FILE"/>
|
||||
</logger>
|
||||
<root level="info">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="ROLLING-FILE"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
15
file-upload/src/main/resources/templates/main.mustache
Normal file
15
file-upload/src/main/resources/templates/main.mustache
Normal file
@@ -0,0 +1,15 @@
|
||||
<h1>File Upload Example!!!</h1>
|
||||
<form action="/upload" method="POST" enctype="multipart/form-data">
|
||||
<label for="file">Select a file: </label>
|
||||
<input id="file" name="file" type="file" title="Upload"/>
|
||||
<div>
|
||||
<button>Upload</button>
|
||||
</div>
|
||||
</form>
|
||||
<h2>Uploaded Files</h2>
|
||||
<p>Click to download.</p>
|
||||
<ul>
|
||||
{{#files}}
|
||||
<li><a href="/files/{{.}}">{{.}}</a></li>
|
||||
{{/files}}
|
||||
</ul>
|
||||
Reference in New Issue
Block a user