파일 Upload를 위해서는 MultipartRequest를 사용하였슴...
이 클래스는 한빛미디어의 Java 서블릿 프로그래망에 자세히
설명되어 있습니다... 물론 다른 Class를 이용해서 파일을
Upload 할수도 있습니다.
---------------
goods.html
---------------
<!-- 쇼핑몰에서 상품을 등록하기 위한 HTML -->
<html>
<head><title>상품등록</title></head>
<body bgcolor='#9999EA' text='black' link='blue' vlink='purple' alink='red'>
<form method='post' action='/jsp/servlet/GoodsUpload' enctype='multipart/form-data'>
<p align='left'> </p>
<p align='center'>상품등록</p>
<div align='center'>
<table border='1' cellspacing='1'>
<tr>
<td width='104' rowspan='6' bgcolor='white'><p align='center'>
<font face='돋움' size='2'>
<img src='상품1.gif' width=107 height='154' border='0'>
</font>
</td>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품번호</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_num' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품이름</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_name' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제조회사</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_co' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품가격</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_price' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품수량</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_su' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='17' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품사진</font>
</td>
<td width='248' height='17' bgcolor='FFD8F7'><p align='left'>
<input type='file' name='p_imagename' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='449' height='31' colspan='3' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품설명</font>
</td>
</tr>
<tr>
<td width='449' height='107' colspan='3' bgcolor='FFD8F7'><p align='left'>
<textarea name='p_info' rows='6' cols='72'></textarea>
</td>
</tr>
</table>
</div>
<p align='center'><br>
<input type='submit' name='submitS' value='등 록'>
<input type='reset' name='resetS' value='취 소'>
</p>
</form>
</body>
</html>
----------------------
GoodsUpload.java
----------------------
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.*;
import neonet.util.*; //파일업로드를 위한 Class(별도 필요)
public class GoodsUpload extends HttpServlet {
Connection con;
Statement stmt;
ResultSet rs;
//form variable
String p_noS, p_nameS, p_coS, p_infoS, ws_dateS, p_imageNameS;
int p_suS, p_priceS;
//입력되는 레코드를 받는 변수
int maxnum;
// 파일이름/Type을 저장하기 위한 변수
String filename, filetype;
File f = new File("");
PrintWriter out;
private void DBConnect() {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection( "jdbc:oracle:thin:@211.55.52.254:1521:wink" ,"bitcamp","bitcamp");
con.setAutoCommit(false);
}
catch(Exception e) {
Log.println("DBConnect",e.toString());
}
}
public void init(ServletConfig config) throws ServletException {
super.init();
DBConnect();
}
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
out = res.getWriter();
try {
//파일업로드를 위해 객체생성및 파일사이즈를 5메가로 제함
MultipartRequest multi = new MultipartRequest(req, "c:/tomcat4/webapps/jsp/upload", 5*1024*1024);
Enumeration files = multi.getFileNames();
while(files.hasMoreElements()) {
String name = (String)files.nextElement();
//받아들이는 파일의 이름을 한글처리
filename = Han.Uni2Ksc(multi.getFilesystemName(name));
filetype = multi.getContentType(name);
//파일을 생성
f = multi.getFile(name);
}
p_noS = multi.getParameter("p_num");
p_nameS = Han.Uni2Ksc(multi.getParameter("p_name"));
p_coS = multi.getParameter("p_co");
String su = multi.getParameter("p_su");
p_suS = Integer.parseInt(su);
String price = multi.getParameter("p_price");
p_priceS = Integer.parseInt(price);
p_infoS = Han.Uni2Ksc(multi.getParameter("p_info"));
////////////////////////////////날짜계산하자...
Calendar c = Calendar.getInstance();
String date1 = "" + c.get(Calendar.YEAR);
String month = "" + (c.get(Calendar.MONTH)+1);
if (month.length()==1){
month = "0" + month;
}
date1 += "/"+month+"/";
String date = "" + c.get(Calendar.DATE);
if (date.length()==1){
date = "0" + date;
}
date1 += date + "/";
String hour = "" + c.get(Calendar.HOUR);
if (hour.length()==1){
hour = "0" + hour;
}
date1+=hour+"/";
String min = "" + c.get(Calendar.MINUTE);
if (min.length()==1){
min = "0" + min;
}
date1+=min+"/";
String am = "" + c.get(Calendar.AM_PM);
date1+=am;
////////////////////////////////////////////////
stmt = con.createStatement();
rs = stmt.executeQuery("select max(num) from prod");
while(rs.next()) {
maxnum = rs.getInt(1)+1;
}
//Insert SQL Create
String sql = "insert into prod (num, prod_num, prod_name, prod_co, prod_su";
sql = sql + " , prod_price, prod_image, prod_info, ws_date) values (";
sql = sql + maxnum + "," + make(p_noS) + "," + make(p_nameS) +"," ;
sql = sql + make(p_coS) +"," + p_suS + "," + p_priceS +",";
sql = sql + make(filename) + "," + make(p_infoS) + "," + make(date1) + ")";
stmt.executeUpdate(sql);
con.commit();
out.println("<br>");
out.println(Han.Ksc2Uni("<html><head><title>상품등록</title></head>"));
out.println(Han.Ksc2Uni("<center><h3>다음과 같이 상품이 등록되었습니다...</h3></center>"));
out.println("<body bgcolor='#9999EA' text='black' link='blue' vlink='purple' alink='red'>");
out.println("<p align='left'>");
out.println("<div align='center'>");
out.println("<table border='1' cellspacing='0' cellpadding='0' bordercolor='white' bordercolordark='white' bordercolorlight='whte'>");
out.println(Han.Ksc2Uni("<tr>"
+ " <td width='159' height='184' rowspan='6' bgcolor='white'><p align='center'> "
+ "<img src='c:\\tomcat4\\webapps\\jsp\\upload\"+filename+"' width=84 height='119' border='0'>"
+ "</td>"
+ "<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품번호</font>"
+ " </td>"
+ "<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_noS + "</font>"
+ "</td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품이름</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_nameS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제조회사</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_coS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품가격</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_priceS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품수량</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_suS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='17' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품사진</font>"
+ " </td>"
+ " <td width='248' height='17' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + filename + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='449' height='31' colspan='3' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품설명</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='449' height='107' colspan='3' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_infoS + "</font>"
+ " </td>"
+ "</tr>"));
out.println("</table></div>");
out.println("<p align='center'><br></p>");
out.println(Han.Ksc2Uni("<center><a href=/jsp/goods.html'>추가등록</a></center>"));
out.println("</body></html>");
out.flush();
stmt.close();
}
catch (Exception e) {
out.println("Error!::"+e);
}
}
public void destroy() {
try {
if (con != null){
con.close();
}
}
catch (Exception e) {out.println("Servlet Destroy Error::"+e);}
}
public String make(String str) {
return "'" + str + "'";
}
}
이 클래스는 한빛미디어의 Java 서블릿 프로그래망에 자세히
설명되어 있습니다... 물론 다른 Class를 이용해서 파일을
Upload 할수도 있습니다.
---------------
goods.html
---------------
<!-- 쇼핑몰에서 상품을 등록하기 위한 HTML -->
<html>
<head><title>상품등록</title></head>
<body bgcolor='#9999EA' text='black' link='blue' vlink='purple' alink='red'>
<form method='post' action='/jsp/servlet/GoodsUpload' enctype='multipart/form-data'>
<p align='left'> </p>
<p align='center'>상품등록</p>
<div align='center'>
<table border='1' cellspacing='1'>
<tr>
<td width='104' rowspan='6' bgcolor='white'><p align='center'>
<font face='돋움' size='2'>
<img src='상품1.gif' width=107 height='154' border='0'>
</font>
</td>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품번호</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_num' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품이름</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_name' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제조회사</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_co' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품가격</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_price' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품수량</font>
</td>
<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>
<input type='text' name='p_su' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='91' height='17' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품사진</font>
</td>
<td width='248' height='17' bgcolor='FFD8F7'><p align='left'>
<input type='file' name='p_imagename' style='border-width:1; border-style:dashed;'>
</td>
</tr>
<tr>
<td width='449' height='31' colspan='3' bgcolor='#E0FFFA'><p align='left'>
<font face='돋움' size='2'> 제품설명</font>
</td>
</tr>
<tr>
<td width='449' height='107' colspan='3' bgcolor='FFD8F7'><p align='left'>
<textarea name='p_info' rows='6' cols='72'></textarea>
</td>
</tr>
</table>
</div>
<p align='center'><br>
<input type='submit' name='submitS' value='등 록'>
<input type='reset' name='resetS' value='취 소'>
</p>
</form>
</body>
</html>
----------------------
GoodsUpload.java
----------------------
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.*;
import neonet.util.*; //파일업로드를 위한 Class(별도 필요)
public class GoodsUpload extends HttpServlet {
Connection con;
Statement stmt;
ResultSet rs;
//form variable
String p_noS, p_nameS, p_coS, p_infoS, ws_dateS, p_imageNameS;
int p_suS, p_priceS;
//입력되는 레코드를 받는 변수
int maxnum;
// 파일이름/Type을 저장하기 위한 변수
String filename, filetype;
File f = new File("");
PrintWriter out;
private void DBConnect() {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection( "jdbc:oracle:thin:@211.55.52.254:1521:wink" ,"bitcamp","bitcamp");
con.setAutoCommit(false);
}
catch(Exception e) {
Log.println("DBConnect",e.toString());
}
}
public void init(ServletConfig config) throws ServletException {
super.init();
DBConnect();
}
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
out = res.getWriter();
try {
//파일업로드를 위해 객체생성및 파일사이즈를 5메가로 제함
MultipartRequest multi = new MultipartRequest(req, "c:/tomcat4/webapps/jsp/upload", 5*1024*1024);
Enumeration files = multi.getFileNames();
while(files.hasMoreElements()) {
String name = (String)files.nextElement();
//받아들이는 파일의 이름을 한글처리
filename = Han.Uni2Ksc(multi.getFilesystemName(name));
filetype = multi.getContentType(name);
//파일을 생성
f = multi.getFile(name);
}
p_noS = multi.getParameter("p_num");
p_nameS = Han.Uni2Ksc(multi.getParameter("p_name"));
p_coS = multi.getParameter("p_co");
String su = multi.getParameter("p_su");
p_suS = Integer.parseInt(su);
String price = multi.getParameter("p_price");
p_priceS = Integer.parseInt(price);
p_infoS = Han.Uni2Ksc(multi.getParameter("p_info"));
////////////////////////////////날짜계산하자...
Calendar c = Calendar.getInstance();
String date1 = "" + c.get(Calendar.YEAR);
String month = "" + (c.get(Calendar.MONTH)+1);
if (month.length()==1){
month = "0" + month;
}
date1 += "/"+month+"/";
String date = "" + c.get(Calendar.DATE);
if (date.length()==1){
date = "0" + date;
}
date1 += date + "/";
String hour = "" + c.get(Calendar.HOUR);
if (hour.length()==1){
hour = "0" + hour;
}
date1+=hour+"/";
String min = "" + c.get(Calendar.MINUTE);
if (min.length()==1){
min = "0" + min;
}
date1+=min+"/";
String am = "" + c.get(Calendar.AM_PM);
date1+=am;
////////////////////////////////////////////////
stmt = con.createStatement();
rs = stmt.executeQuery("select max(num) from prod");
while(rs.next()) {
maxnum = rs.getInt(1)+1;
}
//Insert SQL Create
String sql = "insert into prod (num, prod_num, prod_name, prod_co, prod_su";
sql = sql + " , prod_price, prod_image, prod_info, ws_date) values (";
sql = sql + maxnum + "," + make(p_noS) + "," + make(p_nameS) +"," ;
sql = sql + make(p_coS) +"," + p_suS + "," + p_priceS +",";
sql = sql + make(filename) + "," + make(p_infoS) + "," + make(date1) + ")";
stmt.executeUpdate(sql);
con.commit();
out.println("<br>");
out.println(Han.Ksc2Uni("<html><head><title>상품등록</title></head>"));
out.println(Han.Ksc2Uni("<center><h3>다음과 같이 상품이 등록되었습니다...</h3></center>"));
out.println("<body bgcolor='#9999EA' text='black' link='blue' vlink='purple' alink='red'>");
out.println("<p align='left'>");
out.println("<div align='center'>");
out.println("<table border='1' cellspacing='0' cellpadding='0' bordercolor='white' bordercolordark='white' bordercolorlight='whte'>");
out.println(Han.Ksc2Uni("<tr>"
+ " <td width='159' height='184' rowspan='6' bgcolor='white'><p align='center'> "
+ "<img src='c:\\tomcat4\\webapps\\jsp\\upload\"+filename+"' width=84 height='119' border='0'>"
+ "</td>"
+ "<td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품번호</font>"
+ " </td>"
+ "<td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_noS + "</font>"
+ "</td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품이름</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_nameS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제조회사</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_coS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품가격</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_priceS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='21' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품수량</font>"
+ " </td>"
+ " <td width='248' height='21' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_suS + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='91' height='17' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품사진</font>"
+ " </td>"
+ " <td width='248' height='17' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + filename + "</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='449' height='31' colspan='3' bgcolor='#E0FFFA'><p align='left'>"
+ " <font face='돋움' size='2'> 제품설명</font>"
+ " </td>"
+ "</tr>"
+ "<tr>"
+ " <td width='449' height='107' colspan='3' bgcolor='FFD8F7'><p align='left'>"
+ " <font face='돋움' size='2'> " + p_infoS + "</font>"
+ " </td>"
+ "</tr>"));
out.println("</table></div>");
out.println("<p align='center'><br></p>");
out.println(Han.Ksc2Uni("<center><a href=/jsp/goods.html'>추가등록</a></center>"));
out.println("</body></html>");
out.flush();
stmt.close();
}
catch (Exception e) {
out.println("Error!::"+e);
}
}
public void destroy() {
try {
if (con != null){
con.close();
}
}
catch (Exception e) {out.println("Servlet Destroy Error::"+e);}
}
public String make(String str) {
return "'" + str + "'";
}
}
기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/웹퍼블리싱… | 12-27 | 2250 | ||
[채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1617 | ||
53 | [평일주간100%환급]Spring,JAVA,JSP,안드로이드,C#닷넷,SQL,튜닝… | 03-15 | 1411 | |
52 | [주말]C#,ASP.NET마스터 | 01-31 | 1530 | |
51 | [기업100%환급,평일주간]SQL기초에서스키마오브젝트,PLSQL,힌트… | 01-31 | 1768 | |
50 | [기업100%환급]개발자를위한스프링,마이바티스,하이버네이트(스… | 01-31 | 1219 | |
49 | [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정 | 01-19 | 1512 | |
48 | [평일주간,평일야간,주말]안드로이드개발자과정 | 01-11 | 1343 | |
47 | [평일야간,주말주간,주말야간]JAVA,Network&JSP&Spring,MyBatis,… | 01-03 | 1879 | |
46 | 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/웹퍼블리싱… | 12-27 | 2250 | |
45 | [기업100%환급]자바웹개발기초과정(JAVA,JDBC,JSP,Servlet,Aajx,… | 12-19 | 1617 | |
44 | [평일주간야간, 주말]웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX… | 12-14 | 1588 | |
43 | [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1617 | |
42 | [주말주간]자바&웹,jQUERY,스프링프레임워크 | 12-09 | 1291 | |
41 | [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 | 12-01 | 1511 | |
40 | [기업100%환급]자바기초&안드로이드개발자과정(Android전액환급… | 12-01 | 1693 | |
39 | [평일야간,주말]SQL기초에서실무까지(SQL기초,PLSQL,힌트,튜닝) | 12-01 | 1162 |
댓글 없음:
댓글 쓰기