more file upload

类别:Java 点击:0 评论:0 推荐:


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.*" %>
<%@ 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 = "";
  Object pid = "";
  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());
         File savedFile = new File(getServletContext().getRealPath("/test/images/"),fullFile.getName());
      ff = fullFile.getName();

    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");
           }
    }
    }


      fi.write(savedFile);     //上传到服务器
         v.addElement(ff);
  
   }
        
    }
   
     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>

 

本文地址:http://com.8s8s.com/it/it14108.htm