注意:在编写上传文件的代码时,需确保"WEB-INF/lib"下含有jspsmartupload.jar包。否则会出错。
<!-- uploadfileform.jsp -->
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>上传文件</title>
</head>
<body>
<form method="post" action="uploadfileaccept.jsp" name="book" enctype="multipart/form-data">
<table CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="474">
<tr>
<td align="center" colspan="2">录入书籍信息</td>
</tr>
<tr>
<td width="150">书名:</td>
<td width="324"><input type="text" name="bookname"></td>
</tr>
<tr>
<td>封面:</td>
<td><input type="file" name="photofile"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>出版社:</td>
<td><input type="text" name="publisher"></td>
</tr>
<tr>
<td colspan="2" width="474">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
<!-- uploadfileaccept.jsp -->
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<%@page import="com.jspsmart.upload.*"%>
<html>
<head>
<title>上传文件</title>
</head>
<body>
<%
// 新建一个SmartUpload对象
SmartUpload su=new SmartUpload();
// 上传初始化,pageContext为JSP的内置对象
su.initialize(pageContext);
// 设定同意上传的文件(通过扩展名限制),仅同意jpg,bmp,gif文件
su.setAllowedFilesList("jpg,bmp,gif");
// 上传文件
su.upload();
// 将上传文件所有保存到指定文件夹
su.save("/upload",SmartUpload.SAVE_VIRTUAL);
%> <table cellspacing="0" cellpadding="3" border="1" width="474">
<tr>
<td width="150">书名:</td>
<td width="324"><%=su.getRequest().getParameter("bookname") %></td>
</tr>
<tr>
<td>封面</td>
<td><img src="<%=su.getFiles().getFile(0).getFileName() %>" ></td>
</tr>
<tr>
<td>作者:</td>
<td><%=su.getRequest().getParameter("author") %></td>
</tr>
<tr>
<td>出版社:</td>
<td><%=su.getRequest().getParameter("publisher") %></td>
</tr>
</table>
</body>
</html>