Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 카페
- Oracle
- 양동점
- ORA-01005
- ORA-01745
- SVN사용방법
- Jsp Pagination
- docker
- 은혜침구
- 디카페인
- AbstractViewe
- 맛집
- Java
- JavaScript
- 배딩작업
- 요리
- Responsively app
- Eclipse
- RefreshableSqlSessionFactoryBean
- ORA-01756
- ORA-00909
- 정민이초밥
- 나주
- 반응형앱
- egov
- 문방구과자
- 루키초밥
- 광주
- css
- mybatis
Archives
- Today
- Total
gnusraun
Egov 파일 업로드/다운로드 본문
728x90
Egov프레임워크에서 파일 업로드 및 다운로드하기
:: pom.xml
<!-- file upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- MultipartHttpServletRequset -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
:: context-common.xml
multipartResolver 빈 추가
<!-- 파일 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000" />
<property name="maxInMemorySize" value="100000000" />
</bean>
:: context-properties.xml
properties.xml 을 이용하여 실제 파일 저장 경로를 추가
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean name="propertiesService" class="egovframework.rte.fdl.property.impl.EgovPropertyServiceImpl" destroy-method="destroy">
<property name="properties">
<map>
<entry key="pageUnit" value="10"/>
<entry key="pageSize" value="10"/>
<entry key="filePath" value="C:\\temp\\"/> // 실제 저장 경로 추가
</map>
</property>
</bean>
</beans>
:: FileVO.java
file을 담을 VO 객체 추가
public class FileVO {
private String fileName;
private MultipartFile uploadFile; // 파일 정보
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public MultipartFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(MultipartFile uploadFile) {
this.uploadFile = uploadFile;
}
}
:: FileUpload.jsp
encType="multipart/form-data"와 상단 VO객체와 매핑할 input 태그 name="uploadFile" 로 지정
<form id="formAction" action="/insertFile.do" method="post" encType="multipart/form-data">
<input type="file" name="uploadFile">
</form>
:: FileController.java
properties에 지정한 경로를 사용하여 파일 업로드
// properties에서 지정한 경로
@Resource(name = "propertiesService")
EgovPropertyService propertiesService;
@RequestMapping(value = "/insertFile.do")
public String insertFile(FileVO fileVO) throws Exception {
// 파일 업로드 처리
String fileName = null;
MultipartFile uploadFile = fileVO.getUploadFile();
if (uploadFile != '' && !uploadFile.isEmpty()) {
String basePath = propertiesService.getString("filePath"); // 경로 가져오기
String originalFileName = uploadFile.getOriginalFilename();
String ext = FilenameUtils.getExtension(originalFileName); // 확장자 구하기
String uuid = UUID.randomUUID().toString(); // UUID toString으로 문자 호출
fileName = uuid + "." + ext;
uploadFile.transferTo(new File(basePath + fileName));
}
// TODO 파일 데이터를 가공해서 DB 저장하기
return "redirect:selectList.do";
}
:: FileDownloadController.java
파일명, 파일경로 지정 후 파일 다운로드
package egovframework.example.ivory.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FileDownloadController {
@Resource(name = "propertiesService")
EgovPropertyService propertiesService;
@RequestMapping(value = "/fileDownload.do")
public void fileDownload(HttpServletRequest request, HttpServletResponse response) throws Exception {
String filename = request.getParameter("fileName");
String realFilename = "";
System.out.println(filename);
try {
String browser = request.getHeader("User-Agent");
// 파일 인코딩하지 않을 경우 에러 발생
if (browser.contains("MSIE") || browser.contains("Trident") || browser.contains("Chrome")) {
filename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");
} else {
filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
}
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException 발생");
}
// 파일 경로
String basePath = propertiesService.getString("filePath");
realFilename = basePath + filename;
System.out.println(realFilename);
File file = new File(realFilename);
if (!file.exists()) {
return;
}
// 파일명 지정
response.setContentType("application/octer-stream");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
try {
OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(realFilename);
int cnt = 0;
byte[] bytes = new byte[512];
while ((cnt = fis.read(bytes)) != -1) {
os.write(bytes, 0, cnt);
}
fis.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
:: FileDownload.jsp
<h1>다운로드</h1>
<a href="/fileDownload.do?fileName=파일이름">

728x90
'Backend > Egov' 카테고리의 다른 글
Egov maven install JRE rather than a JDK? (0) | 2023.05.19 |
---|---|
Egov jsonView를 이용하여 ajax 사용하기 (0) | 2023.05.19 |
Egov Excel download (0) | 2023.05.17 |
Egov jsp 에러 페이지 표출 (0) | 2023.05.16 |
Egov org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement (0) | 2023.05.16 |