๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์›น

jsp ํŒŒ์ผ ์—…๋กœ๋“œ (cos.jar) - ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ, ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๊ธฐ๋Šฅ

728x90

#JSP #MySQL #COS.jar #ํ˜„์žฌ๊ฐ์ฒด์˜์ ˆ๋Œ€๊ฒฝ๋กœ #jsp์ ˆ๋Œ€๊ฒฝ๋กœ๊ตฌํ•˜๊ธฐ

cos.jar๋กœ ์ด๋ฏธ์ง€ ํŒŒ์ผ ์˜ฌ๋ฆฌ๊ธฐ

์›น ์„œ๋ฒ„์— ํŒŒ์ผ(.jpg, .png, .hwp, ๋“ฑ๋“ฑ) ์—…๋กœ๋“œํ•ด DB์™€ ์—ฐ๋™ํ•˜๋Š”๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌํ˜„ ์™„๋ฃŒ ์ƒํƒœ)

์ €๋Š” ์ด๋ฏธ์ง€ํŒŒ์ผ(.jpg, .png)๋กœ ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์ œํ•œํ•˜์—ฌ ์‹œํ์–ด์ฝ”๋”ฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. (javascript๋กœ ์ด ๋ถ€๋ถ„๋„ ์„ค๋ช…ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.)

 

COS.jar (=COS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) : Multipart Request๋กœ ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

COS.jar ์„ค์น˜ํ•˜๊ธฐ > http://www.servlets.com/cos/

 

Servlets.com | com.oreilly.servlet

 

www.servlets.com

cos.jar ๋‹ค์šด๋ฐ›๊ธฐ

 

 

 

 

 

 

 

 

1. ๋‹ค์šด๋ฐ›์€ cos.jarํŒŒ์ผ์„

'ํ”„๋กœ์ ํŠธ ํด๋”> WebContent >lib' ์—

๋ณต์‚ฌ, ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•œ๋‹ค.(์ด๋™x)

 

 

 

 

 

 

 

2. ํ”„๋กœ์ ํŠธ ํด๋” ์šฐํด๋ฆญ > Build Path > Configure Build Path

 

 

 

 

 

 

 

 

3. Add External JARs... > ๋‹ค์šด๋ฐ›์€ cos.jar์„ ์„ ํƒํ›„ > Apply

 


์ฝ”๋“œ์ž‘์„ฑ

<form method="post" action="uploadAction.jsp" enctype="multipart/form-data" style="width: 300px">
	<input type="file" name="file" id="image" accept="image/*" onchange="setThumbnail(event);" /> 
		<img style="width: 300px; margin-top: 10px; border: dashed #D3D3D3;" id="preview-image"
		src="https://dummyimage.com/500x500/ffffff/000000.png&text=preview+image">
			
        <!--์ด๋ฏธ์ง€ ๋ฏธ๋ฆฌ๋ณด๊ธฐ-->    
	<script> 
		function setThumbnail(event) {
			var reader = new FileReader();
               <!--์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋œ ๊ฒฝ์šฐ--> 
			reader.onload = function(event) {
				var img = document.getElementById("preview-image");
				img.setAttribute("src", event.target.result); 
			};
            <!--<input>ํƒœ๊ทธ์—์„œ ์„ ํƒํ•œ ํŒŒ์ผ ์ฝ๊ธฐ-->
			reader.readAsDataURL(event.target.files[0]);  
		}
	</script>
				
	<table style="width: 300px;">
		<tr>
			<th>์ œํ’ˆ</th>
			<td><input name="name" maxlength="50" 
            	style="display: block; width: 100%; max-height: 100%;" required>
               </td>
		</tr>
			<th>๊ธˆ์•ก</th>
			<td><input name="price" maxlength="10" 
            	style="display: block; width: 100%; max-height: 100%;" required></td>
		</tr>
		<tr>
			<th>์ˆ˜๋Ÿ‰</th>
			<td><input name="num" maxlength="10" 
            	style="display: block; width: 100%; max-height: 100%;" required></td>
		</tr>
	</table>				
	<button class="test-result-button" type="submit" 
    	style="margin-top: 20px">๋“ฑ๋กํ•˜๊ธฐ</button>
</form>

<input>ํƒœ๊ทธ์˜ 'accept'์†์„ฑ : ์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ ํƒ€์ž… ๋ช…์‹œ, type ์†์„ฑ๊ฐ’์ด 'file'์ธ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

=>ํŒŒ์ผ ์‹œํ์–ด์ฝ”๋”ฉ

<body>
	<%
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");

	PreparedStatement pstmt = null;
	ResultSet rset = null;
	Connection conn = null;
	Properties connectionProps = new Properties();

	String DBUrl = "jdbc:mysql://localhost:3306/***";
	String DBuser = "***";
	String DBpasswd = "***";
	String DBTimeZone = "UTC";

	connectionProps.put("user", DBuser);
	connectionProps.put("password", DBpasswd);
	connectionProps.put("serverTimezone", DBTimeZone);

	String directory = this.getClass().getResource("").getPath(); 
	directory = directory.substring(1, directory.indexOf(".metadata")) + "***/WebContent/***/***"+ ***;

	int maxSize = 1024 * 1024 * 100;
	String encoding = "UTF-8";


	MultipartRequest multipartRequest = new MultipartRequest(request, directory, maxSize, encoding,
			new DefaultFileRenamePolicy());

	String fileName = multipartRequest.getOriginalFileName("file");
	String name = multipartRequest.getParameter("name");
	int price = Integer.parseInt(multipartRequest.getParameter("price"));
	int num = Integer.parseInt(multipartRequest.getParameter("num"));
	
	try {
		conn = DriverManager.getConnection(DBUrl, connectionProps);
		String sqlSt = null;
		sqlSt = "SELECT LAST_VALUE(column1) OVER (ORDER BY column1 DESC) FROM ***.*** WHERE column2="
		+ *** + " limit 1"; 
		pstmt = conn.prepareStatement(sqlSt);
		rset = pstmt.executeQuery();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	int x = 0;
	while (rset.next()) {
		x = rset.getInt("LAST_VALUE(column1) OVER (ORDER BY column1 DESC)") + 1;
	}

	int storeid = x / 100;
	try {
		DBUtil.insertOfflineStock(conn, x, ***, ***, fileName);
	} catch (Exception e) {
		e.printStackTrace();
	}
	}
	%>
</body>
<script>
	alert("์ œํ’ˆ์„ ๋“ฑ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค.")
	location.href = "***.jsp";
</script>

String directory = this.getClass().getResource("").getPath();  => ํ˜„์žฌ๊ฐ์ฒด์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๋Š”๋ฒ•

728x90