스프링11_파일2 갤러리

charl hi·2022년 1월 26일
0

Spring

목록 보기
12/25
post-thumbnail

VO

GalleryVo

public class GalleryVo {

	private int no;
	private String title;
	private String content;
	private String fName;
	private long fSize;
	private String fType;
	private Date enrollDate;
	
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getfName() {
		return fName;
	}
	public void setfName(String fName) {
		this.fName = fName;
	}
	public long getfSize() {
		return fSize;
	}
	public void setfSize(long fSize) {
		this.fSize = fSize;
	}
	public String getfType() {
		return fType;
	}
	public void setfType(String fType) {
		this.fType = fType;
	}
	public Date getEnrollDate() {
		return enrollDate;
	}
	public void setEnrollDate(Date enrollDate) {
		this.enrollDate = enrollDate;
	}
	
	@Override
	public String toString() {
		return "GalleryVo [no=" + no + ", title=" + title + ", content=" + content + ", fName=" + fName + ", fSize="
				+ fSize + ", fType=" + fType + ", enrollDate=" + enrollDate + "]";
	}
	
}



mybatis

<mapper namespace="gallery">

	<select id="getSeq" resultType="int">
		SELECT GALLERY_SEQ.NEXTVAL FROM DUAL
	</select>
 	
 	<insert id="upload" >
 		INSERT INTO GALLERY
 		VALUES(#{no}, #{title}, #{content}, #{fName}, #{fSize}, #{fType}, SYSDATE)
 	</insert>
 	
 	<!-- <resultMap type="galleryVo" id="gVo">
 		<result column="NO" property="no"/>
 		<result column="TITLE" property="title"/>
 		<result column="CONTENT" property="content"/>
 		<result column="FNAME" property="fName"/>
 		<result column="FSIZE" property="fSize"/>
 		<result column="FTYPE" property="fType"/>
 		<result column="ENROLL_DATE" property="enrollDate"/>
 	</resultMap> -->
	
</mapper>



Controller, Repository, Service

GalleryRepository

public interface GalleryRepository {

	//파일번호 가져오기
	int getSeq();
	
	//디비에 VO 저장
	int upload(GalleryVo g);
}


GalleryRepositoryImpl

@Repository
public class GalleryRepositoryImpl implements GalleryRepository {

	@Autowired
	SqlSession sqlSession;
	
	@Override
	public int getSeq() {
		return sqlSession.selectOne("gallery.getSeq");
	}

	@Override
	public int upload(GalleryVo g) {
		return sqlSession.insert("gallery.upload", g);
	}

}


GalleryService

public interface GalleryService {

	int upload(GalleryVo g, MultipartFile f) throws IllegalStateException, IOException;
}


GalleryServiceImpl

@Service
public class GalleryServiceImpl implements GalleryService {
	@Autowired
	private GalleryRepository galleryRepository;

	@Override
	public int upload(GalleryVo g, MultipartFile f) throws IllegalStateException, IOException {
		
		//f로 받은 걸 -> fName, fSize, fType 에 넣기 
		g.setfName(f.getOriginalFilename());
		g.setfSize(f.getSize());
		g.setfType(f.getContentType());
		
//		Repository
		//mapper에 있는 getSeq로 -> no 에 넣기
		int no = galleryRepository.getSeq();
		g.setNo(no);
				
		//디비에 VO 저장
		System.out.println(g);
		int result = galleryRepository.upload(g);
				
		//서버(내컴퓨터)에 저장
		//file 만들기
		File file = new File("D:\\uploadForSpring", String.valueOf(no));
				
		//file에 옮기기
		f.transferTo(file);		
		
		return result;
	}

}


GalleryController

@Controller
@RequestMapping("gallery")
public class GalleryController {
	
	@Autowired
	private SqlSession sqlSession;
	
	@Autowired
	private GalleryService galleryService;

	@GetMapping("upload")
	public String upload() {
		return "gallery/upload";
	}
	
	@PostMapping("upload")
	public String upload(GalleryVo g, MultipartFile f) throws IllegalStateException, IOException {
//		System.out.println(g.getTitle());
//		System.out.println(g.getContent());
//		System.out.println(f.getOriginalFilename());
		
		//파일 없으면
		if(f.isEmpty()) {
			return "redirect:/error404";
		}
		
//		Service
		int result = galleryService.upload(g, f);
		if(result > 0) {
			return "redirect:/gallery/upload";
		}else {
			return "redirect:/error404";
		}
		
	}
}




upload.jsp

<body>
	<h1>갤러리 업로드 페이지</h1>
	
	<form action="" method="post" enctype="multipart/form-data">
		제목 : <input type="text" name="title"><br>
		내용 : <input type="text" name="content"><br>
		파일 : <input type="file" name="f" multiple accept=".jpg, .png"><br>
		<input type="submit" value="등록">
	</form>
</body>

0개의 댓글