⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadhtmls.jsp

📁 电子签章编译文件
💻 JSP
字号:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.io.*,java.sql.*,java.lang.Math.*,com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%!
 	String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
	String ConnStr = "jdbc:odbc:files23jsp"; 
	Connection conn = null; 
	Statement stmt = null;
public void SaveFileToRS(java.io.File file,ResultSet rs, String colname)  throws IOException
{
	try
	{
		java.io.InputStream inStream=new java.io.FileInputStream(file);
		byte[] bytes = new byte[(int)file.length()];
		inStream.read(bytes);
		inStream.close();
        rs.updateBytes(colname,bytes);	
	}
	catch(SQLException e)
	{
	    
	}
}
public long GetLastID(String myTableName,String myFieldName)
{
	long myResult = 0;
	String sqlStr = "select max("+myFieldName+") as MaxID from " + myTableName;
	try
	{
		ResultSet result = stmt.executeQuery(sqlStr);
		if (result.next())
		{
	 		myResult = result.getLong("MaxID");
		}
		result.close();
	}
	catch(Exception e)
	{
		System.out.println(e.toString());
	}
	return (myResult);
}

%>
<%
	// Initialization
 	mySmartUpload.initialize(pageContext);
	// Upload
	mySmartUpload.upload();
	
	//取消注释以保存文件
	//mySmartUpload.save("/jsp/jspdemo/upload");
	
	ResultSet rs = null; 
	String tempdir = new Long((new Double(Math.random()*10000)).longValue()).toString();
	System.out.println("tempdir="+tempdir);	
	try 
	{ 
		//加载数据库驱动程序 
		Class.forName(DBDriver); 
	} 
	catch(java.lang.ClassNotFoundException e)
	{ 
		System.err.println("DBconn (): " + e.getMessage()); 
	}
		
	//与DBMS建立连接 
	try
	{ 
		conn = DriverManager.getConnection(ConnStr); 
		stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);			
	} 
	catch(SQLException ex)
	{ 
		System.err.println("aq.executeQuery: " + ex.getMessage()); 
	}  
	
	String docid;
	String filename;
	String data;
	long filesize;
	
	com.jspsmart.upload.File myFile = null;
	
	docid = mySmartUpload.getRequest().getParameter("docid");
	System.out.println("docid="+docid);	
	
	String temphtmlpath = "/jsp/temp/"+tempdir;
	
	//创建临时目录
    java.io.File tdir = new java.io.File(application.getRealPath(temphtmlpath));
    tdir.mkdirs();
    
	for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
	{
		myFile = mySmartUpload.getFiles().getFile(i);
		//debug only
		System.out.println("File=" + myFile.getFileName());  		
  		//处理数据及文件
  		if (!myFile.isMissing())
	 	{	 		
	 		filename = myFile.getFileName();
	 		filesize = myFile.getSize();
	 		System.out.println("myFile.getFieldName()="+myFile.getFieldName());	
	 		if(myFile.getFieldName().equalsIgnoreCase("HTMLFILES") )	 //HTML文件
	 		{
	 			System.out.println("处理HTML文件"); 	 		
		 		try		 		
				{
				    //将文件保存到临时目录					
					System.out.println(temphtmlpath+"/"+filename);
					myFile.saveAs(temphtmlpath+"/"+filename);
					out.println("保存的文件: " + filename + "<br>");
					out.println("大小: " + filesize + " bytes<br>");	
		 		}
		 		catch(Exception e)
				{
					out.println("发生错误: " + e.toString());				
				}				
			}
	 		else //其他附件文件.本示例中因为在保存为html时没有提交form数据,不会执行
			{
				try
				{
					//向附件表中增加数据
					System.out.println("处理附件文件");
					out.println("保存的文件: " + filename + "<br>");
					out.println("大小: " + filesize + " bytes<br>");								
		 		}
		 		catch(Exception e)
				{
					out.println("发生错误: " + e.toString());				
				}		
			}	
	 	}  // end if (!myFile.isMissing())
	}// end for 
	//处理文件
	java.io.File allfiles[] = tdir.listFiles();
	java.io.File HTMLFile = null;
	int HTMLFileSize = 0;
	java.io.File OtherFile = null;
	
	System.out.println("寻找html文件");
	//寻找html文件
	for (int i=0;i< allfiles.length; i++)
	{
		if( allfiles[i].isFile() )
		{
		    
			if ( allfiles[i].getName().endsWith(".html"))
			{				
				HTMLFile = 	new java.io.File(application.getRealPath(temphtmlpath+"/"+allfiles[i].getName()));
				HTMLFileSize = 	(int)allfiles[i].length();	
				System.out.println(allfiles[i].getName());
			}
		}
	}
	long FileID = 0;
	if (HTMLFile != null)
	{
        InputStream inStream=new java.io.FileInputStream(HTMLFile);
        byte[] bytes = new byte[HTMLFileSize];
        inStream.read(bytes); 
        inStream.close(); 
        String htmlcontent = new String(bytes);
       
        //将每一个不是HTML的文件添加到数据库,并修改HTML文件中有关其连接的信息
		for (int i=0;i< allfiles.length; i++)
	    {
    		if( allfiles[i].isFile() )
    		{
    		    String tfname = allfiles[i].getName();
    			if ( ! tfname.endsWith(".html"))
    			{		
    				try
				    {		
				        rs = stmt.executeQuery("select * from imagefiles where id=1");	
        				//加入数据库并获得ID         				
        				System.out.println("加入文件"+tfname+"到数据库并获得ID");			
        				rs.moveToInsertRow(); // moves cursor to the insert row				
        				rs.updateLong("filesize",allfiles[i].length());
        				System.out.println("filesize="+allfiles[i].length());
        				rs.updateString("filename",tfname);
        				SaveFileToRS(allfiles[i],rs, "filedata");			 
        				rs.insertRow();
        				System.out.println("rs.insertRow(); ok");
        				rs.close();	
        				//获得该文件的ID
        				FileID = GetLastID("imagefiles","id");
        				if(0!= FileID) 
        				{
            				System.out.println("id=" + FileID);	        							
            				//在HTML文件BUF中替换该文件
         				    htmlcontent = htmlcontent.replaceAll(tfname,"readimage.jsp?id=" + new Long(FileID).toString());
         				    //htmlcontent.replaceAll("html","readimage.jsp?id=" + new Long(FileID).toString());
     				    }     				    
     				}
     				catch(Exception e)
    				{
    					out.println("发生错误: " + e.toString());				
    				}	
    			}
    		}
	    }

	    try
	    {
	        System.out.println("修改HTML文件");		    
    	    bytes = htmlcontent.getBytes();
    	    OutputStream outstream= new java.io.FileOutputStream(HTMLFile);
         	outstream.write(bytes);
         	outstream.flush();
         	outstream.close();
            
    	    System.out.println("保存HTML文件到HTML表");	
        	String tfname = HTMLFile.getName();
        	rs = stmt.executeQuery("select * from htmlfiles where id=1");
        	rs.moveToInsertRow(); // moves cursor to the insert row				
        	rs.updateLong("filesize",HTMLFile.length());
        	rs.updateString("filename",tfname);
            SaveFileToRS(HTMLFile,rs, "filedata");
        	rs.insertRow();	
        	rs.close();	
        }
        catch(Exception e)
        {
            out.println("发生错误: " + e.toString());	
        }   		
	}				
	System.out.println("删除临时文件");
	for (int i=0;i< allfiles.length; i++)
	{
		if( allfiles[i].isFile() )
		{
           allfiles[i].delete();
		}
	}
	System.out.println("删除临时目录");	
    tdir.delete();
	stmt.close();	
	conn.close();
%>

		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -