- ์์ฑํ ๊ฒ์๊ธ์ ์ ์ฅํฉ๋๋ค
- ํ์ผ(์ด๋ฏธ์ง)๋ฅผ ์ ํํ์ ๊ฒฝ์ฐ ํ์ผ๋ ์ ๋ก๋ ํฉ๋๋ค.
- ์ด์ ํ์ผ ์ ๋ก๋์ cos๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋๋ฐ Servlet ๋ฒ์ 3.1 ๋ถํฐ๋ StandardServletMultipartResolver ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์ด๋ฏธ์ง ์ ๋ก๋ ๋ฐฉ๋ฒ
โ
1. enctype="multipart/form-data" ๋ฅผ wirte.jsp ํผํ๊ทธ์ ์ ์ฉ
โ
2. ์คํ๋ง์์๋ StandardServletMultipartResolver ๋น๋ฑ๋ก
ํ์ฌ ์ฌ์ฉํ๊ณ ์๋ ๋ฒ์ ์ด 3.1์ด ์๋๋ผ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ ํจ ใ ใ
pom.xml
<!-- ํ์ผ ์
๋ก๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
servlet-context.xml
<!-- ํ์ผ ์
๋ก๋ -->
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="defaultEncoding" value="utf-8"></beans:property>
<!-- 1024 * 1024 * 10 bytes 10MB -->
<beans:property name="maxUploadSize" value="104857560"></beans:property>
<!-- 1024 * 1024 * 2 bytes 2MB -->
<beans:property name="maxUploadSizePerFile"
value="2097152"></beans:property>
<beans:property name="uploadTempDir"
value="file:/C:/java/upload"></beans:property>
<beans:property name="maxInMemorySize" value="10485756"></beans:property>
</beans:bean>

ํ์ผ ์์น c-> java-> uploadํด๋ ์ง์
|
https://blog.naver.com/drv982/222911772658
[๊ฒ์ํ] ์ด๋ฏธ์ง ์ ๋ก๋ ์ค์
์์ฑํ ๊ฒ์๊ธ์ ์ ์ฅํฉ๋๋ค ํ์ผ(์ด๋ฏธ์ง)๋ฅผ ์ ํํ์ ๊ฒฝ์ฐ ํ์ผ๋ ์ ๋ก๋ ํฉ๋๋ค. ์ด์ ํ์ผ ์ ๋ก๋์ cos...
blog.naver.com
.
write.jsp
enctype="multipart/form-data" ๋ฅผ wirte.jsp ํผํ๊ทธ์ ์ ์ฉ
method๋ post ๋ผ์ ๊ทธ ๋ถ๋ถ ์ง์ฐ๊ณ ์ ๋ ฅํจ
โ

BoardVO

MultipartFile ์ถ๊ฐ

write.jsp

์ฒจ๋ถ์ด๋ฏธ์ง path ์์
DB์๋ ํ์ผ์ ์ด๋ฆ๋ง ์ ์ฅํ๊ธฐ๋กํจ( ํ์ผ์ ๊ฒฝ๋ก)
BoardController
write ์์
@GetMapping("/write")
public String write(@ModelAttribute("writeBean") BoardVO writeBean,@RequestParam("info_idx") int info_idx ) {
writeBean.setBoard_idx(info_idx);
return "board/write";
}
board_idx๋ ๊ฐ์ด ์ ๋ฌ๋๊ฒํจ
write_pro ์์ DB๋ฅผ ๋ถ๋ฌ์ค๊ธฐ์ํด์
BoardServiec ์์ฑ - ๋์ค์ ์์ ํ ๊ฑฐ์
package com.demo.service;
import org.springframework.stereotype.Service;
import com.demo.domain.BoardVO;
@Service
public class BoardService {
public void addContentInfo(BoardVO writeBean) {
}
}
BoardController

@PostMapping("/write_pro")
public String write_pro(@Valid @ModelAttribute("writeBean") BoardVO writeBean, BindingResult result ) {
if(result.hasErrors()) {
return "board/write";
}
//DB์ ์ ๊ฒ์๊ธ ์ ์ฅ
boardService.addContentInfo(writeBean);
return "board/write_success";
}
BoardService์์ ์๋ฃ๋ฅผ ์ ๊ฐ์ง๊ณ ์๋์ง ํ ์คํธ ํ๊ธฐ



HomeController ์์ ํ์ฌ ์คํ๋๊ณ ์๋ ์ฃผ์๋ฅผ ํ์ธํ๋ค.
package com.demo.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(HttpServletRequest request) {
//์ค์ ์๋ฒ ์คํ ์ ํ๋ก์ ํธ ์ฃผ์๋ฅผ ์ถ๋ ฅํ๋ค.
System.out.println(request.getRealPath("/"));
return "index"; ///WEB-INF/views/๊ฐ ์๋ต๋์ด์๋๊ฑฐ์
}
}
์คํํ๋ฉด ์ฝ์์ ์๋์ ๊ฐ์ ์ฃผ์๊ฐ ์ ๋ ฅ๋๋ค.
C:\Java\workspace-legacy\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MiniProject
์ฝ์์ ํ์ธํด ๋ณด๋ฉด ์ ์ ์๋ฏ์ด ํฐ์บฃ์๋ฒ์์๋ ์คํ์ ํ ๋ ๊ฐ ํ๋ก์ ํธ ํด๋์์ ์คํํ๋ ๊ฒ์ด ์๋๋ผ
์ดํด๋ฆฝ์ค ์๋ฒ๋ .metadata์์ ์คํ๋จ
(์๋์ ํ๋ก์ ํธ ํด๋๋ค์ ์์ถํ์ฌ metadataํด๋๋ก ๋ค๊ณ ๊ฐ์ ์ฌ์ฉ)

ํ์ฌ resourcesํด๋์ upload ํด๋๋ฅผ ๋ง๋ ์ํ๊ณ
๋ง์ฝ์ ์ฌ๊ธฐ์ ์ด๋ฏธ๋ฅผ ์ง์ ์ ์ผ๋ก ๋ฃ๋๋ค๊ณ ํด์ ์๋ฒ์ ๋ฐ๋ก ์ ์ฅ๋๋๊ฒ์ด ์๋

๊ทธ๋์ ์ ๋ก๋ ๊ฒฝ๋ก๋ฅผ ์์ ๊ฐ์ด
C:\Java\workspace-legacy\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MiniProject์
resources -> upload์ ๋ฃ์ด์ค์ผํจ
BoardService
package com.demo.service;
import java.io.File;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.demo.domain.BoardVO;
@Service
public class BoardService {
private String path_upload = "C:\\Java\\workspace-legacy\\.metadata\\.plugins\\org.eclipse.wst.server.core\\" + ""
+ "tmp0\\wtpwebapps\\MiniProject\\resources\\upload";
// ์๋ฒ๋ก ์
๋ก๋ ๋ ํ์ผ์ ์
๋ก๋ ํด๋์ ์ ์ฅํ๊ณ ํ์ผ์ ์ด๋ฆ์ ๋ฆฌํดํ๋ ๋ฉ์๋
private String saveUploadFile(MultipartFile upload_file) {
// ํ์ฌ ์๊ฐ(๋ฐ๋ฆฌ์ธ์ปจ๋)์ ์ด์ฉํด์ ํ์ผ์ ์ด๋ฆ์ด ์ค๋ณต๋์ง ์๊ฒ ์ค์
String file_name = System.currentTimeMillis() + "_" + upload_file.getOriginalFilename();
try {
// ํ์ผ์ ๊ฒฝ๋ก(์์น)๋ก ํ์ผ์ ์ ์ฅํ๋ค.
upload_file.transferTo(new File(path_upload + "/" + file_name));
} catch (Exception e) {
e.printStackTrace();
}
return file_name; // ์ค์ ๋ก ์ ์ฅํ ํ์ผ์ ์ด๋ฆ์ด ๋ฆฌํด๋๋ค.
}
public void addContentInfo(BoardVO writeBean) {
MultipartFile upload_file = writeBean.getUpload_file();
//ํ์ผ์ ์ฌ์ด์ฆ๊ฐ 0๋ณด๋ค ํด ๊ฒฝ์ฐ(ํ์ผ์ด ์์ ๊ฒฝ์ฐ) ์๋ง ์ ์ฅํจ
if(upload_file.getSize() > 0) {
String file_name = saveUploadFile(upload_file);
//์ค์ ํ์ผ์ ์ ์ฅ ํ ํ์ผ์ด๋ฆ์ content_file์ ์ ์ฅ
writeBean.setContent_file(file_name);
}
}
}
path_upload ๋ณ์๋ฅผ ์ฌ์ฉํด์ ์ฃผ์๋ฅผ ๋ด์
ํ ์คํธ ์คํ

์ค์ ์ ๋ก๋ ํด๋์ ์ ์ฅ์ด ๋๋์ง ํ์ธํ๋ค.

๊ทธ๋ฆฌ๊ณ ์ฃผ์๊ฐ ๋๋ฌด ๊ธฐ๋๊น, ํ๋กํผํฐ์ ์ ์ฅํด์ ๋ค๋ฅธ ๊ณณ์์๋ ์ฝ๊ฒ ์ ๊ทผํ ์ ์๋๋ก ๋ง๋ ๋ค.
WEB-INF ์๋์ ํด๋ ์์ฑ


option.properties ํ์ผ ์์ฑ
path.upload = C:/Java/workspace-legacy/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MiniProject/resources/upload
์ญ์ฌ๋์ ๋ ๊ฐ๋ฅผ ์ผ๋ฐ ์ฌ๋์๋ก ๋ฐ๊ฟ
BoardService

@PropertySource("/WEB-INF/properties/option.properties") ๋ฅผ ์ฌ์ฉํด์ ์์ฑํ ํ๋กํผํฐ ์ฃผ์๋ฅผ ์ ๋ ฅํ๊ณ
@Value๋ฅผ ์ด์ฉํด์ ์ฌ์ฉ๊ฐ๋ฅํจ

DB์ ์ฅ
BoardMapper.xml
<insert id="newBoard" parameterType="com.demo.domain.BoardVO">
INSERT INTO
board(title,content,content_file, writer_idx,info_idx)
VALUES( #{title},#{content},#{content_file},#{writer_idx},#{info_idx}
)
</insert>

package com.demo.dao;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.demo.domain.BoardVO;
@Repository
public class BoardDAO {
@Autowired
private SqlSessionTemplate sqlTemplate;
//๊ธ ์์ฑ
public void addContentInfo(BoardVO writeBean) {
sqlTemplate.insert("board.newBoard", writeBean);
}
}
BoardService
package com.demo.service;
import java.io.File;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.demo.dao.BoardDAO;
import com.demo.domain.BoardVO;
import com.demo.domain.LoginUser;
@Service
@PropertySource("/WEB-INF/properties/option.properties")
public class BoardService {
@Value("${path.upload}")
private String path_upload;
//๋ก๊ทธ์ธ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด ์ด๋ฆ์ผ๋ก ์ฃผ์
@Resource(name = "sessionUser")
private LoginUser sessionUser;
@Autowired
private BoardDAO boardDAO;
// ์๋ฒ๋ก ์
๋ก๋ ๋ ํ์ผ์ ์
๋ก๋ ํด๋์ ์ ์ฅํ๊ณ ํ์ผ์ ์ด๋ฆ์ ๋ฆฌํดํ๋ ๋ฉ์๋
private String saveUploadFile(MultipartFile upload_file) {
// ํ์ฌ ์๊ฐ(๋ฐ๋ฆฌ์ธ์ปจ๋)์ ์ด์ฉํด์ ํ์ผ์ ์ด๋ฆ์ด ์ค๋ณต๋์ง ์๊ฒ ์ค์
String file_name = System.currentTimeMillis() + "_" + upload_file.getOriginalFilename();
try {
// ํ์ผ์ ๊ฒฝ๋ก(์์น)๋ก ํ์ผ์ ์ ์ฅํ๋ค.
upload_file.transferTo(new File(path_upload + "/" + file_name));
} catch (Exception e) {
e.printStackTrace();
}
return file_name; // ์ค์ ๋ก ์ ์ฅํ ํ์ผ์ ์ด๋ฆ์ด ๋ฆฌํด๋๋ค.
}
//๊ธ ์์ฑ
public void addContentInfo(BoardVO writeBean) {
MultipartFile upload_file = writeBean.getUpload_file();
//ํ์ผ์ ์ฌ์ด์ฆ๊ฐ 0๋ณด๋ค ํด ๊ฒฝ์ฐ(ํ์ผ์ด ์์ ๊ฒฝ์ฐ) ์๋ง ์ ์ฅํจ
if(upload_file.getSize() > 0) {
String file_name = saveUploadFile(upload_file);
//์ค์ ํ์ผ์ ์ ์ฅ ํ ํ์ผ์ด๋ฆ์ content_file์ ์ ์ฅ
writeBean.setContent_file(file_name);
}
//๊ธ์ด์ด๋ฅผ ์ธ์
๋ก๊ทธ์ธ์์ ๊ฐ์ ธ์์ ์ถ๊ฐ
writeBean.setWriter_idx(sessionUser.getUser_idx());
//DB์ ์ ์ฅ
boardDAO.addContentInfo(writeBean);
}
}
write_success.jsp
<%@ 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="root" value="${pageContext.request.contextPath}" />
<script>
alert('์ ์ฅ ๋์์ต๋๋ค')
location.href = "${root}/board/read"
</script>



'BACKEND > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2- ๊ธ ์์ธ๋ณด๊ธฐ ํ์ด์ง (1) | 2023.11.01 |
|---|---|
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 -๊ฒ์ํ ์ ๋ชฉ ๋ฐ ๊ฒ์๊ธ ๋ชฉ๋ก (1) | 2023.11.01 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ๊ธ ์์ฑํ๊ธฐ(์ ํจ์ฑ๊ฒ์ฌ) (0) | 2023.10.31 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ๊ธ ์์ฑํ๊ธฐ (get) (1) | 2023.10.31 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ์ ๋ณด ์์ ํ๊ธฐ (1) | 2023.10.31 |