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

📄 uploadedit.jsp

📁 用jsp实现的文件上传的组件,适合jsp开发人员使用.
💻 JSP
字号:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.sql.*,com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
	// Initialization
 	mySmartUpload.initialize(pageContext);
	// Upload
	mySmartUpload.upload();
	
	//uncomment the following to save file to upload dir also
	//mySmartUpload.save("/upload");
	
 	String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
	String ConnStr = "jdbc:odbc:files"; 
	Connection conn = null; 
	Statement stmt = null;
	ResultSet rs = null; 
	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_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);		
	} 
	catch(SQLException ex)
	{ 
		System.err.println("aq.executeQuery: " + ex.getMessage()); 
	}  
	
	String docid;
	String filename;
	long filesize;
	String contenttype;
	
	// for debug only
	/*
	out.println("<BR><STRONG>Display information about Files</STRONG><BR>");
	out.println("Number of files = " + mySmartUpload.getFiles().getCount() + "<BR>");
	out.println("Total size (bytes) = " + mySmartUpload.getFiles().getSize() +"<BR>");
	for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
		
		out.print(mySmartUpload.getFiles().getFile(i).getFieldName());
		if (!mySmartUpload.getFiles().getFile(i).isMissing())
			out.print(" = " + mySmartUpload.getFiles().getFile(i).getFileName() + " (" + mySmartUpload.getFiles().getFile(i).getSize() + ")");
		else
			out.print(" = vide");		
		out.println("<BR>");
	}
	*/
	
	com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);		
	
 	if (!myFile.isMissing())
 	{
 		docid = mySmartUpload.getRequest().getParameter("docid");
 		filename = myFile.getFileName();
 		filesize = myFile.getSize();
 		contenttype = myFile.getContentType();
 	
 		System.out.println("docid="+docid);
 		System.out.println("filename="+filename);
 		System.out.println("filesize="+filesize);
 		System.out.println("contenttype="+contenttype);
 		
 		try
		{
 			if ( (docid == null)||(docid.length() == 0) ) // new doc
 			{
 				System.out.println("New document....");
 				rs = stmt.executeQuery("select * from MyUploadTable where id=1"); 			
				rs.moveToInsertRow(); // moves cursor to the insert row				
				rs.updateLong("filesize",filesize);
				rs.updateString("filename",filename);
				rs.updateString("contenttype",contenttype);
				
				// Add the current file in the DB field
				myFile.fileToField(rs,"filedata");
				
				// insert
				rs.insertRow();
			}									
 			else // edit doc
 			{
 				System.out.println("Edit document id=" +docid );
 				rs = stmt.executeQuery("select * from MyUploadTable where id="+docid); 
 				if (rs.next())
 				{
					rs.updateLong("filesize",filesize);
					rs.updateString("filename",filename);
					rs.updateString("contenttype",contenttype);				
					// Add the current file in the DB field
					myFile.fileToField(rs,"filedata");
					// Update
					rs.updateRow();
 				}
 			}
 		}
 		catch(Exception e)
		{
			out.println("发生错误: " + e.toString());				
		}
		rs.close();
		out.println("保存的文件: " + filename + "<br>");
		out.println("大小: " + filesize + " bytes<br>");
 	}
 	else
 	{
 		out.println("没有上载的文件!<br>");
 	}
%>

		

⌨️ 快捷键说明

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