IT새내기IM 2023. 2. 2. 17:35

reply.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/include/header.jsp" %>


<style>

.td1{
	border-bottom: 1px solid #E2E2E2;
}

#td1{
	border-bottom: 0.4px solid #E2E2E2;
}

/* 첨부파일디자인변경 */
.filebox input[type="file"] {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip:rect(0,0,0,0);
  border: 0;
}

.filebox label {
  display: inline-block;
  padding: .5em .75em;
  color: #999;
  font-size: inherit;
  line-height: normal;
  vertical-align: middle;
  background-color: #fdfdfd;
  cursor: pointer;
  border: 1px solid #ebebeb;
  border-bottom-color: #e2e2e2;
  border-radius: .25em;
}

/* named upload */
.filebox .upload-name {
  display: inline-block;
  padding: .5em .75em;  /* label의 패딩값과 일치 */
  font-size: inherit;
  font-family: inherit;
  line-height: normal;
  vertical-align: middle;
  background-color: #f5f5f5;
  border: 1px solid #ebebeb;
  border-bottom-color: #e2e2e2;
  border-radius: .25em;
  -webkit-appearance: none; /* 네이티브 외형 감추기 */
  -moz-appearance: none;
  appearance: none;
}
/* 첨부파일디자인변경완료*/
</style>
<script>

$(document).ready(function(){
	  var fileTarget = $('.filebox .upload-hidden');

	  fileTarget.on('change', function(){  // 값이 변경되면
	    if(window.FileReader){  // modern browser
	      var filename = $(this)[0].files[0].name;
	    } 
	    else {  // old IE
	      var filename = $(this).val().split('/').pop().split('\\').pop();  // 파일명만 추출
	    }
	    
	    // 추출한 파일명 삽입
	    $(this).siblings('.upload-name').val(filename);
	  });
	});
</script>





<% 
request.getParameter("utf-8");

String code = request.getParameter("code");
String uid = request.getParameter("uid");
String sql = "select * from "+code+" where uid="+uid+"";

Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);

String gongji = "";
String subject = "";
String comment = "";
int fid = 0;
String thread = "";


if(rs.next()){
	gongji = rs.getString("gongji");
	subject = rs.getString("subject");
	comment = rs.getString("comment");
	fid = rs.getInt("fid");
	thread = rs.getString("thread");
}
%>

<form action="reply_insert.jsp" method="post" enctype="multipart/form-data">
<input name="code" value="<%=code%>" type="hidden">
<input name="uid" value="<%=uid%>" type="hidden">
<input name="fid" value="<%=fid%>" type="hidden">
<input name="thread" value="<%=thread%>" type="hidden">


    <table border=0 align=center width=1200 style="border-top:1px solid #F2F2F2;vertical-align:top;">
		<tr>
			<td class=td1 height=40>유형</td>
			<td class=td1 height=40>
				<%if(session_level == "10"){ %>
					<input type="radio" id="gongji" name="gongji" value="1">공지
					<input type="radio" id="gongji" name="gongji" value="2" checked>일반
					<input type="radio" id="gongji" name="gongji" value="3">비밀
				<%}else if(gongji.equals("3")){%>
					<input type="radio" id="gongji" name="gongji" value="2">일반
					<input type="radio" id="gongji" name="gongji" value="3" checked>비밀
				<%}else{%>
					<input type="radio" id="gongji" name="gongji" value="2" checked>일반
				<%}%>	
			</td>
		</tr>
		
		<tr height=40>
			<td class=td1>제목</td><td class=td1><input id="subject" name="subject" style="width:98%;height:35px;"></td>
		</tr>
		
		<tr height=500>
			<td class=td1>내용</td><td class=td1><input id="comment" name="comment" style="width:98%;height:450px;"></td>
		</tr>
		
		<tr>
		<td width=200 class=td1>첨부파일</td>
			<td width=1000 height=40 class="filebox" id=td1>
			<input class="upload-name" value="파일선택" disabled="disabled">
			<label for="ex_filename">파일찾기</label>
			<input type="file" id="ex_filename" name="file1" class="upload-hidden">
			
			</td>
		</tr>
		<tr>
			<td class=td1 height=40></td>
			<td class=td1 height=40 style="font-size:25px;">
			<button class=button style="font-size:25px;">[답변작성]</button>
			<a href="list.jsp?code=<%=code%>">[목록]</a>
			<a href="reply.jsp?code=<%=code%>&uid=<%=uid%>">[새로고침]</a>
			</td>
		</tr>
	</table>
</form>

 

reply-insert.jsp

 

<%@page import="java.sql.ResultSet"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ include file="/include/header.jsp" %>    
<%
request.setCharacterEncoding("utf-8");

//String uploadPath=request.getRealPath("../upload");
String uploadPath="C:\\jsp\\ppjj3\\WebContent\\upload";
int size=10*1024*1024; //10메가바이트 최대

String file1="";
String file1_name="";
String file1_rename="";
String file1_small="";

MultipartRequest multi=new MultipartRequest(request,uploadPath,size,"utf-8",new DefaultFileRenamePolicy());

String code = multi.getParameter("code");
String gongji = multi.getParameter("gongji");
String subject = multi.getParameter("subject");
String comment = multi.getParameter("comment");
String fid = multi.getParameter("fid");
String thread = multi.getParameter("thread");

//첨부관련
Enumeration files=multi.getFileNames();

file1=(String)files.nextElement();
file1_name=multi.getOriginalFileName(file1);
file1_rename=multi.getFilesystemName(file1);

if(file1_name != null){ //첨부가 있다면
	
}else{ //첨부가 없다면
	file1_name = "";
	file1_rename = "";
}

//thread 값 설정
String sql_fid = "select thread,right(thread,1) as thread_right from "+code+" where fid = "+fid+" AND length(thread) = length('"+thread+"')+1 AND locate('"+thread+"',thread) = 1 ORDER BY thread DESC limit 1";

//locate('AB' , 'ABA') = 1
//첫항의 'AB'가 두번째 항의 내용 중에 1번째 자리부터 매칭이 있는지 여부

Connection conn_count=DriverManager.getConnection(url, user, password);
Statement stmt_count=conn_count.createStatement();
ResultSet rs_count=stmt_count.executeQuery(sql_fid);

String o_thread = "";
String r_thread = "";
String thread_head = "";
String new_thread = "";
char thread_foot = 'a';

if(rs_count.next()){
	o_thread = rs_count.getString("thread");
	//끝자리 +1 처리
	r_thread = rs_count.getString("thread_right");
	thread_foot = (char)(r_thread.charAt(0) + 1);
	//out.print(thread_foot);
}

if(o_thread != null && o_thread != ""){
	thread_head = o_thread.substring(0,o_thread.length()-1);
	new_thread = thread_head + thread_foot;
}else{
	new_thread = thread+"A";
}

//작성시간 설정
Date today = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String signdate = sd.format(today);

String sql = "insert into "+code+" (id,name,subject,comment,gongji,signdate,file1,file1_o,fid,thread) values ('"+session_id+"','"+session_name+"','"+subject+"','"+comment+"','"+gongji+"','"+signdate+"','"+file1_rename+"','"+file1_name+"',"+fid+",'"+new_thread+"')";

Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);

stmt.close();
con.close();
%>

<%if(code.equals("gallery")){%>   <script>location.href = "gal.jsp?code=<%=code%>";</script>     <%}%>
<%if(code.equals("gallery1")){%>   <script>location.href = "gal.jsp?code=<%=code%>";</script>     <%}%>
<%if(code.equals("gallery2")){%>   <script>location.href = "gal.jsp?code=<%=code%>";</script>     <%}%>
<%if(code.equals("gallery3")){%>   <script>location.href = "gal.jsp?code=<%=code%>";</script>     <%}%>

<script>location.href = "list.jsp?code=<%=code%>";</script>