首页 > 代码库 > JSP简单练习-上传文件

JSP简单练习-上传文件

注意:在编写上传文件的代码时,需确保"WEB-INF/lib"下含有jspsmartupload.jar包,否则会出错。

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=http://www.mamicode.com/"提交">>
<!-- 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=http://www.mamicode.com/"" >>