upfile.jsp
<form method="post" action="uploadimage.jsp" name="form1"
enctype="multipart/form-data">
<input type="file" name="file">
<input type="submIT" name="sub" value="upload">
</form>
<form method="post" action="uploadimage.jsp" name="form1"
enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="sub" value="upload">
</form>
<STRONG><FONT color=#ff0000>uploadimage.jsp</FONT>
</STRONG>
uploadimage.jsp
<PRE class=="code">
<%@ page language="java" pageEncoding="gb2312"%>
<%@ page
import="java.io.*,java.awt.image.*,com.sun.image.codec.jpeg.*,jav a.sql.*,com.jspsmart.upload.*,java.util.*"%>
<%@ page import="mainClass.*"%>
<html>
<head>
<title>My JSP'uploadimage.jsp' starting page</title>
</head>
<body>
<%
S martUpload sma = new SmartUpload();
l ong file_max_size = 4000000;
S tring filename1 = "", ext = "", testvar = "";
S tring url = "uploadfiles/";
s ma.initialize(pageContext);
t ry
{
sma.setAllowedFilesList("jpg,gif");
sma.upload();
}catch (Exception e) {
%>
<script language="jscript">
alert("只允许上传jpg,gif格式图片");
window.location.href="upfile.jsp"
</script>
<%
}
try {
com.jspsmart.upload.File myf = sma.getFiles().getFile(0);
if (myf.isMissing()) {
%>
<script language="jscript">
alert("请选择要上传的文件!");
window.location.href="upfile.jsp"
</script>
<%
} else {
ext = myf.getFileExt();
int file_size = myf.getSize();
String saveurl = "";
if (file_size < file_max_size) {
Calendar cal = Calendar.getInstance();
String filename = String.valueOf(cal.getTimeInMills());
saveurl = request.getRealPath("/") + url;
saveurl += filename + "." + ext;
myf.saveAs(saveurl, sma.SAVE_PHYSICAL);
myclass mc = new myclass(request
.getRealPath("data/data.mdb"));
mc
.executeInsert("insert into [path] values('uploadfiles/"
+ filename + "." + ext + "')");//检查
out.println("图片上传成功!");
response.sendRedirect("showimg.jsp");
}
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html> </PRE>。