在Servlet中实现文件的上传下载,要注意以下几个问题:
1、在编译Servlet之前必须把servlet-api.jar和SmartUpload.jar和jsp-api.jar(Tomcat的lib包中有)拷到C:/Program Files/Java/jdk1.6.0/jre/lib/ext下
Servlet-api.jar是只要编译servlet时必须要的。
SmartUpload.jar是上传下载文件时必须要的。
而如果要把保存文件的代码写在servlet中,就必须用jsp-api.jar,因为其中有PageContext.class这个类,只有这个类才可以提供initialize()这个方法。
请看下面的代码:
jsp页面
<%@ page contentType="text ml;charset=gb2312"%> <style type="text/css"> Vector vec =(Vector)session.getAttribute("vecproduc"); <form action="MemberServlet" method="post" <tr bgColor=#f6f6f6> <%}%>
<%@ page import="java.util.*,hjj.web.*"%>
<!--
.STYLE1 {color: #FFFFFF}
td {
}
td {
width: auto;
}
td {
width: auto;
}
-->
</style>
<script language="JavaScript">
<!--
function CheckForm()
{
if (document.result.photo.value.length == 0)
{
alert("请选择作品图片.");
document.result.photo.focus();
return false;
}
return true;
}
//--></script>
<%
Enumeration eu=vec.elements();
int size=vec.size();//获取记录总数
System.out.println("sizejjjjjjjjj="+size);
while(eu.hasMoreElements())
{
ProducBean pb= (ProducBean)eu.nextElement();
String name=pb.getName();
String introduce=pb.getIntroduce();
String photo=pb.getPhoto();
String uploadtime=pb.getUploadtime();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text ml; charset=gb2312">
<title>作品管理</title>
</head>
<body>
enctype="multipart/form-data" name="result" onSubmit="return CheckForm()">
<br style="OVERFLOW: hidden; LINE-HEIGHT: 5px">
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
<tr>
<td>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
<TR>
<TD width=130 bgColor=#AEC0EC><IMG height=18 src="" width=5><STRONG><SPAN class=clear>修改作品信息</SPAN>< ></TD>
<TD width=1 rowSpan=2><IMG height=1 src="" width=1></TD>
<TD bgColor=#e6e8ed><IMG height=15 src="" width=8> <font color="#FF0000">请认真填写以下信息!</font></TD></TR>
<tr>
<td bgColor=#416194><img height=1 src="" width=1></td>
<td bgColor=#b7c1ce><img height=1 src="" width=1></td></tr></table></td></tr></table>
<br style="OVERFLOW: hidden; LINE-HEIGHT: 5px">
<table width="80%" border="0" cellspacing="1" align=center bgColor=#e6e6e6 cellpadding="5">
<br><br>
<tr bgColor=#f6f6f6>
<td width="30%"><div align="right">作品名称:</div></td>
<td width="70%"><input name="producname" type="text" id="username" value="<%=name%>" size="20">
<font color="#FF0000">*</font></td>
</tr>
<tr bgColor=#f6f6f6>
<td><div align="right">作品简介:</div></td>
<td><input type=text name="introduce" value="<%=introduce%>">
<font color="#FF0000">*</font></td>
</tr>
<tr bgColor=#fefdf5>
<td><div align="right">photo:</div></td>
<td><input type=file name="photo">
<font color="#FF0000"></td>
</tr>
<td><div align="right">上传时间:</div></td>
<td><input type=text name="uploadtime" value="<%=uploadtime%>">
<font color="#FF0000">*注:时间格式如 2007-07-13</font></td>
</tr>
</table><br>
<input type=hidden name="action" value="updateproduc">
<input type=hidden name="idd" value="<%=pb.getProducId()%>">
<div align="center"> <input type=submit name="uname" width=50 value=" 确 认 提 交 "></div>
</form>
<CENTER>
<a href="UserServlet?action=getproduc">查看作品</a></CENTER>
<CENTER><a href="UserServlet?action=addproduc">增加作品</a></CENTER>
</td></tr>
</body>
< ml>
servlet页面
package hjj.web; import java.util.Date; import javax.servlet.*; public class MemberServlet extends HttpServlet Vector vec=new Vector(); upload.initialize(this.getServletConfig(), request, response); //upload.setAllowedFilesList("jgp"); |