日期:2014-05-18  浏览次数:20727 次

在上传图片时如何获得上传图片的文件名,并写入数据库
upload.html

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">
<html>
<head>
<title> 文件上传 </title>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
</head>
<body>
<p> &nbsp; </p>
<p   align= "center "> 上传文件选择 </p>
<FORM   METHOD= "POST "   ACTION= "do_upload.jsp "
ENCTYPE= "multipart/form-data ">
<input   type= "hidden "   name= "TEST "   value= "good ">
    <table   width= "75% "   border= "1 "   align= "center ">
        <tr>  
            <td> <div   align= "center "> 请选择上传的图片:  
                    <input   type= "FILE "   name= "FILE1 "   size= "30 ">
                </div> </td>
        </tr>
        <tr>  
            <td> <div   align= "center ">
                    <input   type= "submit "   name= "Submit "   value= "上传它! ">
                </div> </td>
        </tr>
    </table>
</FORM>
</body>
</html>
 
do_upload.jsp

<%@   page   contentType= "text/html;charset=gb2312 "%>
<%@   page   import= "java.sql.* "%>
<%@   page   import= "com.jspsmart.upload.* "   %>

<%       //Integer   ii=(Integer)session.getAttribute( "filename ");
        //   int   filename=ii.intValue();
//   int   filename1=filename+1;
//   session.setAttribute( "filename ",new   Integer(filename1));
      //     System.out.println((((Integer)session.getAttribute( "filename ")).intValue()));
  String   myFileName   =   " ";
      //实例化上载bean
          com.jspsmart.upload.SmartUpload   mySmartUpload=new   com.jspsmart.upload.SmartUpload();
        //初始化
          mySmartUpload.initialize(pageContext);  
        //设置上载的最大值
        mySmartUpload.setMaxFileSize(500*1024*1024);
        //上载文件
        mySmartUpload.upload();
      //循环取得所有上载的文件
      for   (int   i=0;i <mySmartUpload.getFiles().getCount();i++){
      //取得上载的文件
      com.jspsmart.upload.File   myFile   =   mySmartUpload.getFiles().getFile(i);
      if   (!myFile.isMissing())
        {
  &nb