- <%@page import="java.util.*" %>
- <%@page import="java.io.*" %>
- <%@ page contentType="text/html; charset=gb2312" %>
- <%@ include file="../../../include/common_page.jsp" %>
- <%
- //jsp文件上传源码,单一或多文件也可与其他标单类型混杂使用,可不限制上传大小,速度一流
- //作者:jjcc 2007.10.26
- UserInfo t_user=(UserInfo)session.getAttribute("userinfo");
- Connection con=ConnectPool.getConnection("syjc");
- Statement St_xh=con.createStatement();
- //设置上传服务器路径
- String mypath_m =application.getRealPath("/syjc");
- if((System.getProperty("os.name").toLowerCase().indexOf("sun"))>-1)
- {
- mypath_m+="/docup/";
- }
- else
- {
- mypath_m+="//docup//";
- }
- String[][] arr_param = new String[20][2]; //可以存放20个参数的数组
- int param_len=0; //参数数组的长度
- String message= ""; //出错信息
- String folder = ""; //文件夹参数
- char[] hchl={13,10}; //char(10)表示回车 char(13)表示换行
- String boundary=request.getContentType().substring(30);
- String field_boundary="--"+boundary+new String(hchl);
- String last_boundary="--"+boundary+"--"+new String(hchl);
- System.out.println("boundary: "+boundary);
- //System.out.println("field_boundary: "+field_boundary);
- //System.out.println("hchl: "+new String(hchl));
- ServletInputStream getdata=request.getInputStream(); //客户端的输入流
- ByteArrayOutputStream temp=new ByteArrayOutputStream(); //建立临时2进制输出流
- byte[] data_line=new byte[8192]; //临时读取字符数组
- int line_byte_count=0; //一次读入的字节数
- boolean found_boundary=false; //边界标志
- while((line_byte_count=getdata.readLine(data_line,0,data_line.length))!=-1){ //读入第一个文件分割符
- if(!found_boundary){
- line_byte_count=getdata.readLine(data_line,0,data_line.length);
- }
- String temp_str=new String(data_line,0,line_byte_count); //将读入的字节形成字符串
- //System.out.println("temp_str: "+temp_str);
- if(temp_str.indexOf("filename")!=-1){ //读入的是file数据
- if(temp_str.substring(temp_str.indexOf("filename=")+9,temp_str.lastIndexOf("/"")+1).length()> 2){
- String file_name=new String(temp_str.substring(temp_str.lastIndexOf("//")+1,temp_str.lastIndexOf("/"")).getBytes("ISO8859_1"),"EUC-CN");
- //对文件名的处理
- if(file_name.matches("^.*缓凝.+$")) {
- file_name = "HNJ.xls";
- System.out.println("file_name: "+file_name);
- }
- else if(file_name.matches("^.*减阻.+$")) {
- file_name = "JZJ.xls";
- System.out.println("file_name: "+file_name);
- }
- else if(file_name.matches("^.*降失水.+$")) {
- file_name = "JSSJ.xls";
- System.out.println("file_name: "+file_name);
- }
- else if(file_name.matches("^.*水泥质检.+$")) {
- file_name = "SNZJ.xls";
- System.out.println("file_name: "+file_name);
- }
- else if(file_name.matches("^.*取样.+$")) {
- file_name = "QY.xls";
- }
- line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type信息
- line_byte_count=getdata.readLine(data_line,0,data_line.length); //文件信息第一行
- //在服务器端生成文件
- ResultSet Rst_xh=St_xh.executeQuery("select to_char(sysdate,'yyyymmddhh24miss') xh from dual");
- Rst_xh.next();
- String f_xh=Rst_xh.getString("xh");
- Rst_xh.close();
- folder += mypath_m +f_xh+file_name+"@";
- FileOutputStream myfile=new FileOutputStream(mypath_m+f_xh+file_name,false);
- boolean test=true; //在此文件中的标志
- while(test) {
- line_byte_count=getdata.readLine(data_line,0,data_line.length);
- if(line_byte_count==-1){ //到文件流的末尾,退出
- test=false;
- break;
- }
- if(temp.size()==0){ //使用 空的 临时2进制输出流
- temp.write(data_line,0,line_byte_count);
- }else{
- if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
- //如果是文件分隔符
- myfile.write(temp.toByteArray(),0,temp.toByteArray().length-2); //去掉最后的回车换行符
- temp.reset(); //清空临时2进制输出流
- myfile.close();
- //out.println(file_name+"上传成功了 <br> "); message += "上传成功了";
- test=false;
- found_boundary=true;
- }else{
- temp.writeTo(myfile);
- temp.reset();
- temp.write(data_line,0,line_byte_count);
- }
- }
- }
- }else{ //读入的不是文件
- String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf(";")-1);
- line_byte_count=getdata.readLine(data_line,0,data_line.length);
- line_byte_count=getdata.readLine(data_line,0,data_line.length);
- line_byte_count=getdata.readLine(data_line,0,data_line.length);
- line_byte_count=getdata.readLine(data_line,0,data_line.length);
- found_boundary=true;
- message = field_name+"没有选择上传文件! //n ";
- }
- } else{ //不是文件部分
- String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf("/""));
- line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type
- temp.reset();
- boolean test=true;
- while(test) {
- line_byte_count=getdata.readLine(data_line,0,data_line.length);
- if(line_byte_count==-1){
- test=false;
- break;
- }
- if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
- test=false;
- found_boundary=true;
- if(temp.size()> 2){
- arr_param[param_len][0]=field_name;
- arr_param[param_len][1]=new String(temp.toByteArray()).substring(0,new String(temp.toByteArray()).length()-2);
- param_len++;
- }else{
- arr_param[param_len][0]=field_name;
- arr_param[param_len][1]="null";
- param_len++;
- }
- temp.reset();
- }else{
- temp.write(data_line,0,line_byte_count); //temp里存放的是data_line上一次读入的数据
- }
- }
- }
- }
- getdata.close();
- for(int i=0;i < param_len;i++) {
- System.out.println(arr_param[i][0]+" = "+arr_param[i][1]);
- }
- System.out.println("folder: "+folder);
- %>
- <html>
- <body>
- <form name="form1" method="POST">
- <%
- for(int i=0;i < param_len;i++) {
- %>
- <input type="hidden" name="<%=arr_param[i][0]%>" value="<%=arr_param[i][1]%>">
- <%
- }
- %>
- <input type="hidden" name="folder" value="<%=folder%>">
- </form>
- <script language="javascript">
- var msg = "<%=message%>";
- if(msg != "")
- alert("msg:"+msg);
- document.form1.action = "syjc_yjsn_sjjslr_insert_commit.jsp?page_flag=pl";
- document.form1.submit();
- </script>
- </body>
- </html>
JSP多文件上传
最新推荐文章于 2023-05-16 14:41:18 发布