#JSP #MySQL #COS.jar #ํ์ฌ๊ฐ์ฒด์์ ๋๊ฒฝ๋ก #jsp์ ๋๊ฒฝ๋ก๊ตฌํ๊ธฐ
์น ์๋ฒ์ ํ์ผ(.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
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(); => ํ์ฌ๊ฐ์ฒด์ ์ ๋ ๊ฒฝ๋ก ๊ฐ์ ธ์ค๋๋ฒ