티스토리 뷰
이전 게시글에서 이어집니다.
https://rla5378.tistory.com/47
<body>
<h1>파일 업로드</h1>
<form action="${contextPath }/upload" method="post"
enctype="multipart/form-data">
<!-- 파일 업로드때는 무조건 post, enctype은 무조건 multipart/form-data이어야함 -->
<!-- multipart/form-data쓰면 multipart로 받아야ㅑ함 -->
<input type="text" name="id" placeholder="id"><br>
<input type="text" name="name" placeholder="name"><br>
<input type="file" name="file"><br>
<input type="submit" name="업로드"><br>
</form>
<hr>
<a href="${contextPath }/views">파일보기</a>
</body>
view에 파일보기 경로를 추가했다.
1.컨트롤러
@GetMapping("views")
public String views(Model model) {
fs.getShoesImage(model);
return "result";
}
2.서비스
FileService.java
public interface FileService {
public static final String IMAGE_REPO ="C:/spring/image_repo";
void fileProcess(MultipartHttpServletRequest mul);
void getShoesImage(Model model);
}
FileServiceImpl.java
@Override
public void getShoesImage(Model model) {
model.addAttribute("list", fm.getShoesImage());
}
3.FileMapper(dao)
public interface FileMapper {
void saveData(ShoesDTO dto);
List<ShoesDTO> getShoesImage();
}
4.맵퍼에 select문 입력
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.care.root.mybatis.FileMapper">
<resultMap type="com.care.root.dto.ShoesDTO" id="shoes">
<id property="id" column="id"/><!-- 기본키는 보통 id태그를 씀 -->
<result property="name" column="name"/>
<result property="imgName" column="img_name"/>
</resultMap>
<select id="getShoesImage" resultMap="shoes">
select * from shoes_image
</select>
</mapper>
*select태그를 쓸때는 resultMap태그를 함께 써야한다!
5.views만들기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="contextPath" value="${pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach var="dto" items="${list }">
id : ${dto.id }<br>
name : ${dto.name }<br>
image name : ${dto.imgName }<hr>
<img src="${contextPath }/download?file=${dto.imgName}" width="100px" height="100px">
<a href="${contextPath }/download?file=${dto.imgName}">${dto.imgName }-클릭 시 다운</a>
<hr>
</c:forEach>
<a href="${contextPath }/form">다시 업로드 이동</a>
</body>
</html>
데이터베이스에 저장된 파일명을 가지고 파일(이미지)를 가져올 것이다.
그러기 위해서는 download를 컨트롤러에 추가해줘야한다.
${contextPath }/download?file=${dto.imgName} |
==> download로 가는데 file=dto에 저장되어있던 imgName까지 같이 넘겨주겠다는 것이다.
6.컨트롤러에 download경로를 추가
@Controller
public class FileDownloadController {
@GetMapping("download")
public void download(@RequestParam("file") String fileName, HttpServletResponse response) throws Exception {
response.addHeader("Content-disposition", "attachment; fileName=" + fileName);
//변수명, 값
File file = new File(FileService.IMAGE_REPO + "/" + fileName);
FileInputStream in = new FileInputStream(file);
FileCopyUtils.copy(in, response.getOutputStream());
}
}
Content-disposition : 파일을 다운로드하겠다는 의미 attachment : 파일을 다운로드하여 브라우저로 표현하겠다. response.addHeader(String name, String value)형태이다. |
FileInputStream in = new FileInputStream(file);
InputStream 은 해당하는 경로에서 데이터를 가져오겠습니다라는 의미이다.
*파일을 보낼때는 아웃풋 스트림/ 파일을 갖고올때는 인풋스트림
그러니 지금은 in -> out ==>사용자 순으로 써야한다
해당 저장소에 데이터를 가져오겠습니다:인풋스트림
사용자에게 데이터를 보낼때:아웃풋스트림
FileCopyUtils를 통해서 바로 사용자에게 데이터를 보낼 수 있다.
FileCopyUtils.copy(in, response.getOutputStream()); |
왼쪽에 있는 데이터를 오른쪽으로 보내주라는 의미이다.
'Spring' 카테고리의 다른 글
[스프링]이메일 보내기(2-이메일 인증) (0) | 2021.11.16 |
---|---|
[스프링]이메일 보내기 (1) (0) | 2021.11.16 |
[스프링]파일 업로드 (0) | 2021.11.15 |
스프링_@RequestParam 간단하게 사용해보기 (0) | 2021.05.25 |
스프링_Annotation(@RequestMapping,@GetMapping,@PostMapping)을 이용하여 더 짧고 간단하게 get,Post방식으로 보내기 (0) | 2021.05.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Margin
- RequestDispatcher
- getParameter
- this.
- link href
- 실기
- jsp
- CLASS
- pageContext
- Redirect
- session
- request
- span
- id
- Declaration
- Expression
- 합격
- 자격증
- 정보산업처리기사
- div
- application
- padding
- 독학
- dl
- 정처기
- NAV
- 정처산기
- scriptlet
- forward
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함