编程语言

img ziying

more file upload

发表于2004/10/28 10:50:00  693人阅读

分类: jsp


fileup2_in.jsp多文件同时上传

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.util.*,java.util.regex.*" %>
<%@ page import="java.io.*,java.text.*" %>
<%@ page import="javax.servlet.*,javax.servlet.http.*"%>
<jsp:useBean id="test" scope="page" class="test.DBconn"/>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>
<%
   
   try{
        String dir=request.getRealPath(".");
        DiskFileUpload fu = new DiskFileUpload();
  
       
        fu.setSizeMax(4194304);                 //设置文件大小.

        fu.setSizeThreshold(4096);              //设置缓冲大小.

        fu.setRepositoryPath(dir+"/test");      //设置临时目录.
 
        List fileItems = fu.parseRequest(request);  //解析请求,返回一个集合.
        //System.out.println("fileItems="+fileItems.length());
        Iterator i = fileItems.iterator();
   String regExp=".+////(.+)$";
        String fieldvalue="";
        String ff = null;
  String picname=null;
  int q = 1;
  Object pid = null;
  Vector vname = new Vector();
  Vector v = new Vector();
  //int k = 0;
  String[] errorType={".exe",".com",".cgi",".asp",".swf",".txt"};   //还没完成
        Pattern p = Pattern.compile(regExp);

        while(i.hasNext())            
       {
    
         FileItem fi = (FileItem)i.next();
 
         if(fi.isFormField())                        //这是用来确定是否为文件属性,
         {
     
         String fieldName = fi.getFieldName();     //这里取得表单名
         fieldvalue=fi.getString();        //这里取得表单值
     
      v.addElement(fieldvalue);
    
 // out.print("<br>1");
    // out.print("name:"+fieldName);
    // out.print("value:"+fieldvalue);
    // System.out.println(fieldvalue);
 
         }
         else                                           //这里开始外理文件
      {
   
  
      File fullFile = new File(fi.getName());
         ff = fullFile.getName();
      String rr = "";
 
      java.util.Date date2 = new java.util.Date();
      SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss");
         String str2 = formatter.format(date2);
      StringTokenizer st = new StringTokenizer(ff,".");
         if (st.hasMoreTokens()){
          String test12 = st.nextToken();
          rr = st.nextToken();
          //System.out.println(rr);
         }
   
       if (rr.equals(""))
      {
    picname = "0";
   }
   else
      {
             picname = str2+q+"."+rr;     //以时间为图片名称
            }
         File savedFile = new File(getServletContext().getRealPath("/test/images/"),picname);
  
  /*  if(ff==null||ff.equals(""))
               continue;
              Matcher m = p.matcher(ff);
             boolean result = m.find();
          if (result){
              for (int temp=0;temp<errorType.length;temp++){
              if (m.group(1).endsWith(errorType[temp])){
                    System.out.println(ff+": wrong type");
           }
    }
   }*/


      if (picname!="0"){
        fi.write(savedFile);        //上传到服务器
       }                       
         v.addElement(picname);
   q++;
  
   }
        
    }
   
     for (int k = 0;k<(v.size()-1);k=k+3)
    {
   
     
  // for (int j = 0;j<vname.size();j++)
   // {
   pid = v.elementAt(k);    //将数据写入到数据库
      Object pixid = v.elementAt(k+1);
     Object ff2 = v.elementAt(k+2);
    //Object pic_url = v.elementAt(2);
   // Object seqno = v.elementAt(3);
   // System.out.println(down);
    //java.util.Date date = new java.util.Date();
      // String strdate = date.toLocaleString();
  //  String url = tt + "/";
       //int down = 20;
      //k++;
      // System.out.println(pid);
      // System.out.println(pixid);
   // System.out.println(ff2);
   
     System.out.println(v);
  
    try
    {
            String sql = "insert into picturelib(pid,pixid,pic_url)values("+pid+",'"+pixid+"','"+ff2+"')";
   System.out.println("sql="+sql);
            test.executeUpdate(sql);
    }
   
    catch(Exception e)
    {
          System.out.println("insert into db error!");
    }
   //}
  }
     }
    catch(Exception e)
   {
   System.out.println("upload saved error");
   }

      
 %>
<script language="javascript">
   alert("上传成功了,请返回!");window.location.reload("fileup.jsp");
</script>
</body>
</html>

 

阅读全文
0 0

相关文章推荐

img
取 消
img