使用jspSmartUpload组件实现图片的上传
upload.html页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>选择上传的图片</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="uploadImage.jsp" method="post" enctype="multipart/form-data" name="form1"> <table border="1" align="center" cellpadding="1" cellspacing="1"> <tr> <td height="45" align="center" valign="middle"> 请选择上传的图片 <input type="file" name="file" /> </td> </tr> <tr> <td align="center"> <input type="submit" name="submit" value="上传" /> </td> </tr> </table> </form> </body> </html>
uploadImage.jsp文件
<%@ page language="java" pageEncoding="utf-8" import="java.util.*,java.io.*"%> <%@ page import="com.jspsmart.upload.SmartUpload"%> <%@ page import="javax.servlet.jsp.tagext.TryCatchFinally"%> <%@ page import="javax.imageio.ImageIO"%> <%@ page import="java.awt.image.BufferedImage"%> <%@ page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%> <%@ page import="com.sun.image.codec.jpeg.JPEGCodec"%> <%@ page import="javax.servlet.*"%> <%@ page import="javax.servlet.http.*"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <% SmartUpload mySmartUpload = new SmartUpload();//实例SmartUpload对象 long file_size_max = 4000000; String fileName2;//文件名 String ext;//文件扩展名 String url = "upload/images/";//应保证在WebRoot目录下有此目录的存在 //初始化 mySmartUpload.initialize(pageContext); //只允许上载此类文件 try { //支持上载文件的后缀名 //mySmartUpload.setAllowedFilesList("jpg,gif"); //mySmartUpload.setAllowedFilesList("jpg,gif,jpeg,png"); //不支持指定的后缀 mySmartUpload.setDeniedFilesList("exe"); //上载文件 // mySmartUpload.upload();//不指定编码的upload()方法 mySmartUpload.upload("utf-8");//指定编码的upload()方法 } catch (Exception e) { out.print("<script type=\"text/javascript\">"); out.print("window.alert(\"文件格式不符\");"); out.print("window.location=\"upload.html;\""); out.print("</script>"); } try { com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0); if (myFile.isMissing()) {//如果没有拿到文件,提示 out.print("<script type=\"text/javascript\">"); out.print("window.alert(\"请先选择要上传的文件\");"); out.print("window.location=\"upload.html;\""); out.print("</script>"); } else { String myFileName = myFile.getFileName();//取得上载的文件的文件名 ext = myFile.getFileExt();//取得后缀名 if (!(ext.length() > 0)) { out.println("**************myFileName的文件名是:" + myFileName); } int file_size = myFile.getSize();//取得文件的大小 String saveUrl = "";//文件保存路径 if (file_size < file_size_max) { //更改文件名,取得当前上传时间的毫秒数值 Calendar calendar = Calendar.getInstance(); String fileName = String.valueOf(calendar.getTimeInMillis());//设置新的文件名 saveUrl += fileName + "." + ext; myFile.saveAs(saveUrl, mySmartUpload.SAVE_PHYSICAL);//保存文件 //上传完成,开始生成缩略图 java.io.File file = new java.io.File(saveUrl);//读入刚才上传的文件 out.println("ext = " + ext); String newUrl = request.getRealPath("/") + url + fileName + "_min." + ext;//新的缩略图保存地址 System.out.println(newUrl); java.awt.Image src = javax.imageio.ImageIO.read(file);//构造Image对象 float tagSize = 200; int old_w = src.getWidth(null);//得到原图宽 int old_h = src.getHeight(null);//得到原图高 int new_w = 0; int new_h = 0; int tempSize;//设置临时大小 float tempDouble; if (old_w > old_h) { tempDouble = old_w / tagSize; } else { tempDouble = old_h / tagSize; } new_w = Math.round(old_w / tempDouble);//设置新的宽度 new_h = Math.round(old_h / tempDouble);//设置新的高度 BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null);//绘制缩小后的图 FileOutputStream newImage = new FileOutputStream(newUrl);//输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImage); encoder.encode(tag);//近JPEG编码 newImage.close(); } else { out.print("<script type=\"text/javascript\">"); out.print("window.alert(\"上传文件大小不能超过\"+(file_size_max/1000)+\"K\");"); out.print("window.location=\"upload.html;\""); out.print("</script>"); } } } catch (Exception e) { e.printStackTrace(); } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>处理上传图片的JSP</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。