Today I Learned
FileUpload
- ํ์๊ฐ์ ์ ์ฒจ๋ถํ ์ด๋ฏธ์งํ์ผ์ Mypage์์ ์ถ๋ ฅ๋จ
- MemberDAO
public int addFile(MemberFileDTO memberFileDTO) throws Exception {
return sqlSession.insert(NAMESPACE + "addFile", memberFileDTO);
}
- MemberFileDTO
package com.spring.s1.member;
public class MemberFileDTO {
private Long fileNum;
private String id;
private String fileName;
private String oriName;
public Long getFileNum() {
return fileNum;
}
public void setFileNum(Long fileNum) {
this.fileNum = fileNum;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getOriName() {
return oriName;
}
public void setOriName(String oriName) {
this.oriName = oriName;
}
}
- memberMapper
<resultMap type="MemberDTO" id="mypageResult">
<id column="id" property="id"/> <!-- PK -->
<result column="name" property="name"/> <!-- PK๋ฅผ ์ ์ธํ ๋๋จธ์ง -->
<result column="email" property="email"/>
<result column="phone" property="phone"/>
<association property="memberFileDTO" javaType="com.spring.s1.member.MemberFileDTO">
<id column="fileNum" property="fileNum"/>
<result column="fileName" property="fileName"/>
<result column="oriName" property="oriName"/>
</association> <!-- 1:1 ๊ด๊ณ -->
</resultMap>
- FileManager
package com.spring.s1.util;
import java.io.File;
import java.util.Calendar;
import java.util.UUID;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
@Component
public class FileManager {
@Autowired
private ServletContext servletContext;
public String save(MultipartFile multipartFile, String path) throws Exception {
//ํ์ผ ์ ์ฅ์ tomcat์ด ์๋๋ผ OS์์ ์ ์ฅ
//path=/resources/upload/member
String realPath = servletContext.getRealPath(path);
System.out.println(realPath);
File file = new File(realPath);
//System.out.println(file2.exists());
//System.out.println(file2.isDirectory());
if(!file.exists()) {
//file2.mkdir(); //์ค๊ฐ ํด๋๊ฐ ์์ผ๋ฉด ์๋ฌ
file.mkdirs(); //์ค๊ฐ ํด๋๊ฐ ์์ผ๋ฉด ์ค๊ฐ ํด๋๋ ์์ฑ
}
//1. ์๊ฐ
Calendar ca = Calendar.getInstance();
long l = ca.getTimeInMillis(); //ํ์ฌ ์๊ฐ์ ๋ฐ๋ฆฌ์ธ์ปจ์ฆ๋ก ๋ณํ
System.out.println("time : " + l);
String oriName = multipartFile.getOriginalFilename();
String fileName = l + "_" + oriName;
System.out.println("fileName : " + fileName);
//2. UUID
fileName = UUID.randomUUID().toString();
fileName = fileName + "_" + oriName;
System.out.println("UUID: " + fileName);
//file HDD์ ์ ์ฅ
//1. MultipartFile ํด๋์ค์ transferTo ๋ฉ์๋ ์ฌ์ฉ
//file = new File(realPath, fileName);
//file = new File(file, fileName);
//multipartFile.transferTo(file);
//2. FileCopyUtils
file = new File(file, fileName);
FileCopyUtils.copy(multipartFile.getBytes(), file);
return fileName;
}
}
- MemberService
public int join(MemberDTO memberDTO, MultipartFile photo) throws Exception {
int result = memberDAO.join(memberDTO);
//1. ํ์ผ์ HDD์ ์ ์ฅ
String fileName = fileManager.save(photo, "resources/upload/member/");
//2. ์ ๋ณด๋ฅผ DB์ ์ ์ฅ
MemberFileDTO memberFileDTO = new MemberFileDTO();
memberFileDTO.setId(memberDTO.getId());
memberFileDTO.setFileName(fileName);
memberFileDTO.setOriName(photo.getOriginalFilename());
result = memberDAO.addFile(memberFileDTO);
return result;
}
728x90