package
or.kosta.mvc.controller;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.util.List;
import
javax.servlet.ServletContext;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.HttpSession;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.ui.Model;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.servlet.ModelAndView;
import
or.kosta.mvc.dao.TvoDao;
import
or.kosta.vo.TBoardDTO;
@Controller
public
class
TBoardController {
@Autowired
private
TvoDao tvoDao;
@RequestMapping
(value =
"/form"
)
public
String form() {
return
"form"
;
}
@RequestMapping
(value =
"/tBoardProcess"
, method = RequestMethod.POST)
public
ModelAndView save(TBoardDTO tvo, HttpServletRequest request) {
HttpSession session = request.getSession();
String r_path = session.getServletContext().getRealPath(
"/"
);
System.out.println(r_path);
String img_path =
"resources\\img\\"
;
System.out.println(img_path);
StringBuffer path =
new
StringBuffer();
path.append(r_path).append(img_path);
String oriFn = tvo.getMfile1().getOriginalFilename();
path.append(oriFn);
System.out.println(path);
File f =
new
File(path.toString());
try
{
tvo.getMfile1().transferTo(f);
}
catch
(IllegalStateException | IOException e) {
e.printStackTrace();
}
tvo.setMfile(oriFn);
tvoDao.add(tvo);
ModelAndView mav =
new
ModelAndView(
"redirect:/list"
);
return
mav;
}
@RequestMapping
(value =
"/list"
, method = RequestMethod.GET)
public
ModelAndView list() {
ModelAndView mav =
new
ModelAndView();
mav.setViewName(
"list"
);
List<TBoardDTO> listvo = tvoDao.getList();
mav.addObject(
"list"
, listvo);
return
mav;
}
private
static
final
int
BUFFER_SIZE =
4096
;
@RequestMapping
(value =
"/fileDown"
)
public
void
fileDown(
@RequestParam
(
"fileName"
) String fileName, HttpSession session, HttpServletRequest request,
HttpServletResponse response)
throws
IOException {
ServletContext context = request.getServletContext();
String img_path =
"resource\\img\\"
;
String path = session.getServletContext().getRealPath(
"resources/img/"
) + fileName;
System.out.println(
"FileDown:"
+ path);
File downloadFile =
new
File(path);
FileInputStream fi =
new
FileInputStream(downloadFile);
String mimeType = context.getMimeType(path);
if
(mimeType ==
null
) {
mimeType =
"application/octet-stream"
;
}
response.setContentType(mimeType);
response.setContentLength((
int
) downloadFile.length());
String headerKey =
"Content-Disposition"
;
String headerValue = String.format(
"attachment; filename=\"%s\""
, downloadFile.getName());
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
byte
[] buffer =
new
byte
[BUFFER_SIZE];
int
bytesRead = -
1
;
while
((bytesRead = fi.read(buffer)) != -
1
) {
outStream.write(buffer,
0
, bytesRead);
}
fi.close();
outStream.close();
}
@RequestMapping
(value =
"/detail"
)
public
String detail(
int
num, Model m) {
TBoardDTO vo = tvoDao.getDetail(num);
m.addAttribute(
"vo"
, vo);
return
"detail"
;
}
@RequestMapping
(value =
"/update"
)
public
String updateForm(
int
num, Model m) {
TBoardDTO vo = tvoDao.getDetail(num);
m.addAttribute(
"vo"
, vo);
return
"updateForm"
;
}
@RequestMapping
(value =
"/updateProcess"
, method = RequestMethod.POST)
public
ModelAndView updateProcess(TBoardDTO tvo, HttpServletRequest request) {
if
(tvo.getMfile1().isEmpty() ==
false
) {
HttpSession session = request.getSession();
String r_path = session.getServletContext().getRealPath(
"/"
);
System.out.println(r_path);
String img_path =
"resources\\img\\"
;
System.out.println(img_path);
StringBuffer path =
new
StringBuffer();
path.append(r_path).append(img_path);
String oriFn = tvo.getMfile1().getOriginalFilename();
path.append(oriFn);
System.out.println(path);
File f =
new
File(path.toString());
try
{
tvo.getMfile1().transferTo(f);
}
catch
(IllegalStateException | IOException e) {
e.printStackTrace();
}
tvo.setMfile(oriFn);
}
else
{
tvo.setMfile(tvo.getMfile());
}
tvoDao.update(tvo);
ModelAndView mav =
new
ModelAndView(
"redirect:/list"
);
return
mav;
}
@RequestMapping
(value =
"/delete"
)
public
String delete(
int
num) {
tvoDao.delete(num);
return
"redirect:/list"
;
}
}