IT/Jsp

jsp - 이미지 축소, 썸네일 만들기

노마드오브 2018. 11. 21. 20:53

thumbnailForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>썸네일 이미지 폼</title>

</head>

<body>

<h3>썸네일 이미지 폼</h3>

<form action="thumbnail.jsp" method="post" enctype="multipart/form-data">

이미지 파일: <input type="file" name="filename">

<input type="submit" value="전송">

</form>

</body>

</html>




thumbnail.jsp

<%@page import="javax.imageio.ImageIO"%>

<%@page import="java.io.File"%>

<%@page import="java.awt.Graphics2D"%>

<%@page import="java.awt.image.BufferedImage"%>

<%@page import="javax.media.jai.RenderedOp"%>

<%@page import="java.awt.image.renderable.ParameterBlock"%>

<%@page import="javax.media.jai.JAI"%>

<%@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 imagePath = application.getRealPath("/image");

int size = 1024*1024*5;


MultipartRequest multi

= new MultipartRequest(request,

        imagePath,

        size,

        "utf-8",

        new DefaultFileRenamePolicy());


Enumeration enu = multi.getFileNames();

// while (enu.hasMoreElements()) {

//     String filename = enu.nextElement();

// }


String file = (String) enu.nextElement(); // file타입 input태그의 name속성값들을 하나씩 가져옴

String filename = multi.getFilesystemName(file);


ParameterBlock pb = new ParameterBlock();

pb.add(imagePath + "/" + filename);

RenderedOp rOp = JAI.create("fileload", pb);


BufferedImage bi = rOp.getAsBufferedImage();//원본

BufferedImage thumb = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);


Graphics2D g = thumb.createGraphics();

g.drawImage(bi, 0, 0, 100, 100, null);


File thumbnailFile = new File(imagePath, "sm_"+filename);

ImageIO.write(thumb, "jpg", thumbnailFile);

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

-원본이미지-<br>

<img src="../image/<%=filename %>"><br><br>

-썸네일 이미지<br>

<img src="../image/sm_<%=filename %>"><br>

</body>

</html>