java

JSP - ํŒŒ์ผ์—…๋กœ๋“œ cos๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

dev_summer 2021. 2. 27. 13:42

 

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ ์žˆ๋Š”๋ฐ , ๊ถŒํ•œ์„ ๊ฐ€์ง„ ์‚ฌ๋žŒ๋งŒ ์ œํ’ˆ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—…๋กœ๋“œ ํ• ์ˆ˜์žˆ๊ฒŒ ํ•˜๋ ค๋‹ˆ

์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐฐ์šด textarea๋‚˜ ์ธ๋จธ๋…ธํŠธ๊ฐ€ ์•„๋‹Œ ํŒŒ์ผ์—…๋กœ๋“œ๋ฅผ ํ•˜๊ณ  ๊ทธ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ๋””๋น„์—์ €์žฅํ• ์ˆ˜์žˆ๊ฒŒ ํ•˜๊ณ ์‹ถ์–ด์กŒ๋‹ค.

์ด๋ฏธ์ง€ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ• ์ˆ˜์ž‡๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์˜ˆ์ •!

์ผ๋‹จ ํผ์„ ๋งŒ๋“  ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์•„์•ผ๊ฒ ๋‹ค

 

1. COS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด

http://www.servlets.com/cos/

 

Servlets.com | com.oreilly.servlet

 

www.servlets.com

 

 

 

 

์ญ‰ ๋‚ด๋ ค๊ฐ€๋ฉด ๋‹ค์šด๋กœ๋“œ๊ฐ€ ๋ณด์ธ๋‹ค!

์ฑ…์—์žˆ๋Š” ๋ฒ„์ „๊ณผ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๋‹ค์šด~

 

web-inf -> libํด๋”์— ๋„ฃ์œผ๋ฉด cos ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„ ๋!!!

 

 

์™€์šฐ.. ๋“œ๋””์–ด cos๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ง„์งœ ํ•˜๋ฃจ์ข…์ผ ์–˜๋ž‘ ๋ฐ€๋‹นํ•œ๊ฑฐ ๊ฐ™๋‹ค

ํ…Œ์ŠคํŠธํ•œ ํผ์€ ์‚ฌ๋ผ์กŒ์œผ๋‹ˆ ์ง€๊ธˆ ํ”„๋กœ์ ํŠธ ํผ์œผ๋กœ ์ •๋ฆฌํ•ด์•ผ๊ฒŒ๋•….

 

 

 

1. form๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” jspํŒŒ์ผ

formํƒœ๊ทธ ์•ˆ์— input type์ด file์ธ ๊ฒƒ๋งŒ ์ถ”๊ฐ€ํ•ด๋„๋œ๋‹ค.

๊ธฐ์กด์— ์žˆ๋Š” ํผ์— fileํƒœ๊ทธ๋งŒ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฑด ํผ ์†์„ฑ์— enctype="multipart/form-data" ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

<div class="card">
		<div class="card-header">Ring ์ œํ’ˆ ์—…๋กœ๋“œ</div>
		<div class="card-body">
			<form action="/product?cmd=writeProc" method="POST"
				enctype="multipart/form-data">
	
				<div class="form-group">
					<label for="images">product images upload</label> 
					<input type="file" class="form-control" name="images" id="img_input">
				</div>
				
				<div class="form-group">
					<label for="productname">Ring Name</label> <input type="text"
						class="form-control" placeholder="๋ง์ด๋ฆ„" name="productname"
						required="required" maxlength="50">
				</div>
				
				
				<div class="form-group">
					<label for=madeof>Made Of</label> <input type="text"
						class="form-control" placeholder="์žฌ์งˆ" name="madeof"
						required="required" maxlength="50">
				</div>
				
				
				<div class="form-group">
					<label for="content">Content</label> <input type="text"
						class="form-control" placeholder="๋ฆฌ์ŠคํŠธํŽ˜์ด์ง€ ๊ฐ„๋‹จํ•œ ์„ค๋ช…" name="content"
						required="required" maxlength="50">
				</div>
				
				
				<div class="form-group">
					<label for="detail">detail</label>
					<textarea name="detail" class="form-control" rows="10" cols="5"placeholder="๋ง ์„ค๋ช…"></textarea>
				</div>

				<button type="submit" class="btn btn-primary">Submit</button>
			</form>
		</div>
	</div>

 

2. ํผ์„ ๋ฐ›์•„ ์‹คํ–‰ํ•  javaํŒŒ์ผ

 

๋‚˜๋Š” uploadํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ์ €์žฅํ• ๊ฑฐ๋ผ์„œ savePath๋ผ๋Š” ๋ณ€์ˆ˜ ์•ˆ์— upload๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹น

์ด ํŒŒ์ผ๋„ ์ดํด๋ฆฝ์Šค๊ฐ€ ๋งŒ๋“ค์–ด ์ฃผ๋Š”๊ฑฐ ์•„๋‹ˆ๊ณ  ๋‚ด๊ฐ€ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผํ•œ๋‹ค. ๋งŒ๋“ค์ˆ˜์žˆ๋Š” ์ฝ”๋“œ๋„ ์žˆ๋Š”๊ธด ํ•˜๋”๋ผ

๊ทธ ์•„๋ž˜๋Š” ์ ˆ๋Œ€๊ฒฝ๋กœ์˜ ์ฃผ์†Œ์ด๋‹ค. ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์ €์žฅํ•˜์—ฌ JSPํŒŒ์ผ์— ๋ฟŒ๋ฆฌ๋ฉด ๋ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์•„๋‹ˆ์˜€๋‹ค.

์ตœ์ดˆ๊ฒฝ๋กœ๊ฐ€ / ๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— /๋’ค์˜ ๊ฒฝ๋กœ๋กœ ์ €์žฅํ•ด์•ผ๋˜๋”๋ผ..

์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋‹น์—ฐํ•œ๊ฑด๋ฐ ์ด๊ฑธ ๋ช‡์‹œ๊ฐ„๋™์•ˆ์ด๋‚˜ ๋‚‘๋‚‘๊ฑฐ๋ ธ๋‹ค..

 

๋˜, enctype="multipart/form-data"์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋Š” request.getparmeter๋กœ ๋ฐ›์œผ๋ฉด ์•ˆ๋˜๋”๋ผ..

MultipartRequest๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ  ๊ฑฐ๊ธฐ์„œ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์•ผํ•œ๋‹ค.

 

์ ˆ๋Œ€๊ฒฝ๋กœ๋‚˜ ํŒŒ์ผ๋ช…๋“ฑ์„ ๋ถˆ๋Ÿฌ์˜ค์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๋“ค์ด ๋งŽ์€๋ฐ ๋ชจ๋ฅด๋‹ˆ๊นŒ ํ•˜๋‚˜์”ฉ ๋ฟŒ๋ ค๋ดค๋‹ค.

๋‚˜๋Š” "/upload/~~~" ์—…๋กœ๋“œ๋’ค์˜ ํŒŒ์ผ๋ช…๋งŒ ์ ์–ด์ค„๊ฒƒ์ด๊ธฐ๋•Œ๋ฌธ์— ํŒŒ์ผ๋ช…๋งŒ ๊ฐ–๊ณ ์™€์„œ ๋””๋น„์— ์ €์žฅํ–ˆ๋‹ค..

์ด๋ ‡๊ฒŒ ํ–ˆ๋Š”๋ฐ ๋‚ด์ผ ๋ชจ๋ ˆ ๊ฐ”๋Š”๋ฐ ์™œ ์ด๋Ÿฐ ๋…ธ๊ฐ€๋‹ค๋ฅผ ํ–ˆ๋Š๋ƒ๊ณ  ์ƒ˜์ด ์งˆ์ฑ…ํ•˜์‹ค๊ฑฐ ๊ฐ™๋‹ค..

ํ–ˆ๋‹ค๋Š” ๊ฒƒ์— ์˜๋ฏธ๋ฅผ ๋‘๊ฒ ๋‹ค..

 

public class ProductWriteProcAction implements Action {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		String savePath="upload"; // ์ €์žฅํ•˜๊ณ ์‹ถ์€ ํŒŒ์ผ, 
		ServletContext context = request.getSession().getServletContext();//ํ”„๋กœ์ ํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ
		String uploadFilePath = context.getRealPath(savePath);
		System.out.println(uploadFilePath);
		
		
		MultipartRequest multi = new MultipartRequest(request, uploadFilePath, 1024 * 1024 * 2, "UTF-8",
				new DefaultFileRenamePolicy());
		
		
		
		String type = multi.getParameter("type");
		String productname = multi.getParameter("productname");
		String madeof = multi.getParameter("madeof");
		String content = multi.getParameter("content");
		String detail = multi.getParameter("detail");
		String images = multi.getOriginalFileName("images");
		
		
		ProductDao productDao = ProductDao.getInstance();
		int result = productDao.save(type, productname, madeof, content, detail, images);

		if (result == 1) {
			RequestDispatcher dis = request.getRequestDispatcher("/product?cmd=list&type="+type);
			dis.forward(request, response);
		} else {
			Script.back(response, "๊ธ€ ์ž…๋ ฅ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
		}
	}
}

 

3. DB์— ์žˆ๋Š” ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ๋ฅผ ๋ฟŒ๋ฆด jspํŒŒ์ผ

img src="" ์ด ๋ถ€๋ถ„์—์„œ ๊ณ„์† ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค..

DB์— ์ƒ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•ด์•ผํ• ์ง€, ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•ด์•ผํ• ์ง€

๋˜ ๋ฟŒ๋ฆฌ๋ฆฌ๋•Œ ์Œ๋”ฐ์˜ดํ‘œ๋„ ํ•จ๊ป˜ ๋ฟŒ๋ ค์•ผํ•˜๋Š”๊ฑด๊ฐ€ ์ด๋Ÿฐ์ €๋Ÿฐ ๊ณ ๋ฏผ์„ ๋งŽ์ดํ–ˆ๋‹ค..

ํ•˜๊ณ ๋‚˜๋‹ˆ๊นŒ ์ง„์งœ ๋ณ„๊ฑฐ ์•„๋‹Œ๊ฑฐ ๊ฐ™๋‹ค....

 

<!--  ๋ฐ˜๋ณต๊ตฌ๊ฐ„ -->
				<c:forEach var="productList" items="${productList}">
					<div class="post-entry-1 h-50">
					 	<a href="#"> <img src="/upload/${productList.images}"		 	
							alt="Image" class="img-fluid">
						</a>
						<div class="post-entry-1-contents">
							<span class="meta">${productList.productname}</span>
							<h2>${productList.madeof}</h2>
							<p>${productList.content}</p>
						</div>
					</div>
				</c:forEach>
				<!--  ๋ฐ˜๋ณต๊ตฌ๊ฐ„ ๋ -->