<form action="vipForm.do" method="POST" enctype="multipart/form-data">
★ 중요!
method는 POST로
enctype은 multipart/form-data
<tr>
<td align=center>사진첨부</td>
<td><input type=file name=uploadfile size=10> </td>
</tr>
input type은 file
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000" />
</bean>
[ MultipartFile 인터페이스가 제공하는 주요 메서드 ]
- 업로드한 파일명을 문자열로 리턴
: String getOriginalFilename()- 업로드한 파일을 desfFile에 저장
: void transferTo(File destFile)- 업로드한 파일 존재 여부 리턴 (없으면 true 리턴)
: boolean isEmpty()
CREATE TABLE member_vip (
custno NUMBER(6) NOT NULL PRIMARY KEY ,
custname VARCHAR2(20),
phone VARCHAR2(13),
address VARCHAR2(60),
joindate DATE,
grade CHAR(1),
city CHAR(2),
fname NVARCHAR2(50)
);
INSERT INTO member_vip
VALUES(900001,'김귀빈','010-9999-9999','서울 동대문구 휘경1동','20151202','A','01','space.jpg');
SELECT *
FROM member_vip;
package com.springbook.view;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import com.springbook.biz.jungbo.common.MemberVO;
import com.springbook.biz.jungbo.service.JungboService;
@Controller
public class MemberVIPController {
@Autowired
JungboService s;
@RequestMapping(value="/vipForm.do", method=RequestMethod.POST)
public String file_form(MemberVO vo , HttpServletRequest request) {
String Realpath = request.getSession().getServletContext().getRealPath("/psd/");
System.out.println(Realpath);
MultipartFile uploadFile = vo.getUploadFile();
if(!uploadFile.isEmpty()) {
try {
String genId = UUID.randomUUID().toString();
String originalFileName = uploadFile.getOriginalFilename();
String onlyFile = originalFileName.substring(0, originalFileName.lastIndexOf("."));
String extention = FilenameUtils.getExtension(originalFileName);
String saveFileName = onlyFile + genId.substring(0,3) + "." + extention;
vo.setFilename(saveFileName);
uploadFile.transferTo(new File(Realpath+saveFileName));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
vo.setFilename("empty.jpg");
}
s.insertfile(vo);
return "";
}
}
@Override
public void insertfile(MemberVO vo) {
dao.insertfile(vo);
}
String insert_file_sql="insert into member_vip (custno, custname, phone, address, joindate, grade, city, fname) values ((select max(custno) from member_vip)+1, ?, ?, ?, ?, ?, ?, ?) ";
@Override
public void insertfile(MemberVO vo) {
Object[] args = {vo.getCustname(), vo.getPhone(),vo.getAddress(), vo.getJoindate(), vo.getGrade(), vo.getCity(), vo.getFilename()};
jdbcTemplate.update(insert_file_sql, args);
}
class rowMapper_file implements RowMapper<MemberVO>{
@Override
public MemberVO mapRow(ResultSet rs, int rowNum) throws SQLException {
MemberVO vo = new MemberVO();
vo.setCustno(rs.getInt("custno"));
vo.setCustname(rs.getString("custname"));
vo.setPhone(rs.getString("phone"));
vo.setAddress(rs.getString("address"));
vo.setJoindate(rs.getString("joindate"));
vo.setGrade(rs.getString("grade"));
vo.setCity(rs.getString("city"));
vo.setFilename(rs.getString("fname"));
return vo;
}
}
또 다른 방법으로 flag를 사용할 수도 있는데,
전역변수 flag를 설정해서 if문에 문자열비교를 통해 rs.fname의 존재 유무를 설정한다.
다른부분은 일반 목록보기와 동일하다.
다만, 사진을 불러오려면 img src 태그를 사용해야한다.
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:import url="header.jsp" />
<section>
<br><br>
<div align="center"><font size=5> VIP회원목록보기 </font></div> <br>
<div align="center">
<br><br>
<table border=1 width=800 height=250>
<tr><td>회원번호</td><td>회원성명</td> <td>전화번호</td>
<td>주소</td><td>가입일자</td><td>고객등급</td><td>거주지역</td><td>사진</td></tr>
<c:forEach items="${list}" var="m">
<tr>
<td><a href="delete.do?custno=${m.getCustno()}">${m.getCustno()}</a></td>
<td><a href="edit.do?custno=${m.getCustno()}">${m.getCustname()}</a></td>
<td>${m.getPhone()}</td>
<td>${m.getAddress()}</td>
<td>${m.getJoindate()}</td>
<td>
<c:if test="${m.getGrade() == 'A'}"> VIP </c:if>
<c:if test="${m.getGrade() == 'B'}"> 일반 </c:if>
<c:if test="${m.getGrade() == 'C'}"> 직원 </c:if>
</td>
<td>${m.getCity()}</td>
<td><img src="./psd/${m.getFilename()}" width="50" height="50"></td>
</tr>
</c:forEach>
</table>
</div>
</section>
<c:import url="footer.jsp" />
@RequestMapping(value="/vip_list.do")
public String file_list(MemberVO vo, Model model) {
model.addAttribute("list", s.fileAllmember(vo));
return "vip_list.jsp";
}
@Override
public List<MemberVO> fileAllmember(MemberVO vo) {
return dao.fileAllmember(vo);
}
String select_file_sql="select * from member_vip order by custno desc ";
@Override
public List<MemberVO> fileAllmember(MemberVO vo) {
return jdbcTemplate.query(select_file_sql, new rowMapper_file());
}
class rowMapper_file implements RowMapper<MemberVO>{
@Override
public MemberVO mapRow(ResultSet rs, int rowNum) throws SQLException {
MemberVO vo = new MemberVO();
vo.setCustno(rs.getInt("custno"));
vo.setCustname(rs.getString("custname"));
vo.setPhone(rs.getString("phone"));
vo.setAddress(rs.getString("address"));
vo.setJoindate(rs.getString("joindate"));
vo.setGrade(rs.getString("grade"));
vo.setCity(rs.getString("city"));
vo.setFilename(rs.getString("fname"));
return vo;
}
}