Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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 more
Archives
Today
Total
관리 메뉴

기초공사 (html,css,javascript)

첨부파일 webhard-->add_file.jsp소스 본문

게시판

첨부파일 webhard-->add_file.jsp소스

에스프레소라떼 2024. 2. 11. 19:01

//webhard.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.sql.*" %>
<%-- <%@ page import="com.companyboard.db.*" %>  --%>


<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<style>
		table {width:700px; text-align:center;}
		th {background-color: cyan;}
		
		.left{text-align:left;}
		.right{text-align:right;}
		
		a:link{text-decoration:none; color:blue;}
		a:hover{text-decoration:none; color:Red;}	
	
	</style>
</head>
<body>
	<form action="add_file.jsp" enctype = "multipart/form-data" method="post">
		업로드할 파일을 선택하세요.<br>
		<input type="file" name="upload"><br>
		<input type="submit" value="업로드">	
	</form>
	<br>
	
	<table>
		<tr>
			<th>파일명</th>
			<th>업로드 시간<th>
			<th>크기</th>
			<th>삭제</th>
		</tr>
		
<%
	Class.forName("com.mysql.cj.jdbc.Driver");  //MySQL JDBC 드라이버 불러오기
	try(
	Connection conn = DriverManager.getConnection( // DB 접속
			"jdbc:mysql://localhost:3308/webhard" , "root", "5356");

	Statement stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery("select * from webhard_t");
	){
		while (rs.next()){
%>
		<tr>
			<td class="left">
			<!-- 파일명을 출력할떄 파일에 대한 링크를 만든다. -->
				<a href="files/<%=rs.getString("fname")%>">
					<%=rs.getString("fname")%>
				</a>
			</td>
			<td><%=rs.getString("ftime")%></td>
			<td class="right"><%=rs.getInt("fsize")%>&nbsp;&nbsp;</td>
			<td><a href="del_file.jsp?num=<%=rs.getInt("num")%>">X</a></td>
		</tr>

<%
		}
	} 	catch(Exception e){
		e.printStackTrace();
	}
%>
</table>

</body>

</html>

 

// add_file.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import = "java.sql.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import = "com.oreilly.servlet.MultipartRequest,
					com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@ page import = "java.io.File" %>
<%@ page import="com.companyboard.db.*" %>
<%@ page import = "java.time.*" %>

<!--3가지 미리 정해야함  
	1. 업로드 경로 : 절대경로
	2. 업로드 제한 용량 : byte단위
	3. 인코딩 정보
-->

<%
	MultipartRequest multi = new MultipartRequest(
		request,								// POST로 전달된 내용을 담은 객체
		application.getRealPath("webhard_folder/files"),		// 파일을 저장할 경로
		100 * 1024 * 1024,						// 최대파일 크기 (100MB)
		"utf-8",								// 인코딩
		new DefaultFileRenamePolicy()			//동일 파일명 처리 방법	
			
	);

	File file = multi.getFile("upload");		//파일객체 얻기
	
	if(file != null){
		/* out.print("파일 업로드 오류!"); */
	
		Class.forName("com.mysql.cj.jdbc.Driver");  //MySQL JDBC 드라이버 불러오기
		try(
		Connection conn = DriverManager.getConnection( // DB 접속
				"jdbc:mysql://localhost:3308/webhard" , "root", "5356");

		Statement stmt = conn.createStatement();
	){
		//현재시간 얻기
		String curTime = LocalDate.now() + " " + 
						LocalTime.now().toString().substring(0, 8);
		
		
		
		//쿼리 실행
		stmt.executeUpdate(String.format(
			"insert into webhard_t(fname, ftime, fsize)" + 
			"values ('%s','%s', %d)",
			file.getName(), curTime, (int)file.length()));
		
		//메인페이지로 돌아가기
		response.sendRedirect("webhard.jsp");
		
		return;
			
		} catch(Exception e){
			e.printStackTrace();
		}
	
	}
	
%> 

<!DOCTYPE html>
<html>
<head>
	<meta charset = "UTF-8">
</head>
<body>
<p>현재시간:<%=LocalTime.now().toString() %></p>

<script>
	alert('업로드실패!');
	history.back();//이전화면으로 돌아가게 하는 메소드이다.
</script>


</body>
</html>