首页 > 代码库 > 使用jspSmartUpload组件实现图片的上传

使用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=http://www.mamicode.com/"./styles.css">-->>
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 = http://www.mamicode.com/javax.imageio.ImageIO.read(file);//构造Image对象>

使用jspSmartUpload组件实现图片的上传