文件上传页面:<%@ page language="java" pageEncoding="GB2312"%>
<style>
*{ font-size:12px;}
</style>
<form action="remote" method="post" enctype="multipart/form-data" name="form1">
<TABLE width="532" >
         <tr>
             <td><div align="right">头像 :</div></td>
            <td id="file" >
              <img src="file:///D|/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/RemotePic/images/touxiang.jpg" width="100" height="100" /><br>
                <input type="file" name="file1">    
                <input name="继续添加附件" type="button" value="继续添加附件" onclick="AddMore()">    
            </td>
        </tr>
<TR >
            <td align="left"></td>
<td align="left"><input type="submit" name="Submit" value="上传" /></td>
        </tr>
  </table>    
</form><script type="text/javascript">
        function AddMore(){
            var more = document.getElementById("file");
            var br = document.createElement("br");
            var input = document.createElement("input");
            var button = document.createElement("input");
            
            input.type = "file";
            input.name = "file";
            
            button.type = "button";
            button.value = "取消";
            
            more.appendChild(br);
            more.appendChild(input);
            more.appendChild(button);
            
            button.onclick = function(){
                more.removeChild(br);
                more.removeChild(input);
                more.removeChild(button);
            }; 
        }
    </script>文件获取代码:package smartupload;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
//import java.util.Calendar;
import java.util.Date;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
//import com.jspsmart.upload.SmartUploadException;public class UploadFile extends HttpServlet 
{
private static final long serialVersionUID = 1L;
@SuppressWarnings({ "static-access", "deprecation" })
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException 
{
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();

SmartUpload mySmartUpload = new SmartUpload();
@SuppressWarnings("unused")
Request myrequest = null;
// String title =null;

mySmartUpload.initialize(this.getServletConfig(),request,response);// 初始化

try 
{
mySmartUpload.setAllowedFilesList("doc,txt,pdf,rar,gif,jpg,ppt,xml");// 只允许上载此类文件
mySmartUpload.upload();// 上载文件
myrequest = mySmartUpload.getRequest();

catch (Exception e) 
{
out.print("<script>");
out.println("alert('只允许上传doc,txt,pdf,rar,gif,jpg,ppt类型的文件!');window.location='index.jsp';");
out.print("</script>");
}
try 
{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
@SuppressWarnings("unused")
int count = 0;
if (myFile.isMissing())
{
out.print("<script>");
out.println("alert('请至少选择一个要上传的文件!');window.location='index.jsp';");
out.print("</script>");

else 
{
out.println("正在上传文件,请等待。。");
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) 
{
myFile = mySmartUpload.getFiles().getFile(i);
if (myFile.isMissing())
continue;
String ext = myFile.getFileExt(); // 取得后缀名
@SuppressWarnings("unused")
int file_size = myFile.getSize(); // 取得文件的大小
String saveurl=null;

Date dt = null; 
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
dt = new Date(System.currentTimeMillis()); 
//             String newfilename = fmt.format(dt)+ filename.substring(filename.lastIndexOf("."));

// Calendar calendar = Calendar.getInstance();// 更改文件名,取得当前上传时间的毫秒数值
String filename =fmt.format(dt);
saveurl = request.getRealPath("\\")+"file\\";

saveurl +=filename + "." + ext;// 最终文件的保存路径
String name= filename + "." + ext;  System.out.println(saveurl); myFile.saveAs(saveurl, mySmartUpload.SAVE_VIRTUAL);
// title = myrequest.getParameter("title"); PreparedStatement pstmt = null;
DataBaseConnection dbc = null;
dbc = new DataBaseConnection();// FileInputStream fis;
// File file;
//
// file = new File(saveurl);
// fis = new FileInputStream(file);


pstmt = dbc.getConnection().prepareStatement("insert into file(name) values(?)");
pstmt.setString(1,name);
// pstmt.setBinaryStream(2, fis, (int) file.length());
System.out.println("success inserted file into database");
pstmt.execute();
pstmt.close();
dbc.close();
} out.print("<script>");
out.print("alert('上传成功');");
out.print("window.location='list.jsp'");
out.print("</script>");
}
}
catch (Exception e) 
{
e.toString(); }
out.flush();
out.close();
}
}