첫 커밋
This commit is contained in:
19
src/main/java/com/example/demo/DemoApplication.java
Normal file
19
src/main/java/com/example/demo/DemoApplication.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication extends SpringBootServletInitializer {
|
||||
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(DemoApplication.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
18
src/main/java/com/example/demo/config/WebMvcConfig.java
Normal file
18
src/main/java/com/example/demo/config/WebMvcConfig.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.example.demo.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
MultipartResolver configureMultipart() {
|
||||
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
|
||||
resolver.setMaxUploadSize(500000000);
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.example.demo.mapper.UserProfileMapper;
|
||||
import com.example.demo.model.UserProfile;
|
||||
|
||||
@RestController
|
||||
public class UserProfileController {
|
||||
|
||||
private UserProfileMapper mapper;
|
||||
|
||||
public UserProfileController(UserProfileMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@GetMapping("/user/{id}")
|
||||
public UserProfile getUserProfile(@PathVariable String id) {
|
||||
return mapper.getUserProfile(id);
|
||||
}
|
||||
|
||||
@GetMapping("/user/all")
|
||||
public List<UserProfile> getUserProfileList() {
|
||||
return mapper.getUserProfileList();
|
||||
}
|
||||
|
||||
@Value("${server.url.img}")
|
||||
private String SERVER_URL_IMG;
|
||||
|
||||
@PutMapping("/user/{id}")
|
||||
// public HashMap<String, Object> putUserProfile(@PathVariable String id, @RequestParam String name,
|
||||
// public void putUserProfile(@PathVariable String id, @RequestParam String name,
|
||||
public List<UserProfile> putUserProfile(@PathVariable String id, @RequestParam String name,
|
||||
@RequestParam String phone, @RequestParam String address,
|
||||
@RequestParam MultipartFile upfile) {
|
||||
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String fileName = upfile.getOriginalFilename();
|
||||
String fileExtName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
|
||||
String contentType = upfile.getContentType();
|
||||
long fileLength = upfile.getSize();
|
||||
|
||||
System.out.println("fileLength : " + fileLength);
|
||||
System.out.println("fileName : " + fileName);
|
||||
System.out.println("fileExtName : " + fileExtName);
|
||||
System.out.println("contentType : " + contentType);
|
||||
|
||||
byte[] data = null;
|
||||
try {
|
||||
data = upfile.getBytes();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String baseFolder = SERVER_URL_IMG;
|
||||
String baseMiddleFolder = "/img";
|
||||
String saveFolder = baseFolder + baseMiddleFolder;
|
||||
String dbSavePath = baseMiddleFolder + "/" + uuid + fileExtName;
|
||||
String saveFullFilePath = saveFolder + "/" + uuid + fileExtName;
|
||||
|
||||
// 입력된 내용 파일로 쓰기
|
||||
File folder = new File(saveFolder);
|
||||
// 해당 디렉토리가 없을경우 디렉토리를 생성합니다.
|
||||
if (!folder.exists()) {
|
||||
try {
|
||||
folder.mkdir(); // 폴더 생성합니다.
|
||||
System.out.println(saveFolder + " SUCCESS");
|
||||
} catch (Exception e) {
|
||||
e.getStackTrace();
|
||||
}
|
||||
} else {
|
||||
System.out.println("A folder has already been created");
|
||||
}
|
||||
|
||||
File file = new File(saveFullFilePath);
|
||||
System.out.println("saved temp dir excel download file : " + saveFullFilePath);
|
||||
FileOutputStream fos = null;
|
||||
|
||||
try {
|
||||
fos = new FileOutputStream(file);
|
||||
fos.write(data);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (fos != null)
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
mapper.insertUserProfile(id, name, phone, address, dbSavePath);
|
||||
|
||||
return mapper.getUserProfileList(); //스프링이 자동으로 JSON타입으로 반환해서 전달한다.
|
||||
|
||||
// HashMap<String,Object> map = new HashMap<String,Object>();
|
||||
// map.put("age", 24);
|
||||
// map.put("name", "mangdo");
|
||||
//
|
||||
// return map; //스프링이 자동으로 JSON타입으로 반환해서 전달한다.
|
||||
}
|
||||
|
||||
@PostMapping("/user/{id}")
|
||||
public void postUserProfile(@PathVariable String id, @RequestParam String name,
|
||||
@RequestParam String phone, @RequestParam String address, @RequestParam String filepath) {
|
||||
mapper.updateUserProfile(id, name, phone, address, filepath);
|
||||
}
|
||||
|
||||
@DeleteMapping("/user/{id}")
|
||||
public void deleteUserProfile(@PathVariable String id) {
|
||||
mapper.deleteUserProfile(id);
|
||||
}
|
||||
}
|
||||
33
src/main/java/com/example/demo/mapper/UserProfileMapper.java
Normal file
33
src/main/java/com/example/demo/mapper/UserProfileMapper.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.example.demo.mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.example.demo.model.UserProfile;
|
||||
|
||||
@Mapper
|
||||
public interface UserProfileMapper {
|
||||
@Select("SELECT * FROM UserProfile WHERE id=#{id}")
|
||||
UserProfile getUserProfile(@RequestParam String id);
|
||||
|
||||
@Select("SELECT * FROM UserProfile")
|
||||
List<UserProfile> getUserProfileList();
|
||||
|
||||
@Insert("INSERT INTO UserProfile VALUES( #{id}, #{name}, #{phone}, #{address}, #{filepath} )")
|
||||
int insertUserProfile(@RequestParam String id, @RequestParam String name,
|
||||
@RequestParam String phone, @RequestParam String address, @RequestParam String filepath );
|
||||
|
||||
@Update("UPDATE UserProfile SET name=#{name}, phone=#{phone}, address=#{address}, filepath=#{filepath} WHERE id=#{id}")
|
||||
int updateUserProfile(@RequestParam String id, @RequestParam String name,
|
||||
@RequestParam String phone, @RequestParam String address, @RequestParam String filepath);
|
||||
|
||||
@Delete("DELETE FROM UserProfile WHERE id=#{id}")
|
||||
int deleteUserProfile(@RequestParam String id);
|
||||
}
|
||||
58
src/main/java/com/example/demo/model/UserProfile.java
Normal file
58
src/main/java/com/example/demo/model/UserProfile.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package com.example.demo.model;
|
||||
|
||||
public class UserProfile {
|
||||
private String id;
|
||||
private String name;
|
||||
private String phone;
|
||||
private String address;
|
||||
private String filepath;
|
||||
|
||||
public UserProfile(String id, String name, String phone, String address, String filepath) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.phone = phone;
|
||||
this.address = address;
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getFilepath() {
|
||||
return filepath;
|
||||
}
|
||||
|
||||
public void setFilepath(String filepath) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
}
|
||||
18
src/main/resources/application.properties
Normal file
18
src/main/resources/application.properties
Normal file
@@ -0,0 +1,18 @@
|
||||
# DB
|
||||
spring.datasource.url=jdbc:mysql://leejk0523.com:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=Ghtkssk0325
|
||||
|
||||
# Encoding
|
||||
server.servlet.encoding.charset=UTF-8
|
||||
server.servlet.encoding.enabled=true
|
||||
server.servlet.encoding.force=true
|
||||
# Multipoart
|
||||
spring.servlet.multipart.max-file-size=500MB
|
||||
spring.servlet.multipart.max-request-size=500MB
|
||||
spring.servlet.multipart.enabled=true
|
||||
spring.servlet.multipart.location=${java.io.tmpdir}
|
||||
|
||||
# Image save folder
|
||||
#server.url.img=/Users/daniellee/Downloads
|
||||
server.url.img=/usr/local/soft/tomcat/webapps/profile
|
||||
13
src/test/java/com/example/demo/DemoApplicationTests.java
Normal file
13
src/test/java/com/example/demo/DemoApplicationTests.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user