屌丝成长之路——jspsmart+servlet实现图片上传并生成缩略图
屌丝成长之路——jspsmart+servlet实现图片上传并生成缩略图
一、jspsmart简介
jspSmartUpload是一个可免费使用的全功能的文件上传下载组件,适于嵌入执行上传下载操作的JSP文件中,是应用JSP进行B/S程序开发过程中经常使用的上传下载组件,它使用简单,方便。
二、简单步骤
(1)导入jspsmart.jar
(2)编写上传页面
(3)编写servlet类
1.新建组件对象
2.初始化组件
3.设置上传文件类型
4.上载文件
5.获取上载文件
6.判断文件大小、设置保存地址
7.保存图片
8.读入保存图片
9.构造Image对象
10.设置缩略图尺寸以及保存路径
11.绘制图片
12.输出到文件流
13.关闭文件流
(4)配置web.xml
三、代码实现
上传页面:mageLoadTest.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'imageLoadTest.jsp' starting page</title> </head> <body> <form action="imageLoad" method="post" enctype="multipart/form-data" name="imageLoadForm"> 请选择上传的图片 <input type="file" name="file"> <input type="submit" name="Submit" value="上传"> </form> </body> </html>
实现文件上传与生成缩略图的Servlet:mageLoadServlet.jsp
package com.servlet; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ImageLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String allowedFilesList="jpg,gif,jpeg,png";//允许上传文件类型 SmartUpload mySmartUpload=new SmartUpload();//新建一个上传组件 long fileSizeMax=1024*1024*10;//文件上传大小最大值 String ext="";//文件格式 String url="image/";//文件存储目录 mySmartUpload.initialize(this.getServletConfig(),request,response);//初始化上传组件 mySmartUpload.setAllowedFilesList(allowedFilesList);//设置上传类型 try { mySmartUpload.upload();//上载文件 } catch (SmartUploadException e) { e.printStackTrace(); System.out.println("只允许上传"+allowedFilesList+"类型图片"); } com.jspsmart.upload.File myFile=mySmartUpload.getFiles().getFile(0);//获取第一个上载文件 if(myFile.isMissing()){//判断是否有文件 System.out.println("请选择要上传的文件"); }else{ ext=myFile.getFileExt();//获取文件格式 int fileSize=myFile.getSize();//获取文件大小 String saveurl="";//保存地址 if(fileSize<fileSizeMax){ String filename=String.valueOf(Calendar.getInstance().getTimeInMillis());//获取当前毫秒数 saveurl=request.getRealPath("/");//获取网站根目录 saveurl+=url+filename+"."+ext; try { myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);//保存文件 } catch (SmartUploadException e) { e.printStackTrace(); System.out.println("文件保存出错"); } //-----------------------上传完成,开始生成缩略图------------------------- java.io.File file=new java.io.File(saveurl);//读入刚刚上传的文件 String newSaveurl=request.getRealPath("/")+url+filename+"_min."+ext;//缩略图存储路径 Image image=javax.imageio.ImageIO.read(file);//构造Image对象 float tagSize=200;//保存大小 int oldWidth=image.getWidth(null);//原来图片宽度 int oldHeight=image.getHeight(null);//原来图片高度 int width=0; int height=0; float tempdouble=0; if(oldWidth>oldHeight){ tempdouble=oldWidth/tagSize; }else{ tempdouble=oldHeight/tagSize; } width=Math.round(oldWidth/tempdouble); height=Math.round(oldHeight/tempdouble); BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);//图片缓存 bufferedImage.getGraphics().drawImage(image, 0, 0, width, height, null);//绘制缩小后的图 FileOutputStream newImage=new FileOutputStream(newSaveurl);//输出到文件流 JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(newImage); encoder.encode(bufferedImage);//近JPEG编码 newImage.close();//关闭文件流 response.sendRedirect("index.jsp"); }else{ System.out.println("上传文件不能超过"+(fileSizeMax/1024/1024)+"M"); } } } }
配置文件: web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <servlet-name>ImageLoadServlet</servlet-name> <servlet-class>com.servlet.ImageLoadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageLoadServlet</servlet-name> <url-pattern>/imageLoad</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

浙公网安备 33010602011771号