JSP多文件上传

  1. <%@page   import="java.util.*"   %> 
  2. <%@page   import="java.io.*"   %> 
  3. <%@ page contentType="text/html; charset=gb2312" %>
  4. <%@ include file="../../../include/common_page.jsp" %>
  5. <%
  6. //jsp文件上传源码,单一或多文件也可与其他标单类型混杂使用,可不限制上传大小,速度一流 
  7. //作者:jjcc   2007.10.26
  8. UserInfo t_user=(UserInfo)session.getAttribute("userinfo");
  9. Connection con=ConnectPool.getConnection("syjc");
  10. Statement St_xh=con.createStatement();
  11. //设置上传服务器路径
  12. String mypath_m =application.getRealPath("/syjc");
  13. if((System.getProperty("os.name").toLowerCase().indexOf("sun"))>-1)
  14. {
  15.    mypath_m+="/docup/";
  16. }
  17. else
  18. {
  19.    mypath_m+="//docup//";
  20. }
  21. String[][] arr_param = new String[20][2]; //可以存放20个参数的数组
  22. int param_len=0//参数数组的长度
  23. String message= ""//出错信息
  24. String folder = ""//文件夹参数
  25. char[]   hchl={13,10}; //char(10)表示回车 char(13)表示换行
  26. String   boundary=request.getContentType().substring(30); 
  27. String   field_boundary="--"+boundary+new   String(hchl); 
  28. String   last_boundary="--"+boundary+"--"+new   String(hchl);
  29. System.out.println("boundary: "+boundary);
  30. //System.out.println("field_boundary: "+field_boundary);
  31. //System.out.println("hchl: "+new   String(hchl));
  32. ServletInputStream   getdata=request.getInputStream(); //客户端的输入流
  33. ByteArrayOutputStream   temp=new   ByteArrayOutputStream(); //建立临时2进制输出流
  34. byte[]   data_line=new   byte[8192]; //临时读取字符数组
  35. int   line_byte_count=0//一次读入的字节数
  36. boolean   found_boundary=false//边界标志
  37. while((line_byte_count=getdata.readLine(data_line,0,data_line.length))!=-1){ //读入第一个文件分割符
  38.         if(!found_boundary){ 
  39.                 line_byte_count=getdata.readLine(data_line,0,data_line.length); 
  40.         } 
  41.         String   temp_str=new   String(data_line,0,line_byte_count); //将读入的字节形成字符串
  42.         //System.out.println("temp_str: "+temp_str);
  43.         if(temp_str.indexOf("filename")!=-1){    //读入的是file数据
  44.                 if(temp_str.substring(temp_str.indexOf("filename=")+9,temp_str.lastIndexOf("/"")+1).length()> 2){ 
  45.                         String   file_name=new String(temp_str.substring(temp_str.lastIndexOf("//")+1,temp_str.lastIndexOf("/"")).getBytes("ISO8859_1"),"EUC-CN"); 
  46.                         
  47.                         //对文件名的处理
  48.                         if(file_name.matches("^.*缓凝.+$")) {
  49.                         file_name = "HNJ.xls";
  50.                         System.out.println("file_name: "+file_name);
  51.                         }
  52.                         else if(file_name.matches("^.*减阻.+$")) {
  53.                         file_name = "JZJ.xls";
  54.                         System.out.println("file_name: "+file_name);
  55.                         }
  56.                         else if(file_name.matches("^.*降失水.+$")) {
  57.                         file_name = "JSSJ.xls";
  58.                         System.out.println("file_name: "+file_name);
  59.                         }
  60.                         else if(file_name.matches("^.*水泥质检.+$")) {
  61.                         file_name = "SNZJ.xls";
  62.                         System.out.println("file_name: "+file_name);
  63.                         }
  64.                         else if(file_name.matches("^.*取样.+$")) {
  65.                         file_name = "QY.xls";
  66.                         }
  67.                         line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type信息
  68.                         line_byte_count=getdata.readLine(data_line,0,data_line.length); //文件信息第一行
  69.                         
  70.                         //在服务器端生成文件
  71.                         ResultSet Rst_xh=St_xh.executeQuery("select to_char(sysdate,'yyyymmddhh24miss') xh from dual");
  72.        Rst_xh.next();
  73.        String f_xh=Rst_xh.getString("xh");
  74.        Rst_xh.close();
  75.        folder += mypath_m +f_xh+file_name+"@";
  76.                         FileOutputStream   myfile=new   FileOutputStream(mypath_m+f_xh+file_name,false); 
  77.                         boolean   test=true//在此文件中的标志
  78.                         while(test)   { 
  79.                                 line_byte_count=getdata.readLine(data_line,0,data_line.length); 
  80.                                 if(line_byte_count==-1){ //到文件流的末尾,退出
  81.                                         test=false
  82.                                         break
  83.                                 } 
  84.                                 if(temp.size()==0){ //使用 空的 临时2进制输出流
  85.                                         temp.write(data_line,0,line_byte_count); 
  86.                                 }else
  87.                                         if(new   String(data_line,0,line_byte_count).equals(field_boundary)   || new   String(data_line,0,line_byte_count).equals(last_boundary)){ 
  88.                                                 //如果是文件分隔符 
  89.                                                 myfile.write(temp.toByteArray(),0,temp.toByteArray().length-2); //去掉最后的回车换行符
  90.                                                 temp.reset(); //清空临时2进制输出流
  91.                                                 myfile.close(); 
  92.                                                 //out.println(file_name+"上传成功了 <br> "); message += "上传成功了";
  93.                                                 test=false
  94.                                                 found_boundary=true
  95.                                         }else
  96.                                                 temp.writeTo(myfile); 
  97.                                                 temp.reset(); 
  98.                                                 temp.write(data_line,0,line_byte_count); 
  99.                                         } 
  100.                                 } 
  101.                         } 
  102.                 }else//读入的不是文件
  103.                         String   field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf(";")-1); 
  104.                         line_byte_count=getdata.readLine(data_line,0,data_line.length); 
  105.                         
  106.                         line_byte_count=getdata.readLine(data_line,0,data_line.length); 
  107.                        
  108.                         line_byte_count=getdata.readLine(data_line,0,data_line.length); 
  109.                        
  110.                         line_byte_count=getdata.readLine(data_line,0,data_line.length); 
  111.                         found_boundary=true
  112.                         message = field_name+"没有选择上传文件! //n "
  113.                         
  114.                 } 
  115.         }   else//不是文件部分
  116.                 String   field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf("/"")); 
  117.                 line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type
  118.                 temp.reset(); 
  119.                 boolean   test=true
  120.                 while(test)   { 
  121.                         line_byte_count=getdata.readLine(data_line,0,data_line.length); 
  122.                         if(line_byte_count==-1){ 
  123.                                 test=false
  124.                                 break
  125.                         } 
  126.                         if(new   String(data_line,0,line_byte_count).equals(field_boundary)   ||   new   String(data_line,0,line_byte_count).equals(last_boundary)){ 
  127.                                 test=false
  128.                                 found_boundary=true
  129.                                 if(temp.size()> 2){ 
  130.                                         arr_param[param_len][0]=field_name;
  131.                                         arr_param[param_len][1]=new   String(temp.toByteArray()).substring(0,new   String(temp.toByteArray()).length()-2);
  132.                                         param_len++;
  133.                                 }else
  134.                                         arr_param[param_len][0]=field_name;
  135.                                         arr_param[param_len][1]="null";
  136.                                         param_len++;
  137.                                 } 
  138.                                 temp.reset(); 
  139.                         }else
  140.                                 temp.write(data_line,0,line_byte_count); //temp里存放的是data_line上一次读入的数据
  141.                         } 
  142.                 } 
  143.         } 
  144.         
  145. getdata.close();
  146. for(int i=0;i < param_len;i++) {
  147. System.out.println(arr_param[i][0]+" = "+arr_param[i][1]);
  148. }
  149. System.out.println("folder: "+folder);
  150. %>
  151. <html>
  152. <body>
  153. <form name="form1" method="POST">
  154. <%
  155. for(int i=0;i < param_len;i++) {
  156. %>
  157. <input type="hidden" name="<%=arr_param[i][0]%>" value="<%=arr_param[i][1]%>">
  158. <%
  159. }
  160. %>
  161. <input type="hidden" name="folder" value="<%=folder%>">
  162. </form>
  163. <script language="javascript">
  164. var msg = "<%=message%>";
  165. if(msg != "")
  166.    alert("msg:"+msg);
  167. document.form1.action = "syjc_yjsn_sjjslr_insert_commit.jsp?page_flag=pl";
  168. document.form1.submit();
  169. </script>
  170. </body>
  171. </html>
相关文章