250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Spring Boot
- Eclipse
- JavaScript
- git
- Exception
- jpa
- 설정
- 오픈소스
- maven
- Open Source
- Thymeleaf
- Core Java
- IntelliJ
- ubuntu
- oracle
- Tomcat
- MSSQL
- MySQL
- 문서
- AJAX
- SpringBoot
- spring
- Source
- STS
- error
- myBatis
- Python
- Docker
- JDBC
- PostgreSQL
Archives
- Today
- Total
헤르메스 LIFE
[Spring] 개발환경 구축 - 이미지 파일 뷰 본문
728x90
Spring 개발환경을 갑자기 구축하려고 하다보니, 막막합니다.
하나 하나 구축해 보겠습니다
개발환경
Spring 4.3.30.RELEASE
MAVEN 3.8.4
Logback 1.2.9
commons-fileupload 1.4
commons-io 2.6
commons-dbcp 1.4
postgresql driver 42.5.0
poi 4.1.2
ImageViewerController.java
package simple.spring.file;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.annotation.Resource;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class ImageViewerController {
private static final Logger logger = LoggerFactory.getLogger(ImageViewerController.class);
@Resource(name = "uploadPath") // 아이디로 받음 (서블릿 beans)
private String UPLOAD_LOCATION;
@RequestMapping(value = "/displayFile", method = RequestMethod.GET)
public ResponseEntity<byte[]> displayFile() {
ResponseEntity<byte[]> entity = null;
String filename = "홍길동.jpg";
try {
String formatName = filename.substring(filename.lastIndexOf(".") + 1);
org.springframework.http.MediaType type = null;
if ( formatName.equalsIgnoreCase("jpg") ) {
type = org.springframework.http.MediaType.IMAGE_JPEG;
} else if ( formatName.equalsIgnoreCase("png") ) {
type = org.springframework.http.MediaType.IMAGE_PNG;
} else if ( formatName.equalsIgnoreCase("gif") ) {
type = org.springframework.http.MediaType.IMAGE_GIF;
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(type);
// 파일을 읽어들임
InputStream in = new FileInputStream(UPLOAD_LOCATION + "/" + filename);
entity = new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
in.close(); // inputstream은 사용 완료 후 close 해줘야 다른 곳에서 파일 접근 가능
} catch (Exception e) {
}
return entity;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Tutorial</title>
</head>
<body>
<ul>
<li>HTML, JSP, Servlet
<ul>
<li><a href='/hello'>Hello World</a></li>
</ul>
</li>
<li>File Upload
<ul>
<li><a href='/singleUpload'>Single Upload</a></li>
<li><a href='/multiUpload'>Multi File Upload</a></li>
</ul>
</li>
<li>File Download
<ul>
<li><a href='/fileDownload'>직접 File Download</a></li>
<li><a href='/fileDownload2'>View 를 통한 File Download</a></li>
</ul>
</li>
<li>DBCP
<ul>
<li><a href='/dbcpConnectionTest'>DBCP Connection Test</a></li>
</ul>
</li>
<li>Excel
<ul>
<li><a href='/excelDownload'>Excel Download</a></li>
</ul>
</li>
<li>Image
<ul>
<li><a href='/displayFile'>Image Viewer</a></li>
</ul>
</li>
</ul>
</body>
</html>
홍길동.jpg
https://hermeslog.tistory.com/652
https://hermeslog.tistory.com/655
728x90
'Spring Framework' 카테고리의 다른 글
[전자정부프레임워크] 전자정부프레임워크 설치 (0) | 2023.06.22 |
---|---|
[개발환경] Spring + Maven + MyBatis + PostgreSQL Upgrade (0) | 2023.05.23 |
[Spring] 개발환경 구축 - 대용량 Excel 다운로드 (0) | 2023.01.19 |
[Spring] 개발환경 구축 - DBCP Connection Test (0) | 2022.12.29 |
[Spring] 개발환경 구축 - File Download (0) | 2022.12.28 |