๋ชฉ์ฐจ
[ FileSystemApp06 ]
Test.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>Test.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1>ํ์ผ ์
๋ก๋ - ๋จ์ผ ํ์ผ ์
๋ก๋</h1>
<hr>
</div>
<div>
<form action="Test_ok.jsp" method="post" enctype="multipart/form-data">
์์ฑ์ : <input type="text" name="userName" >
<br>
์ ๋ชฉ : <input type="text" name="subject" >
<br>
ํ์ผ : <input type="file" name="uploadFile" >
<br>
<button type="submit" class="btn">ํ์ผ ์ฌ๋ฆฌ๊ธฐ</button>
</form>
</div>
</body>
</html>
Test_ok.jsp
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@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();
%>
<%
// Test_ok.jsp
//String root = request.getRealPath("/"); //-- ์์ ๋ฐฉ์
String root = pageContext.getServletContext().getRealPath("/");
//String savePath = root + "\\" + "pds" + "\\" + "saveFile";
String savePath = root + "pds" + "\\" + "saveFile";
File dir = new File(savePath);
// ํ
์คํธ
//System.out.println(savePath);
//--==>>
// C:\SpringMVCStudy\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileSystemApp06\pds\saveFile
if (!dir.exists())
dir.mkdirs();
String encType = "UTF-8"; //-- ์ธ์ฝ๋ฉ ๋ฐฉ์
int maxFileSize = 5*1024*1024; //-- ์ต๋ ์
๋ก๋ ํฌ๊ธฐ(5MB)
try
{
MultipartRequest multi = null;
multi = new MultipartRequest(request, savePath, maxFileSize, encType, new DefaultFileRenamePolicy());
out.println("์์ฑ์ : " + multi.getParameter("userName") + "<br>");
out.println("์ ๋ชฉ : " + multi.getParameter("subject") + "<br>");
out.println("์๋ฒ์ ์ ์ฅ๋ ํ์ผ๋ช
: " + multi.getFilesystemName("uploadFile") + "<br>");
out.println("์
๋ก๋ํ ํ์ผ๋ช
: " + multi.getOriginalFileName("uploadFile") + "<br>");
out.println("ํ์ผ ํ์
: " + multi.getContentType("uploadFile")+ "<br>");
File file = multi.getFile("uploadFile");
if (file != null)
{
out.println("ํ์ผ ํฌ๊ธฐ " + file.length() + "<br>");
}
}
catch(Exception e)
{
System.out.print(e.toString());
}
// ์๋ ๋ณด์ฌ์ฃผ๋ ํ
ํ๋ฆฟ ๋ชจ๋ ์ ๊ฑฐ
%>
[ FileSystemApp07 ]
Write.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>Write.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1>ํ์ผ ์
๋ก๋ - ๋จ์ผํ์ผ์
๋ก๋ ๋ฐ ๋ค์ด๋ก๋</h1>
<hr>
</div>
<div>
<form action="Write_ok.jsp" method="post" enctype="multipart/form-data">
์์ฑ์ : <input type="text" name="userName"><br>
์ ๋ชฉ : <input type="text" name="subject"><br>
ํ์ผ : <input type="file" name="uploadFile"><br>
<br>
<button type="submit" class="btn">ํ์ผ์ฌ๋ฆฌ๊ธฐ</button>
</form>
</div>
</body>
</html>
Write_ok.jsp
<%@page import="org.apache.tomcat.util.http.fileupload.UploadContext"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@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();
%>
<%
// Write_ok.jsp
// โ ์ฃผ์ ์์ฑ๊ฐ๋ค ์ค๋น
// โ -1. ๊ฒฝ๋ก
String root = "/";
root = pageContext.getServletContext().getRealPath(root);
String savePath = root + "pds" + File.separator + "saveFile";
// "\\"
// โ -2. ์ธ์ฝ๋ฉ ๋ฐฉ์
String encType = "UTF-8";
// โ -3. ์ต๋ํ์ผ์ฉ๋
int maxFileSize = 5 * 1024 * 1024;
// โก ์์์ ์ค์ ํ ๊ฒฝ๋ก ์ ๋๋ ํฐ๋ฆฌ๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด... ์์ฑํ๋ค.
File dir = new File(savePath);
if(!dir.exists())
dir.mkdirs();
// โข MultipartRequest ๊ฐ์ฒด ๊ตฌ์ฑ
MultipartRequest req = null;
String urlFile = "";
try
{
// โป ์ธ์๋ฆฌ์คํธ
// - request์์ฒญ๊ฐ์ฒด, ํ์ผ์ ์ฅ๊ฒฝ๋ก, ํ์ผ์ต๋ํฌ๊ธฐ, ์ธ์ฝ๋ฉ๋ฐฉ์, ์ค๋ณตํ์ผ๋ช
์ฒ๋ฆฌ์ ์ฑ
req = new MultipartRequest(request, savePath, maxFileSize, encType, new DefaultFileRenamePolicy());
// โฃ ๊ตฌ์ฑ๋ multipartRequest ๊ฐ์ฒด๋ก๋ถํฐ ํ์ํ ๊ฐ ์ป์ด๋ด๊ธฐ
out.println("์์ฑ์ : " + req.getParameter("userName") + "<br>");
out.println("์ ๋ชฉ : " + req.getParameter("subject") + "<br>");
// - getFilesystemName()
out.println("์๋ฒ์ ์ ์ฅ๋ ํ์ผ๋ช
: " + req.getFilesystemName("uploadFile") + "<br>");
// - getOriginalFileName()
out.println("์
๋ก๋ํ ํ์ผ๋ช
: " + req.getOriginalFileName("uploadFile")+ "<br>");
// - getContentType
out.println("ํ์ผ ํ์
: " + req.getContentType("uploadFile")+ "<br>");
// - getFile()
File f = req.getFile("uploadFile");
if (f != null)
{
// - length()
out.println("ํ์ผ ํฌ๊ธฐ : " + f.length() + "Bytes.<br>");
}
// โค ๋ค์ด๋ก๋ ๊ธฐ๋ฅ์ ์ํํ๊ธฐ ์ํ ์์ฑ์ get ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ
urlFile = "Download.jsp?saveFileName=" + req.getFilesystemName("uploadFile");
//-- ์์ฑ ๊ตฌ์ฑ 1 → ์๋ฒ์ ์ ์ฅ๋ ํ์ผ์ ์ด๋ฆ
urlFile += "&originalFileName=" + req.getOriginalFileName("uploadFile");
//-- ์์ฑ ๊ตฌ์ฑ 2 → ์ค์ ์
๋ก๋ํ ํ์ผ์ ์ด๋ฆ
}
catch(Exception e)
{
System.out.println(e.toString());
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Write_ok.jsp </title>
<link rel="stylesheet" type="text/css" href="<%=cp %>/css/main.css">
</head>
<body>
<div>
<a href="<%=urlFile%>">ํ์ผ ๋ค์ด๋ก๋</a>
</div>
</body>
</html>
Download.jsp
<%@page import="com.test.util.FileManager"%>
<%@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();
%>
<%
// Download.jsp
String root = "/";
root = pageContext.getServletContext().getRealPath(root);
String savePath = root + "pds" + File.separator + "saveFile";
String saveFileName = request.getParameter("saveFileName");
String originalFileName = request.getParameter("originalFileName");
// check~!!
// ํ์ผ ๋ค์ด๋ก๋
out.clear(); //-- ๊ธฐ์กด ์ถ๋ ฅ ์คํธ๋ฆผ ํด๋ฆฌ์ด~
boolean falg = FileManager.doFileDownload(saveFileName, originalFileName, savePath, response);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
</body>
</html>
FileManager.java
/*=====================
FileManager.java
======================*/
package com.test.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
public class FileManager
{
// ํ์ผ ๋ค์ด๋ก๋
//-- ๋ค์ด๋ก๋ํ ํ์ผ์ด ์ ์์ ์ผ๋ก ์กด์ฌํ ๊ฒฝ์ฐ true ๋ฐํ
// ํ์ผ์ด ์กด์ฌํ์ง ์์ ์ ์์ ์ธ ์ฒ๋ฆฌ๊ฐ ์ด๋ฃจ์ด์ง์ง ์์ ๊ฒฝ์ฐ false ๋ฐํ
// - saveFileName : ์๋ฒ์ ์ ์ฅ๋ ํ์ผ์ ์ด๋ฆ
// - originalFileName : ํด๋ผ์ด์ธํธ๊ฐ ์
๋ก๋ํ ํ์ผ์ ์ด๋ฆ
// - path : ์๋ฒ์ ์ ์ฅ๋ ๊ฒฝ๋ก
// - response : HttpServletResponse ์๋ต๊ฐ์ฒด
public static boolean doFileDownload(String saveFileName, String originalFileName, String path, HttpServletResponse response)
{
// ์ํฌ์คํ์ด์ค ~ ๋ฌผ๋ฆฌ์ ์ธ ์ค์ ํ์ผ์ด ์กด์ฌํ๋ ์๋ฒ ๊ฒฝ๋ก → ์ ์ฒด ๊ฒฝ๋ก
String loadDir = path + File.separator + saveFileName;
// ํ์ธ
System.out.println(loadDir);
try
{
if (originalFileName == null || originalFileName.equals(""))
{
originalFileName = saveFileName;
}
// new String(originalFileName.getBytes("EUC-KR"), "ISO-8859-1");
originalFileName= new String(originalFileName.getBytes("EUC-KR"), "8859_1");
//} catch (Exception e)
} catch (UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
try
{
File file = new File(loadDir);
if (file.exists())
{
byte[] readByte = new byte[4*1024]; //-- 4096 byte == 4 kb
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + originalFileName);
// ์๋ฒ์ ๋ฌผ๋ฆฌ์ ์ธ ํ์ผ์ ์ ์ฒด ๊ฒฝ๋ก๋ก FileInputStream ๊ฐ์ฒด ์์ฑ
// ์ด ๊ฐ์ฒด๋ฅผ BufferedInputStream ๊ฐ์ฒด๋ก ๊ฐ์ธ๋ ์ฒ๋ฆฌ
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
OutputStream os = response.getOutputStream();
int read;
while ( (read=fis.read(readByte, 0, 4096)) != -1 )
{
os.write(readByte, 0, read);
}
// ๋ฒํผ์ ๋ด์ฉ์ ๋ฐ์ด๋ด๊ธฐ
os.flush();
// ๋ฆฌ์์ค ๋ฐ๋ฉํ๊ธฐ
os.close();
fis.close();
// ํ์ผ์ด ์กด์ฌํ๋ ์ํฉ์ด๋ฏ๋ก true ๋ฐํ
return true;
}
} catch (Exception e)
{
System.out.println(e.toString());
}
// ํ์ผ์ด ์กด์ฌํ์ง ์์ ์ ์์ ์ธ ์ฒ๋ฆฌ๊ฐ ์ด๋ฃจ์ด์ง์ง ์๋ ์ํฉ์ด๋ฏ๋ก false๋ฐํ
return false;
}// end doFileDownload()
// ์ค์ ์๋ฒ์ ํ์ผ ์ญ์ (์ ๊ฑฐ)
public static void doFileDelete(String fileName, String path)
{
try
{
File file = null;
String fullFileName = path + File.separator + fileName;
file = new File(fullFileName);
if (file.exists())
{
file.delete();
}
} catch (Exception e)
{
System.out.println(e.toString());
}
}
}
[ FileSystemApp08 ]
Test.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>Test.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1>ํ์ผ์
๋ก๋ - ๋ค์ค ํ์ผ ์
๋ก๋</h1>
<hr>
</div>
<div>
<form action="Test_ok.jsp" method="post" enctype="multipart/form-data">
์์ฑ์ : <input type="text" name="userName"><br>
์ ๋ชฉ: <input type="text" name="subject"><br>
ํ์ผ๋ช
: <input type="file" name="uploadFile1"><br>
ํ์ผ๋ช
: <input type="file" name="uploadFile2"><br>
<br>
<input type="submit" value="ํ์ผ์
๋ก๋">
</form>
</div>
</body>
</html>
Test_ok.jsp
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@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();
%>
<%
// Test_ok.jsp
String root = "/";
root = pageContext.getServletContext().getRealPath(root);
String savePath = root + "pds" + File.separator + "saveFile";
// ํ์ธ
System.out.println(savePath);
File dir = new File(savePath);
if (!dir.exists())
dir.mkdirs();
String encType = "UTF-8";
int maxFileSize = 5*1024*1024;
MultipartRequest req = null;
try
{
req = new MultipartRequest(request, savePath, maxFileSize, encType, new DefaultFileRenamePolicy());
out.println("์์ฑ์ : " + req.getParameter("userName") + "<br>");
out.println("์ ๋ชฉ : " + req.getParameter("subject") + "<br>");
out.println("*****************************************************<br>");
out.println("์
๋ก๋๋ ํ์ผ<br>");
// check~!!!
// โป ๋ค์ค ํ์ผ ์
๋ก๋ ๊ตฌ์กฐ์์ ์ฒดํฌํด์ผ ํ ์ฌํญ
// ใMultipartRequestใ ๊ฐ์ฒด์ ใgetFileNames()ใ
// → ์์ฒญ์ผ๋ก ๋์ด์จ ํ์ผ๋ค์ ์ด๋ฆ์ Enumeration ํ์
์ผ๋ก ๋ฐํํ๋ค.
Enumeration files = req.getFileNames();
// hasMoreElements() / nextElement()
while (files.hasMoreElements())
{
String name = (String)files.nextElement();
if (req.getFilesystemName(name) != null)
{
out.println("์๋ฒ์ ์ ์ฅ๋ ํ์ผ๋ช
: " + req.getFilesystemName(name) + "<br>");
out.println("์
๋ก๋ํ ์ค์ ํ์ผ๋ช
: " + req.getOriginalFileName(name) + "<br>");
out.println("ํ์ผ ํ์
(์ ํ) : " + req.getContentType(name) + "<br>");
File f = req.getFile(name);
if (f != null)
{
out.println("ํ์ผ ํฌ๊ธฐ(Bytes) : " + f.length() + "<br>");
}
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
%>
'SpringMVCStudy > FileSystemApp' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ FileSystemApp 01 ~ 05 (0) | 2024.01.29 |
---|