码迷,mamicode.com
首页 > Web开发 > 详细

jspSmartUpload实现图片的批量上传

时间:2014-12-06 19:32:43      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   os   sp   for   

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="上传" />
						</td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>
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="<%=basePath%>">
		<title>处理批上传页面</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">
		<%
			//这个处理文件没有对图片进行压缩
			SmartUpload smartUpload = new SmartUpload();
			smartUpload.initialize(pageContext);
			int count = 0;
			String ext = null;
			float file_size = 0f;
			long file_size_max = 4000000;
			String url = "uploadfile/images/";//应保证在根目录中有此目录的存在
			out.println("文件的个数是:" + smartUpload.getFiles().getCount());
			for (int i = 0; i < smartUpload.getFiles().getCount(); i++) {
				com.jspsmart.upload.File myFile = smartUpload.getFiles().getFile(i);
				//String fileName = myFile.getFileName(); //获取源文件名
				//count = smartUpload.save();
				file_size = myFile.getSize();
				smartUpload.upload("utf-8");//指定编码的upload()方法
				if (myFile.isMissing()) {
		%>
		<script type="text/javascript">
window.alert("请先选择要上传的文件");
window.location = " batchUpload.html";
</script>
		<%
			} else {
					String myFileName = myFile.getFileName();//取得上载的文件的文件名
					ext = myFile.getFileExt();//取得后缀名
					if (!(ext.length() > 0)) {
						out.println("**************myFileName的文件名是:" + myFileName);
					}
					String saveUrl = "";//文件保存路径
					if (file_size < file_size_max) {
						//更改文件名,取得当前上传时间的毫秒数值
						Calendar calendar = Calendar.getInstance();
						String fileName = String.valueOf(calendar.getTimeInMillis());//设置新的文件名
						saveUrl += fileName + "." + ext;
						myFile.saveAs(saveUrl, smartUpload.SAVE_PHYSICAL);
						out.println(saveUrl);
						out.println(file_size);
					}
				}
			}
		%>
	</head>
	<body>
	</body>
</html>

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 = 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>



jspSmartUpload实现图片的批量上传

标签:des   style   blog   http   io   ar   os   sp   for   

原文地址:http://blog.csdn.net/shijiebei2009/article/details/41777655

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!