首页 > 代码库 > jspSmartUpload实现图片的批量上传
jspSmartUpload实现图片的批量上传
batchUpload.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"> <style type="text/css"> .tableStyle { border: 1px solid blue; padding: 1px; height: 45px; vertical-align: middle; } .tableStyle .tdStyle { /* 派生选择器+类选择器 */ border: 1px solid green; } </style> </head> <body> <div> <form action="doBatchUpload.jsp" method="post" enctype="multipart/form-data" name="form1"> <table class="tableStyle" align="center" border="1"> <tr> <td class="tdStyle"> 请选择上传文件 <input type="file" name="file1" /> </td> </tr> <tr> <td class="tdStyle"> 请选择上传文件 <input type="file" name="file2" /> </td> </tr> <tr> <td class="tdStyle"> 请选择上传文件 <input type="file" name="file3" /> </td> </tr> <tr> <td class="tdStyle"> <input type="submit" name="submit" value=http://www.mamicode.com/"上传" />>doBatchUpload1.jsp<%@ page language="java" import="java.util.*,com.jspsmart.upload.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href=http://www.mamicode.com/"">>
doBatchUpload2.jsp<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="utf-8"%> <%@ 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"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <% //这个处理文件实现了对图片的压缩 SmartUpload mySmartUpload = new SmartUpload(); long file_size_max = 4000000; String fileName2;//文件名 String ext;//文件扩展名 String testVar; String url = "upload/images/";//应保证在根目录中有此目录的存在 //初始化 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(\"只允许上传.jpg和.gif类型图片文件\");"); out.print("window.location=\"upload.html;\""); out.print("</script>"); } try { for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) { com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); 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;//新的缩略图保存地址 java.awt.Image src = http://www.mamicode.com/javax.imageio.ImageIO.read(file);//构造Image对象>jspSmartUpload实现图片的批量上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。