196 lines
6.2 KiB
Java
196 lines
6.2 KiB
Java
package com.example.demo.controller;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
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.RequestPart;
|
|
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;
|
|
|
|
@Value("server.servlet.context-path")
|
|
private String CONTEXT_PATH;
|
|
|
|
@PostMapping("/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> postUserProfile(@PathVariable("id") String id, @RequestParam("name") String name,
|
|
@RequestParam("phone") String phone, @RequestParam("address") String address,
|
|
@RequestPart 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 + " folder create OK");
|
|
} catch (Exception e) {
|
|
e.getStackTrace();
|
|
}
|
|
} else {
|
|
System.out.println("A folder has already been created");
|
|
}
|
|
|
|
File file = new File(saveFullFilePath);
|
|
System.out.println("saved 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타입으로 반환해서 전달한다.
|
|
}
|
|
|
|
@PutMapping("/user/{id}")
|
|
public void putUserProfile(@PathVariable("id") String id, @RequestParam("name") String name,
|
|
@RequestParam("phone") String phone, @RequestParam("address") String address,
|
|
@RequestPart 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 + " folder create OK");
|
|
} catch (Exception e) {
|
|
e.getStackTrace();
|
|
}
|
|
} else {
|
|
System.out.println("A folder has already been created");
|
|
}
|
|
|
|
File file = new File(saveFullFilePath);
|
|
System.out.println("saved 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.updateUserProfile(id, name, phone, address, dbSavePath);
|
|
}
|
|
|
|
@DeleteMapping("/user/{id}")
|
|
public void deleteUserProfile(@PathVariable String id) {
|
|
mapper.deleteUserProfile(id);
|
|
}
|
|
}
|