728x90
반응형
AAA.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> AAA.jsp </title>
<style type="text/css">
*{ font-size: 20pt; font-weight: bold; }
a{ font-size: 24pt; text-decoration:none; color:blue ; font-family: Comic Sans MS; }
a:hover{ font-size: 30pt; text-decoration:underline; color:green ; font-family: Comic Sans MS; }
</style>
<script type="text/javascript">
function handleFileSelect(){
var files = document.getElementById('file').files[0];
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
//alert(theFile.name);
//원본 var img_view = ['<img src="', e.target.result, '" name="', escape(theFile.name),'" width="450" height="250" alt="tis" />'].join('');
var img_view = ['<img src=', e.target.result + ' width="450" height="250" alt="tis" />'].join('');
//alert(img_view);
//내껏 var img_view = "[<img src="+theFile.name+" width='450' height='250' alt='tis' >]" + join("");
document.getElementById('preview').innerHTML = img_view ;
};
})(files);
reader.readAsDataURL(files);
}//end
function previewFiles() { //새로만든함수
var preview = document.querySelector('#preview');
var files = document.querySelector('input[type=file]').files;
//var files = document.getElementById('file').files[0];
//document.getElementById('preview')
function readAndPreview(file) {
//file.name` 형태의 확장자 규칙에 주의하세요
if ( /\.(jpe?g|png|gif)$/i.test(file.name) ) {
var reader = new FileReader();
//reader.onload = (function(theFile) { 코드기술 } ).(files)
reader.addEventListener("load", function () {
var image = new Image();
image.width = 400;
image.height = 200;
//image.title = file.name;
image.src = this.result;
preview.appendChild( image );
}, false);
reader.readAsDataURL(file);
} //정규식end
}
if (files) {
[].forEach.call(files, readAndPreview);
}
}
</script>
</head>
<body>
<form action="BBB.jsp" method="post" enctype="multipart/form-data">
<input type="hidden" name="num" value="7789"> <br>
제목: <input type="text" name="title" value="snow"> <br>
파일: <input type="file" name="file1" id="file" onchange="handleFileSelect();"> <p>
<input type="submit" value="파일전송">
<input type="reset" value="입력취소">
</form>
<div id="preview"></div>
<hr>
<a href="AAA.jsp">[AAA.jsp]</a>
<a href="index.jsp">[index.jsp]</a>
<a href="guestList.jsp">[guestList.jsp]</a>
</body>
</html>
BBBjsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.io.File" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> BBB.jsp </title>
<style type="text/css">
*{ font-size: 20pt; font-weight: bold; }
a{ font-size: 24pt; text-decoration:none; color:blue ; font-family: Comic Sans MS; }
a:hover{ font-size: 30pt; text-decoration:underline; color:green ; font-family: Comic Sans MS; }
</style>
<script type="text/javascript">
</script>
</head>
<body>
<%
String path = application.getRealPath("storage");
System.out.println(path);
int size=1024*1024*7;
DefaultFileRenamePolicy DFRP = new DefaultFileRenamePolicy();
//MultipartRequest MPR = new MultipartRequest(request내장개체,2경로,3크기,4인코딩,5문서 덮어씌우기);
//MultipartRequest MPR = new MultipartRequest(request,path,size,"UTF-8",DFRP);
MultipartRequest MPR = new MultipartRequest(request,path,size,"UTF-8");
String a = MPR.getParameter("num");
String b = MPR.getParameter("title");
String c = MPR.getFilesystemName("file1");
out.println("mr넘어온번호 = " + a + "<br>");
out.println("mr넘어온제목 = " + b + "<br>");
out.println("mr넘어온파일 = " + c + "<p>");
File ff = MPR.getFile("file1");
long fsize = ff.length();
out.println(c + "파일의 크기는" + fsize + "Byte<br>");
%>
<br>
<a href="BBBdownload.jsp?fname=<%=c%>"><img src="<%=request.getContextPath()%>/storage/<%=c%>" width="300" height="120"></a>
<a href="BBBdownload.jsp?fname=<%=c%>"><img src="./storage/<%=c%>" width="300" height="120"></a><br>
<a href="AAA.jsp">[AAA.jsp]</a>
<a href="index.jsp">[index.jsp]</a>
<a href="guestList.jsp">[guestList.jsp]</a>
</body>
</html>
BBBdownload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.GregorianCalendar" %>
<%@ page import="java.net.URLEncoder" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> BBBdownload.jsp </title>
<style type="text/css">
*{ font-size: 20pt; font-weight: bold; }
a{ font-size: 24pt; text-decoration:none; color:blue ; font-family: Comic Sans MS; }
a:hover{ font-size: 30pt; text-decoration:underline; color:green ; font-family: Comic Sans MS; }
</style>
</head>
<body>
BBBdownload.jsp단독실행금지 <br>
<%
//request.setCharacterEncoding("UTF-8");
String path = application.getRealPath("storage");
String data = request.getParameter("fname");
System.out.println("BBBdownload.jsp문서로 넘어온파일명 = " + data );
File file = new File( path, data) ;
//************response내장개체 헤더정의=>팝업창처럼실행
response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(data, "UTF-8") ); //다운로드정보표시하는 팝업창역할
//원본 response.setHeader("Content-Disposition","attachment;filename="+data); //다운로드정보표시하는 팝업창역할
//축소 response.setHeader("C-D","attach~;fn="+data); //다운로드정보표시하는 팝업창역할
try{
Date dt = new Date( );
Calendar cc = new GregorianCalendar( );
//추상메소드가 있으면 추상클래스/인터페이스
InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();
//표준데이터타입=primitiveType byte,int,double,boolean,char,long
byte[] bt = new byte[(int)file.length()] ; //byte타입은 1바이트타입
is.read(bt,0,bt.length);
os.write(bt);
is.close();
os.close();
}catch(Exception e){ }
%>
</body>
</html>
CCCjsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> AAA.jsp </title>
<style type="text/css">
*{ font-size: 20pt; font-weight: bold; }
a{ font-size: 24pt; text-decoration:none; color:blue ; font-family: Comic Sans MS; }
a:hover{ font-size: 30pt; text-decoration:underline; color:green ; font-family: Comic Sans MS; }
</style>
</head>
<body>
현재문서 CCC.jsp <p>
세션 값 확인 : <%=session.getId()%><hr>
<%
%>
<hr>
<a href="AAA.jsp">[AAA.jsp]</a>
<a href="index.jsp">[index.jsp]</a>
<a href="guestList.jsp">[guestList.jsp]</a>
</body>
</html>
728x90
반응형
'개발중 > JavaServer Pages (JSP)' 카테고리의 다른 글
단일 파일 업로드 (0) | 2020.08.15 |
---|---|
session Login (0) | 2020.08.15 |
화폐단위 표현 (0) | 2020.08.14 |
Login (0) | 2020.08.14 |
session 사용법 (0) | 2020.08.14 |