cos라이브러리
다운로드
라이브러리에 추가
MultipartRequest
메소드를 확인해보자
https://zewtion.tistory.com/287
fileUploadForm
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fileupload</title>
</head>
<body>
<section id="uploadFormArea">
<form action="fileUpload.jsp" method="post" enctype="multipart/form-data">
<table>
<tr>
<td colspan="2" class="td_title"> 파일 업로드 폼 </td>
</tr>
<tr>
<td><label for="name">올린사람 : </label></td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td><label for="name">제목 : </label></td>
<td><input type="text" name="subject" id="subject"></td>
</tr>
<tr>
<td><label for="fileName1">파일명1 : </label></td>
<td><input type="file" name="fileName1" id="fileName1"></td>
</tr>
<tr>
<td><label for="fileName2">파일명2 : </label></td>
<td><input type="file" name="fileName2" id="fileName2"></td>
</tr>
<tr>
<td colspan="2" align=center><input type="submit" value="전송"> </td>
</tr>
</table>
</form>
</section>
</body>
</html>
fileUpload
더보기
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String uploadPath = request.getRealPath("/upload");
int size = 10*1024*1024;
String name = "";
String subject = "";
String filename1 = "";
String filename2 = "";
String origfilename1 = "";
String origfilename2 = "";
try{
MultipartRequest multi = new MultipartRequest(request, uploadPath, size, "utf-8", new DefaultFileRenamePolicy());
name = multi.getParameter("name");
subject = multi.getParameter("subject");
Enumeration files = multi.getFileNames();
String file1 = (String)files.nextElement();
filename1 = multi.getFilesystemName(file1);
origfilename1 = multi.getOriginalFileName(file1);
String file2 = (String)files.nextElement();
filename2 = multi.getFilesystemName(file2);
origfilename2 = multi.getOriginalFileName(file2);
} catch (Exception e){
e.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="filecheck" action="fileCheck.jsp" method="post">
<input type="hidden" name="name" value="<%=name%>">
<input type="hidden" name="subject" value="<%=subject%>">
<input type="hidden" name="filename1" value="<%=filename1%>">
<input type="hidden" name="filename2" value="<%=filename2%>">
<input type="hidden" name="origfilename1" value="<%=origfilename1%>">
<input type="hidden" name="origfilename2" value="<%=origfilename2%>">
</form>
<a href="#" onclick="javascript:filecheck.submit()">업로드 확인 및 다운로드 페이지 이동</a>
</body>
</html>
fileCheck
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String subject=request.getParameter("subject");
String filename1=request.getParameter("filename1");
String filename2=request.getParameter("filename2");
String origfilename1=request.getParameter("origfilename1");
String origfilename2=request.getParameter("origfilename2");
%>
<html>
<head>
<title>파일 업로드 확인 및 다운로드</title>
</head>
<body>
올린 사람 : <%=name %><br>
제목 : <%=subject %><br>
파일명1 : <a href="file_down.jsp?file_name=<%=filename1 %>"><%=origfilename1 %></a><br>
파일명2 : <a href="file_down.jsp?file_name=<%=filename2 %>"><%=origfilename2 %></a><p>
</body>
</html>
file_down
더보기
<%@page import="java.net.URLEncoder"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<%
String fileName = request.getParameter("file_name");
String savePath = "upload";
ServletContext context = getServletContext();
String sDownloadPath = context.getRealPath(savePath);
String sFilePath = sDownloadPath + "\\" + fileName;
byte b[] = new byte[4096];
FileInputStream in = new FileInputStream(sFilePath);
String sMimeType = getServletContext().getMimeType(sFilePath);
System.out.println("sMimeType>>>" + sMimeType);
if (sMimeType == null)
sMimeType = "application/octet-stream";
response.setContentType(sMimeType);
String agent = request.getHeader("User-Agent");
boolean ieBrowser = (agent.indexOf("MSIE") > -1) || (agent.indexOf("Trident") > -1);
if (ieBrowser) {
fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
} else {
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename= " + fileName);
ServletOutputStream out2 = response.getOutputStream();
int numRead;
while ((numRead = in.read(b, 0, b.length)) != -1) {
out2.write(b, 0, numRead);
}
out2.flush();
out2.close();
in.close();
%>
결과
-------------------------------
'Web > Jsp' 카테고리의 다른 글
자바 UUID 생성 (0) | 2020.06.11 |
---|---|
네비게이션 바에 사진 넣기 (0) | 2020.06.11 |
200610 // 검색 페이징 (0) | 2020.06.10 |
검색창 만들기 (0) | 2020.06.10 |
쿠키를 이용해서 같은 페이지 접속시 조회수 증가 막기 (1) | 2020.06.10 |