--- .classpath
+++ .classpath
... | ... | @@ -2,6 +2,7 @@ |
2 | 2 |
<classpath> |
3 | 3 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> |
4 | 4 |
<attributes> |
5 |
+ <attribute name="module" value="true"/> |
|
5 | 6 |
<attribute name="maven.pomderived" value="true"/> |
6 | 7 |
</attributes> |
7 | 8 |
</classpathentry> |
... | ... | @@ -18,9 +19,9 @@ |
18 | 19 |
</classpathentry> |
19 | 20 |
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> |
20 | 21 |
<attributes> |
22 |
+ <attribute name="test" value="true"/> |
|
21 | 23 |
<attribute name="optional" value="true"/> |
22 | 24 |
<attribute name="maven.pomderived" value="true"/> |
23 |
- <attribute name="test" value="true"/> |
|
24 | 25 |
</attributes> |
25 | 26 |
</classpathentry> |
26 | 27 |
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> |
... | ... | @@ -29,5 +30,6 @@ |
29 | 30 |
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> |
30 | 31 |
</attributes> |
31 | 32 |
</classpathentry> |
33 |
+ <classpathentry kind="lib" path="C:/dev/cos-20.08/lib/cos.jar"/> |
|
32 | 34 |
<classpathentry kind="output" path="target/classes"/> |
33 | 35 |
</classpath> |
--- .settings/org.eclipse.wst.common.component
+++ .settings/org.eclipse.wst.common.component
... | ... | @@ -1,9 +1,18 @@ |
1 | 1 |
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0"> |
2 |
+ |
|
2 | 3 |
<wb-module deploy-name="WebTest-0.0.1-SNAPSHOT"> |
4 |
+ |
|
3 | 5 |
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/> |
6 |
+ |
|
4 | 7 |
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/> |
8 |
+ |
|
5 | 9 |
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> |
10 |
+ <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/> |
|
11 |
+ |
|
6 | 12 |
<property name="java-output-path" value="/JSP_Board/build/classes"/> |
13 |
+ |
|
7 | 14 |
<property name="context-root" value="WebTest"/> |
15 |
+ |
|
8 | 16 |
</wb-module> |
17 |
+ |
|
9 | 18 |
</project-modules> |
+++ src/main/java/config/FileDownloadHelper.java
... | ... | @@ -0,0 +1,28 @@ |
1 | +package config; | |
2 | + | |
3 | +import java.io.FileInputStream; | |
4 | +import java.io.IOException; | |
5 | +import java.io.OutputStream; | |
6 | + | |
7 | +public class FileDownloadHelper { | |
8 | + | |
9 | + public static void copy(String filePath, OutputStream os) throws IOException{ | |
10 | + FileInputStream is = null; | |
11 | + try { | |
12 | + is = new FileInputStream(filePath); | |
13 | + byte[] data = new byte[8096]; | |
14 | + int len = -1; | |
15 | + while((len = is.read(data)) != -1) { | |
16 | + os.write(data, 0, len); | |
17 | + } | |
18 | + }finally { | |
19 | + if(is != null) { | |
20 | + try { | |
21 | + is.close(); | |
22 | + }catch (Exception e) { | |
23 | + } | |
24 | + } | |
25 | + } | |
26 | + } | |
27 | + | |
28 | +} |
--- src/main/java/control/MemberServlet.java
+++ src/main/java/control/MemberServlet.java
... | ... | @@ -1,7 +1,11 @@ |
1 | 1 |
package control; |
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 |
+import java.io.FileInputStream; |
|
5 |
+import java.io.FileNotFoundException; |
|
4 | 6 |
import java.io.IOException; |
7 |
+import java.io.InputStream; |
|
8 |
+import java.io.OutputStream; |
|
5 | 9 |
import java.io.PrintWriter; |
6 | 10 |
import java.text.SimpleDateFormat; |
7 | 11 |
import java.util.Calendar; |
... | ... | @@ -10,6 +14,7 @@ |
10 | 14 |
import java.util.List; |
11 | 15 |
import java.util.Map; |
12 | 16 |
import javax.servlet.RequestDispatcher; |
17 |
+import javax.servlet.ServletContext; |
|
13 | 18 |
import javax.servlet.ServletException; |
14 | 19 |
import javax.servlet.annotation.WebServlet; |
15 | 20 |
import javax.servlet.http.HttpServlet; |
... | ... | @@ -18,6 +23,9 @@ |
18 | 23 |
import model.MemberDAO; |
19 | 24 |
import model.MemberDTO; |
20 | 25 |
import model.ReplyDTO; |
26 |
+import com.oreilly.servlet.*; |
|
27 |
+//import com.oreilly.servlet.MultipartRequest; |
|
28 |
+import com.oreilly.servlet.multipart.DefaultFileRenamePolicy; |
|
21 | 29 |
|
22 | 30 |
/** |
23 | 31 |
* Servlet implementation class MemberServlet |
... | ... | @@ -102,14 +110,29 @@ |
102 | 110 |
request.setCharacterEncoding("UTF-8"); |
103 | 111 |
response.setCharacterEncoding("UTF-8"); |
104 | 112 |
response.setContentType("text/html; charset=utf-8"); |
105 |
- |
|
106 |
- String writer = request.getParameter("inputWriter"); |
|
113 |
+ |
|
114 |
+/**************************파일 업로드*****************************/ |
|
115 |
+ |
|
116 |
+ String savePath = "upload"; |
|
117 |
+ int uploadFileSizeLimit = 5 * 1024 * 1024; |
|
118 |
+ String encType = "UTF-8"; |
|
119 |
+ |
|
120 |
+ ServletContext context = getServletContext(); |
|
121 |
+ String uploadFilePath = context.getRealPath(savePath); |
|
122 |
+ System.out.println("서버상의 실제 디렉토리"); |
|
123 |
+ System.out.println(uploadFilePath); |
|
124 |
+ |
|
125 |
+ MultipartRequest multi = new MultipartRequest(request, uploadFilePath, uploadFileSizeLimit, encType, new DefaultFileRenamePolicy()); |
|
126 |
+ String fileName = multi.getFilesystemName("file"); |
|
127 |
+ |
|
128 |
+/*************************************************************************/ |
|
129 |
+ String writer = multi.getParameter("inputWriter"); |
|
107 | 130 |
if (writer.equals("")) { |
108 | 131 |
writer = "Anonymous"; |
109 | 132 |
} |
110 | 133 |
|
111 |
- String title = request.getParameter("inputTitle"); |
|
112 |
- String contents = request.getParameter("inputContents"); |
|
134 |
+ String title = multi.getParameter("inputTitle"); |
|
135 |
+ String contents = multi.getParameter("inputContents"); |
|
113 | 136 |
Date date = new Date(); |
114 | 137 |
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd"); |
115 | 138 |
String strdate = simpleDate.format(date); |
... | ... | @@ -123,7 +146,7 @@ |
123 | 146 |
int view = 1; |
124 | 147 |
|
125 | 148 |
boolean result = false; |
126 |
- result = dao.setInsert(writer, title, contents, view, inputDate); |
|
149 |
+ result = dao.setInsert(writer, title, contents, view, inputDate, fileName); |
|
127 | 150 |
|
128 | 151 |
if (result) { |
129 | 152 |
out.println("<script>alert('등록 완료')</script>"); |
... | ... | @@ -179,9 +202,70 @@ |
179 | 202 |
out.println("<script>location.href='list.do'</script>"); |
180 | 203 |
} else if (url.indexOf("download.do") != -1) { |
181 | 204 |
|
182 |
- System.out.print("다운로드.두 로 옴"); |
|
183 |
- String filePath = request.getRealPath("/download"); |
|
184 |
- File file = new File(String.valueOf(filePath) + "/" + request.getParameter("file")); |
|
205 |
+ String file1 = request.getParameter("file"); |
|
206 |
+ |
|
207 |
+ request.setCharacterEncoding("UTF-8"); |
|
208 |
+ // 파일 업로드된 경로 |
|
209 |
+ String root = request.getSession().getServletContext().getRealPath("/"); |
|
210 |
+ String savePath = root + "upload"; |
|
211 |
+ // 서버에 실제 저장된 파일명 |
|
212 |
+ String filename = file1; |
|
213 |
+ // 실제 내보낼 파일명 |
|
214 |
+ String orgfilename = file1; |
|
215 |
+ |
|
216 |
+ InputStream in = null; |
|
217 |
+ OutputStream os = null; |
|
218 |
+ File file = null; |
|
219 |
+ boolean skip = false; |
|
220 |
+ String client = ""; |
|
221 |
+ |
|
222 |
+ try{ |
|
223 |
+ // 파일을 읽어 스트림에 담기 |
|
224 |
+ try{ |
|
225 |
+ file = new File(savePath, filename); |
|
226 |
+ in = new FileInputStream(file); |
|
227 |
+ }catch(FileNotFoundException fe){ |
|
228 |
+ skip = true; |
|
229 |
+ } |
|
230 |
+ |
|
231 |
+ client = request.getHeader("User-Agent"); |
|
232 |
+ // 파일 다운로드 헤더 지정 |
|
233 |
+ response.reset() ; |
|
234 |
+ response.setContentType("application/octet-stream"); |
|
235 |
+ response.setHeader("Content-Description", "JSP Generated Data"); |
|
236 |
+ |
|
237 |
+ if(!skip){ |
|
238 |
+ // IE |
|
239 |
+ if(client.indexOf("MSIE") != -1){ |
|
240 |
+ response.setHeader ("Content-Disposition", "attachment; filename="+new String(orgfilename.getBytes("KSC5601"),"ISO8859_1")); |
|
241 |
+ }else{ |
|
242 |
+ // 한글 파일명 처리 |
|
243 |
+ orgfilename = new String(orgfilename.getBytes("utf-8"),"iso-8859-1"); |
|
244 |
+ |
|
245 |
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + orgfilename + "\""); |
|
246 |
+ response.setHeader("Content-Type", "application/octet-stream; charset=utf-8"); |
|
247 |
+ } |
|
248 |
+ |
|
249 |
+ response.setHeader ("Content-Length", ""+file.length() ); |
|
250 |
+ |
|
251 |
+ os = response.getOutputStream(); |
|
252 |
+ byte b[] = new byte[(int)file.length()]; |
|
253 |
+ int leng = 0; |
|
254 |
+ |
|
255 |
+ while( (leng = in.read(b)) > 0 ){ |
|
256 |
+ os.write(b,0,leng); |
|
257 |
+ } |
|
258 |
+ |
|
259 |
+ }else{ |
|
260 |
+ out.println("<script>alert('파일을 찾을 수 없습니다');history.back();</script>"); |
|
261 |
+ } |
|
262 |
+ |
|
263 |
+ in.close(); |
|
264 |
+ os.close(); |
|
265 |
+ |
|
266 |
+ }catch(Exception e){ |
|
267 |
+ e.printStackTrace(); |
|
268 |
+ } |
|
185 | 269 |
|
186 | 270 |
} else if (url.indexOf("replyInsert.do") != -1) { |
187 | 271 |
|
--- src/main/java/model/MemberDAO.java
+++ src/main/java/model/MemberDAO.java
... | ... | @@ -144,12 +144,11 @@ |
144 | 144 |
} |
145 | 145 |
} |
146 | 146 |
|
147 |
- public boolean setInsert(String writer, String title, String contents, int view, String inputDate) { |
|
147 |
+ public boolean setInsert(String writer, String title, String contents, int view, String inputDate, String fileName) { |
|
148 | 148 |
conn = null; |
149 | 149 |
pstmt = null; |
150 | 150 |
rs = null; |
151 | 151 |
boolean result = false; |
152 |
- |
|
153 | 152 |
String query = "insert into user values(?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
154 | 153 |
|
155 | 154 |
try { |
... | ... | @@ -161,7 +160,7 @@ |
161 | 160 |
pstmt.setString(4, contents); |
162 | 161 |
pstmt.setString(5, inputDate); |
163 | 162 |
pstmt.setInt(6, view); |
164 |
- pstmt.setString(7, null); |
|
163 |
+ pstmt.setString(7, fileName); |
|
165 | 164 |
pstmt.setInt(8, 0); /*ÇØ´ç ±ÛÀÇ no¸¦ listNumÀ¸·Î ³Ö±â*/ |
166 | 165 |
pstmt.setInt(9, 0); |
167 | 166 |
pstmt.executeUpdate(); |
--- src/main/webapp/WEB-INF/lib/cos-05Nov2002.jar
Binary file is not shown |
--- src/main/webapp/WEB-INF/lib/cos.jar
+++ src/main/webapp/WEB-INF/lib/cos.jar
Binary file is not shown |
--- src/main/webapp/view/write.jsp
+++ src/main/webapp/view/write.jsp
... | ... | @@ -32,7 +32,7 @@ |
32 | 32 |
<div id="page"> |
33 | 33 |
<h1>This is write Page</h1> |
34 | 34 |
<hr /> |
35 |
- <form action="insert" method="post" id="form" > |
|
35 |
+ <form action="insert" method="post" id="form" enctype="multipart/form-data"> |
|
36 | 36 |
작성자 : <input type="text" name="inputWriter"><br /> |
37 | 37 |
제목 : <input type="text" name="inputTitle"><br /> |
38 | 38 |
내용 : <textarea id="inputContent" name="inputContents"></textarea><br /> |
+++ target/classes/config/FileDownloadHelper.class
Binary file is not shown |
--- target/classes/control/MemberServlet.class
+++ target/classes/control/MemberServlet.class
Binary file is not shown |
--- target/classes/model/MemberDAO.class
+++ target/classes/model/MemberDAO.class
Binary file is not shown |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?