๋ชฉ์ฐจ
โ โ โ ํ์ผ ์์คํ ๋ฐ ํ์ผ ์ ๋ก๋ โ โ โ
โ โ โ ํ์ผ ์์คํ ๋ฐ ํ์ผ ์ ๋ก๋.txt โ โ โ
โ โ โ ํ์ผ ์์คํ
๋ฐ ํ์ผ ์
๋ก๋.txt โ โ โ
โ java.io.FileInputStream ํด๋์ค
์๋ฐ์์๋ ํ์ผ์ ๋ํ ๋ฐ์ดํธ ๋จ์ ์
์ถ๋ ฅ์ ์ํํ ์ ์๋๋ก ํ๊ธฐ ์ํด
FileInputStream ํด๋์ค์ FileOutputStream ํด๋์ค๋ฅผ ์ ๊ณตํ๊ณ ์๋ค.
โป FileInputStream ํด๋์ค
: ์์คํ
์ ํ์ผ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฝ์ด๋ค์ด๊ธฐ ์ํด ์ฌ์ฉ
โป FileInputStream ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด๋ค์ผ ๋์๋
๋ฐ์ดํฐ์์ค์ธ ํ์ผ๊ณผ ์ง์ ์ฐ๊ฒฐํ์ฌ ์ฌ์ฉํ๋ค.
(์ด ๋, ์ง์ ํ ํ์ผ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ IOExcepion ๋ฐ์)
โป FileInputStream ํด๋์ค๋ Inputstream ํด๋์ค์ ํ์ ํด๋์ค์ด๋ฏ๋ก
InputStream ํด๋์ค์ ๊ธฐ๋ณธ์ ์ธ ๋ฉ์๋๋ฅผ ์์๋ฐ๊ฑฐ๋ ์ฌ์ ์ํ์ฌ ์ฌ์ฉํ๋ค.
(InputStream ํด๋์ค๋ ๋ฐ์ดํธ ๋จ์ ์
์ถ๋ ฅ ๊ธฐ๋ฅ์ ์ต์์ ํด๋์ค์ด๋ฉฐ ์ถ์ ํด๋์ค์ด๋ค.)
- ์์ฑ์
· FileInputStream(File file)
: ์ฃผ์ด์ง File ๊ฐ์ฒด๊ฐ ๊ฐ๋ฆฌํค๋ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฝ๊ธฐ ์ํ
FileInputStream ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
· FileInputStream(FileDescriptor fdObj)
: ์ฃผ์ด์ง FileDescriptor ๊ฐ์ฒด๊ฐ ๊ฐ๋ฆฌํค๋ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฝ๊ธฐ ์ํ
FileInputStream ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
· FileInputStream(String name)
: ์ฃผ์ด์ง ์ด๋ฆ์ด ๊ฐ๋ฆฌํค๋ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฝ๊ธฐ ์ํ
FileInputStream ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
โ java.io.FileOutputStream ํด๋์ค
FileOutputStream ํด๋์ค๋
๋ฐ์ดํฐ๋ฅผ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ ์ฅํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
ํ์ผ๋ช
์ด๋ File ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋๊ฒจ์ค์ผ๋ก์จ
์์คํ
์ ํ์ผ์ ์ง์ ์์ฑํ ์ ์๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก(default) ํ์ผ์ด ์ด๋ฏธ ์กด์ฌํ๋ค๋ฉด ๋ฎ์๊ธ๋ก ์ฒ๋ฆฌ๋๊ธฐ ๋๋ฌธ์
๊ธฐ์กด์ ๋ด์ฉ์ ์ฌ๋ผ์ง๊ฒ ๋๋ค.
โปFileOutputStream ํด๋์ค๋ OutputStream ํด๋์ค์ ํ์ ํด๋์ค์ด๋ฏ๋ก
OutputStream ํด๋์ค์ ๊ธฐ๋ณธ์ ์ธ ๋ฉ์๋๋ฅผ ์์๋ฐ๊ฑฐ๋ ์ฌ์ ์ํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
- ์์ฑ์
· FileOutputStream(File file)
: ์ฃผ์ด์ง File ๊ฐ์ฒด๊ฐ ๊ฐ๋ฆฌํค๋ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฐ๊ธฐ ์ํ
FileOutputStream ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
๊ธฐ์กด์ ํ์ผ์ด ์กด์ฌํ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์ ์ผ๋ก (default) ๊ทธ ๋ด์ฉ์ ์ง์ฐ๊ณ
์๋ก์ด ํ์ผ์ ์์ฑํ๊ฒ ๋๋ค.(๋ฎ์ด์ฐ๊ธฐ)
· FileOutputStream(FileDescriptor fdObj)
: ์ฃผ์ด์ง FileDescriptor ๊ฐ์ฒด๊ฐ ๊ฐ๋ฆฌํค๋ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฐ๊ธฐ ์ํ
FileOutputStream ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
๊ธฐ์กด์ ํ์ผ์ด ์กด์ฌํ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์ ์ผ๋ก (default) ๊ทธ ๋ด์ฉ์ ์ง์ฐ๊ณ
์๋ก์ด ํ์ผ์ ์์ฑํ๊ฒ ๋๋ค.(๋ฎ์ด์ฐ๊ธฐ)
· FileOutputStream(String name)
: ์ฃผ์ด์ง ์ด๋ฆ์ด ๊ฐ๋ฆฌํค๋ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฐ๊ธฐ ์ํ
FileOutputStream ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
๊ธฐ์กด์ ํ์ผ์ด ์กด์ฌํ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์ ์ผ๋ก (default) ๊ทธ ๋ด์ฉ์ ์ง์ฐ๊ณ
์๋ก์ด ํ์ผ์ ์์ฑํ๊ฒ ๋๋ค.(๋ฎ์ด์ฐ๊ธฐ)
· FileOutputStream(String name, boolean append)
: ์ฃผ์ด์ง ์ด๋ฆ์ด ๊ฐ๋ฆฌํค๋ ํ์ผ์ ๋ฐ์ดํธ์คํธ๋ฆผ์ผ๋ก ์ฐ๊ธฐ ์ํ
FileOutputStream ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
๊ธฐ์กด์ ํ์ผ์ด ์กด์ฌํ ๊ฒฝ์ฐ append ๋ก ์ค์ ๋ ๊ฐ์ ๋ฐ๋ผ(true / false)
๊ทธ ๋ด์ฉ์ ์ง์ฐ๊ณ ์๋ก์ด ํ์ผ์ ์์ฑํ๊ฑฐ๋(false) ๊ธฐ์กด์ ๋ด์ฉ์ ์ถ๊ฐํ๋ค.(true)
โ java.io.FileReader ํด๋์ค
FileReader ํด๋์ค๋
ํ์ผ์ ์ ์ฅ๋ ๋ฐ์ดํธ๋ฅผ ์ ๋์ฝ๋ ๋ฌธ์๋ก ๋ณํํ์ฌ ์ฝ์ด๋ค์ด๊ณ ,
ํ์ผ ์ฐ๊ธฐ ํด๋์ค์ธ FileWriter ํด๋์ค๋
์ถ๋ ฅํ ์ ๋์ฝ๋ ๋ฌธ์์ด์ ์์คํ
์ ๋ง๋ ๋ํดํธ ๋ฌธ์ ์ธ์ฝ๋ฉ ๋ฐฉ์์ผ๋ก ๋ณํํ์ฌ
ํ์ผ์ ์ ์ฅํ๋ ์ญํ ์ ์ํํ๋ค.
FileReader ํด๋์ค๋
ํ์ผ์ ์ ์ฅ๋ ๋ฌธ์์ด์ ์ฝ์ด๋ค์ด๋๋ฐ ์ฌ์ฉ๋๋ฉฐ,
์ด ํด๋์ค๋ InputStreamReader ํด๋์ค๋ฅผ ์์ํ ๊ฒ์ผ๋ก
๋ฐ์ดํธ ์คํธ๋ฆผ์ ์ฝ์ด์ ๊ทธ ๋ฐ์ดํธ๋ฅผ ์ ๋์ฝ๋๋ก ๋ํ๋ด๋ ์ ์ํ์
์ ๊ฐ์ผ๋ก
๋ณํํด์ค๋ค.
FileReader ํด๋์ค ์ญ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด๋ค์ผ ์์ค์ธ ํ์ผ๊ณผ ์ง์ ์ฐ๊ฒฐํ์ฌ ์ฌ์ฉ๋๋ฉฐ,
์ง์ ํ ํ์ผ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ FileNotFoundException ์ ๋ฐ์์ํค๊ฒ ๋๋ค.
- ์์ฑ์
·FileReader(File file)
: ์ฃผ์ด์ง File ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ FileReader ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
·FileReader(FileDescriptor fd)
: ์ฃผ์ด์ง FileDescriptor ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ FileReader ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
·FileReader(String fileName)
: ์ฃผ์ด์ง ์ด๋ฆ์ ํ์ผ์ ์ด์ด FileReader ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
โ java.io.FileWriter ํด๋์ค
FileWriter ํด๋์ค๋
ํ์ผ ๋ฌธ์ ์ถ๋ ฅ์คํธ๋ฆผ์ผ๋ก ์ถ๋ ฅํ ๋ฌธ์๋ฅผ ๋ํดํธ ๋ฌธ์ ์ธ์ฝ๋ฉ ๋ฐ์ดํธ๋ก ๋ณํํ์ฌ
ํ์ผ์ ์ ์ฅํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค.
์ด ํด๋์ค๋ OutputStreamWriter ํด๋์ค์ ์๋ธ ํด๋์ค๋ก
์ ๋์ฝ๋๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํํ๋ ๊ธฐ๋ฅ์ ๋ด๋นํ๊ฒ ๋๋ค.
FileOutputStream ํด๋์ค์ ๋ง์ฐฌ๊ฐ์ง๋ก
ํ์ผ๋ช
์ด๋ File ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋๊ฒจ์ค์ผ๋ก์จ
์์คํ
์ ํ์ผ์ ์ง์ ์์ฑํ๊ฒ ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก(default) ํด๋น ํ์ผ์ด ์ด๋ฏธ ์กด์ฌํ ๊ฒฝ์ฐ ๋ฎ์ด์ฐ๊ธฐ ํ๊ฒ ๋๋ค.
- ์์ฑ์
·FileWriter(File file)
: ์ฃผ์ด์ง File ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ FileWriter ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
·FileWriter(FileDescriptor fd)
: ์ฃผ์ด์ง FileDescriptor ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ FileWriter ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
·FileWriter(String fileName)
: ์ฃผ์ด์ง ์ด๋ฆ์ ํ์ผ์ ์ด์ด FileWriter ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
·FileWriter(String fileName, boolean append)
: ์ฃผ์ด์ง ํ์ผ์ append ๊ฐ์ ๋ฐ๋ผ(true / false)
์ฝ๊ธฐ / ์ถ๊ฐ ๋ชจ๋๋ก ์ด์ด FileWriter ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
โป ํ์ผ๊ณผ ๋๋ ํฐ๋ฆฌ ๊ด๋ จ(๊ด๋ฆฌ) ํด๋์ค → java.io.File ํด๋์ค
File ํด๋์ค๋ ํ์ผ ๋ฐ ๋๋ ํฐ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ ์ ์๋๋ก ๊ธฐ๋ฅ์ ์ ๊ณตํด์ฃผ๋ ํด๋์ค๋ก
ํ์ผ์ ๋ณต์ฌ ๋๋ ์ด๋ฆ ๋ณ๊ฒฝ ๋ฑ์ ์กฐ์์ ํ ๊ฒฝ์ฐ ์ฌ์ฉ๋ ๋ฟ
ํ์ผ์ ๋ด์ฉ์ ์
์ถ๋ ฅํ๊ธฐ ์ํ ๋ฉ์๋๋ฅผ ์ ๊ณตํด์ฃผ์ง๋ ์๋๋ค.
์๋ฐ์์๋ ๋ชจ๋ ๋ฐ์ดํฐ์ ์
์ถ๋ ฅ์ ใ์ค.ํธ.๋ฆผใ์ ๊ธฐ๋ฐํ์ฌ ์ํํ๊ธฐ ๋๋ฌธ์
File ํด๋์ค๋ ๋ด๋ถ์ ์ผ๋ก ์ด๋ฌํ ๋ฉ์๋๋ฅผ ๊ตฌํํ ํ์๊ฐ ์๋ ๊ฒ์ด๋ค.
๋จ, File ํด๋์ค์ ์ธ์คํด์ค๋ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ๋ค.
์ฆ, ํ ๋ฒ ์์ฑ๋๋ฉด File ๊ฐ์ฒด์ ์ํด ํํ๋๋ ์ถ์ ๊ฒฝ๋ก๋ช
์ ์ ๋ ๋ณํ์ง ์๋๋ค.
โป ํ์ผ ์
๋ก๋
์น ๋ธ๋ผ์ฐ์ ๋ฅผ ํตํด ํ์ผ์ ์ ์กํ๊ธฐ ์ํด ํผ์ ๊ตฌ์ฑํ ๊ฒฝ์ฐ
- form ํ๊ทธ ์์.. input ํ๊ทธ์ type ์์ฑ์ ์์ฑ๊ฐ์ด
file ์ธ ํ๊ทธ๋ฅผ ํตํด ํ์ผ์ ์ ํํ ์ ์๋ ๋ํ์ฐฝ์ ์ ๊ณตํ๋ค.
- ์ด ๋, method ์ ์์ฑ์ post ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ฉฐ,
enctype ์์ฑ์ ์์ฑ๊ฐ์ ใ"multipart/form-data"ใ๋ก ์ง์ ํ ํผ์ ์ ์กํ ๊ฒฝ์ฐ
- ๋จ, ใ"multipart/form-data"ใ๋ก ์ง์ ํ ํผ์ ์ ์กํ ๊ฒฝ์ฐ
request ๊ฐ์ฒด๋ก ์ป์ด๋ธ ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ผ๋ก ๊ฐ์ ์ป์ด๋ผ ์ ์๋ค.
์ด ๋ง์ ใ<input type="file">ใ๋ก ์ง์ ๋ ํ์ผ์
์๋ฒ ์์ ํน์ ๊ฒฝ๋ก์ ์
๋ก๋ ํ๊ธฐ ์ํด์ ใํน๋ณํ ์ปดํฌ๋ํธใ๊ฐ
ํ์ํ๋ค๋ ์๋ฏธ์ด๋ค.
โป ํน๋ณํ ์ปดํฌ๋ํธ : ใcos.jarใ
- ๋ค์ด๋ก๋ URL → http://www.servlets.com/cos
- ๋ค์ด๋ก๋ ํ์ผ → cos-22.05.zip
[ FileSystemApp01 ]
AppRoot.jsp
<%@page import="java.io.File"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
String appRoot = "/";
appRoot = pageContext.getServletContext().getRealPath(appRoot);
File newFile = new File(appRoot, "data/test.txt");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AppRoot.jsp</title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
</head>
<body>
<div>
<h1>๊ด์ฐฐ</h1>
<hr>
</div>
<div>
<h2>์น ์ดํ๋ฆฌ์ผ์ด์
๋ฃจํธ : <%=appRoot %></h2>
<!--
C:\SpringMVCStudy\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileSystemApp01\
-->
<h2>์์ฑํ ํ์ผ ์์น : <%=newFile %></h2>
<!--
C:\SpringMVCStudy\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileSystemApp01\data\test.txt
-->
</div>
</body>
</html>
[ FileSystemApp02 ]
FileWrite.jsp
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.File"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
/* FileWrite.jsp */
String appRoot = "/";
appRoot = pageContext.getServletContext().getRealPath(appRoot);
// 1. ํ์ผ์ ์ฐ๊ธฐ ์ํด File ๊ฐ์ฒด๋ฅผ ์์ฑ
File newFile = new File(appRoot, "data/test.txt");
// 2. ํ์ผ์ด ์กด์ฌํ ๋๋ ํฐ๋ฆฌ๊ฐ ์๋ ๊ฒฝ์ฐ๋ผ๋ฉด...
if ( !newFile.getParentFile().exists() )
{
// ํ์ผ์ด ์์นํ ๊ณณ๊น์ง์ ๋๋ ํฐ๋ฆฌ๋ฅผ ์์ฑ
newFile.getParentFile().mkdirs();
}
// 3-1. ํ์ผ์ ์ฐ๊ธฐ ์ํ FileWriter ๊ฐ์ฒด ์์ฑ
// → ์ถ๋ ฅ ์คํธ๋ฆผ์ ํ์ฉํ์ฌ ํ์ผ์ ๋ด์ฉ์ ๊ธฐ๋ก
FileWriter fw = new FileWriter(newFile);
// 3-2. FileWriter ๊ฐ์ฒด๋ฅผ ์ข ๋ ํธํ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด PrintWriter ๋ก ๊ฐ์ธ์ค → ์ฌ๊ณผ์ฅ์น
PrintWriter pw = new PrintWriter(fw);
// 4. ์ค์ง์ ์ผ๋ก ํ์ผ์ ๋ด์ฉ์ ์(๊ธฐ๋ก)
pw.println("ํ
์คํธ์ด๋ค...");
pw.println("ํ์ธํด๋ณด์...");
// 5-1. PrintWriter ๋ฆฌ์์ค ๋ฐ๋ฉ(๋ซ์์ค)
pw.close();
// 5-2. FileWriter ๋ฆฌ์์ค ๋ฐ๋ฉ(๋ซ์์ค)
fw.close();
%>
FileRead.jsp
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.File"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
/* FileRead.jsp */
String appRoot = "/";
appRoot = pageContext.getServletContext().getRealPath(appRoot);
// 1. ํ์ผ์ ์ฝ๊ธฐ ์ํด File ๊ฐ์ฒด๋ฅผ ์์ฑ
File newFile = new File(appRoot, "data/test.txt");
// 2. ํ์ผ์ด ์กด์ฌํ๋์ง์ ์ฌ๋ถ ํ์ธ
// → ์ง์ ๋ ๊ฒฝ๋ก์ ํ์ผ์ด ์กด์ฌํ๋์ง์ ์ฌ๋ถ๋ฅผ ํ์ธํ์ฌ
// ์กด์ฌํ๋ ๊ฒฝ์ฐ์๋ง ์ฒ๋ฆฌํ์ฌ FileNotFoundException ๋ฐฉ์ง
if (newFile.exists())
{
// 3.1 - ํ์ผ์ ์ฝ๊ธฐ ์ํ FileReader ๊ฐ์ฒด ์์ฑ
FileReader fr = new FileReader(newFile);
// 3-2. FileReader ๊ฐ์ฒด๋ฅผ ์ข ๋ ํธํ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด
// BufferedReader ๋ก ๊ฐ์ธ๊ธฐ
BufferedReader br = new BufferedReader(fr);
// 4. ์ค์ง์ ์ผ๋ก ํ์ผ์ ๋ด์ฉ์ ์ฝ์ด๋
String readData;
while( (readData=br.readLine()) != null ) // ๋ผ์ธ๋ณ๋ก ์ฝ์ด๋ค์ด๋ ๊ธฐ๋ฅ ๋ฐ๋ณต
{
out.println(readData + "<br>");
}
// 5-1. BufferedReader ๋ฆฌ์์ค ๋ฐ๋ฉ(๋ซ์์ค)
br.close();
// 5-2. FileReader ๋ฆฌ์์ค ๋ฐ๋ฉ(๋ซ์์ค)
fr.close();
}
else // ์ง์ ๋ ๊ฒฝ๋ก์ ํ์ผ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
{
out.println("์ ์์ ์ผ๋ก ํ์ผ์ ์ฝ์ด๋ค์ผ ์ ์์ต๋๋ค.");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>FileRead.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
</body>
</html>
[ FileSystemApp03 ]
ObjectWrite.jsp
<%@page import="java.io.ObjectOutputStream"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.util.Hashtable"%>
<%@page import="java.io.File"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
/* ObjectWrite.jsp */
String appRoot = "/";
appRoot = pageContext.getServletContext().getRealPath(appRoot);
// ํ
์คํธ
System.out.println(appRoot);
// ํ์ผ ๊ฐ์ฒด ์์ฑ
File newFile = new File(appRoot, "objData/data.ser");
//-- ๊ฐ์ฒด ์ง๋ ฌํ
// ํ์ผ์ด ์กด์ฌํ ๋๋ ํฐ๋ฆฌ๊ฐ ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ๋
// ํ์ผ์ด ์์นํ ๊ฒฝ๋ก๊น์ง์ ๋๋ ํฐ๋ฆฌ๋ค์ ์์ฑํด์ค๋ค.
if ( !newFile.getParentFile().exists() )
{
newFile.getParentFile().mkdirs();
}
// ํ์ผ์ ๋ฃ๊ธฐ ์ํ Hashtable ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋ฐ์ดํฐ์ ํฌํจ์ํจ๋ค.
Hashtable<String, String> h = new Hashtable<String, String>();
h.put("key1", "์ฌ๊ณผ");
h.put("key2", "์๋ฐ");
// ํ์ผ์ ๋ด๋ณด๋ด๊ธฐ ์ํ ์คํธ๋ฆผ ๊ตฌ์ฑ
FileOutputStream fos = new FileOutputStream(newFile);
// Object ๋ฅผ ํ์ผ์ ๊ธฐ๋กํ๊ธฐ ์ํด ObjectOutputStream ๊ฐ์ฒด ์์ฑ
// → ์์ฑํ ๊ฐ์ฒด๋ก FileOutputStream ๊ฐ์ฒด ๊ฐ์ธ๊ธฐ
ObjectOutputStream oos = new ObjectOutputStream(fos);
// ObjectOutputStream ์ ์ด์ฉํ์ฌ ํ์ผ์ Object ๋ฅผ ๊ธฐ๋ก
oos.writeObject(h);
// ๋ฆฌ์์ค ๋ฐ๋ฉ
oos.close();
fos.close();
// ์๋ ๋ณด์ฌ์ฃผ๋ ํ
ํ๋ฆฟ ์ ๊ฑฐ
%>
ObjectRead.jsp
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Hashtable"%>
<%@page import="java.io.ObjectInputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
/* ObjectRead.jsp */
String appRoot = "/";
appRoot = pageContext.getServletContext().getRealPath(appRoot);
File newFile = new File(appRoot, "objData/data.ser");
// ํ์ผ์ด ์กด์ฌํ๋์ง ํ์ธํ ํ ์ฒ๋ฆฌ
if ( newFile.exists() )
{
// ํ์ผ์ ์ฝ์ด๋ค์ด๊ธฐ ์ํด FileInputStream ๊ฐ์ฒด ์์ฑ
FileInputStream fis = new FileInputStream(newFile);
// ํ์ผ์์ Object ๋ฅผ ์ฝ์ด๋ค์ด๊ธฐ ์ํด ObjectInputStream ๊ฐ์ฒด ์์ฑ
ObjectInputStream ois = new ObjectInputStream(fis);
// ObjectInputStream ์ ์ด์ฉํ์ฌ ํ์ผ๋ก๋ถํฐ Object ์ฝ์ด์ค๊ธฐ
// ์ฝ์ด์จ Object ๋ฅผ ์๋์ ํ(ํ์
)์ผ๋ก ์บ์คํ
Hashtable h = (Hashtable)ois.readObject();
ois.close();
fis.close();
Enumeration e = h.keys();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = (String)h.get(key);
%>
<%=key %> ์ ๊ฐ์ <%=value %> ์
๋๋ค. <br>
<%
}
}
else
{
out.println("ํด๋น ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค~!!!");
}
// ์๋ ๋ณด์ฌ์ฃผ๊ธฐ ์ํ ํ
ํ๋ฆฟ ๋ชจ๋ ์ ๊ฑฐ
%>
[ FileSystemApp04 ]
Send.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send.jsp</title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
</head>
<body>
<div>
<h1>ํ์ผ ์์คํ
๋ฐ ํ์ผ ์
๋ก๋</h1>
<hr />
</div>
<div>
<!-- ใenctype="multipart/form-data"ใ : ํ์ผ์ ๋ฌผ๋ฆฌ์ ์ผ๋ก ์
๋ก๋ ํ๊ธฐ ์ํ ์์ฑ -->
<!-- ใmethod="post"ใ : ํ์ผ์ ๋ฌผ๋ฆฌ์ ์ผ๋ก ์
๋ก๋ ํ๊ธฐ ์ํ ์์ฒญ ๋ฐ ์ ์ก ๋ฐฉ์ -->
<form action="Receive.jsp" method="post" enctype="multipart/form-data">
์ด๋ฆ : <input type="text" name="name"/>
<br>
<!-- ํ์ผ ์
๋ก๋ ๋ํ์ฐฝ ๊ตฌ์ฑ -->
ํ์ผ : <input type="file" name="upload"/>
<br>
<!-- submit ์ก์
์ฒ๋ฆฌ -->
<button type="submit" class="btn">์ ์ก</button>
</form>
</div>
<!--
โป ใSend.jspใ์ form ์๋ฆฌ๋จธํธ enctype ์์ฑ์ ์์ฑ๊ฐ์
ใenctype="multipart/form-dataใ์ ๊ฐ์ด ๊ตฌ์ฑํ ๊ฒฝ์ฐ
form ์ ๋ด์ฉ์ ๋ฐ์ด๋๋ฆฌ ๊ฐ์ ํํ๋ก ์ ์กํ๋ค๋ ์๋ฏธ๋ก ํ์ฉ๋๋ฉฐ,
์ด์ ๊ฐ์ ๋ฐฉ์์ ํตํด ์ ์กํ ๊ฒฝ์ฐ ์์ ๋ ๋ฐ์ดํฐ๋
์ด๋ฆ : null
ํ์ผ : null
๋ก ํ์ธ๋๋ค.
==> ์ด๋, ๋ฐ์ด๋๋ฆฌ ๊ฐ์ getParameter() ๋ฅผ ํตํด ์์ ํ ์ ์๋ค๋ ์๋ฏธ์ด๋ค.
-->
</body>
</html>
Receive.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
// ์ด์ ํ์ด์ง(Send.jsp)๋ก๋ถํฐ ๋์ด์จ ๋ฐ์ดํฐ ์์
// → name, upload
String name = request.getParameter("name");
String upload = request.getParameter("upload");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Receive.jsp</title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
</head>
<body>
<div>
<!-- ์ด๋ฆ : ์ด์ฉ๊ตฌ<br> -->
์ด๋ฆ : <%=name %><br>
<!-- ํ์ผ : ์ ์ฉ๊ตฌ<br> -->
ํ์ผ : <%=upload %><br>
</div>
</body>
</html>
[ FileSystemApp05 ]
UploadForm.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>UploadForm.jsp</title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
</head>
<body>
<div>
<h1>ํ์ผ ์
๋ ฅ ํผ</h1>
<hr />
</div>
<div>
<form action="Receive.jsp" method="post" enctype="multipart/form-data">
file : <input type="file" name="file" size="50" />
<br>
text : <input type="text" name="data" size="30" />
<br>
<input type="submit" value="upload" />
</form>
</div>
</body>
</html>
Receive.jsp
<%@page import="java.io.DataInputStream"%>
<%@page import="java.util.Enumeration"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String cp = request.getContextPath();
%>
<%
/* Receive.jsp */
String contentType = request.getContentType();
Enumeration e = request.getHeaderNames();
out.println("[[ ๋ธ๋ผ์ฐ์ ๋ก๋ถํฐ ๋ ์์ค๋ ํค๋ ์ ๋ณด๋ค ]]<br>");
while(e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = request.getHeader(key);
out.println(key + " " + value + "<br>");
}
out.println("[[ request ๋ก ๋์ด์ค๋ ๋ฐ์ดํฐ๋ค ]]<br>");
DataInputStream dis = new DataInputStream(request.getInputStream());
String line = null;
while((line=dis.readLine()) != null)
{
//out.println(new String(line.getBytes("UTF-8"), "UTF-8") + "<br>");
out.println(new String(line.getBytes("ISO-8859-1"), "UTF-8") + "<br>");
}
// ์๋ ๋ณด์ฌ์ฃผ๋ ํ
ํ๋ฆฟ ๋ชจ๋ ์ ๊ฑฐ
%>
'SpringMVCStudy > FileSystemApp' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ FileSystemApp 06 ~ 08 (0) | 2024.01.30 |
---|