<?xml version="1.0" encoding="UTF-8"?> <config> <!-- 標籤 --> <db-info> <!-- JDBC數據庫參數 --> <driver-name>oracle.jdbc.OracleDriver</driver-name> <url>jdbc:oracle:thin:@localhost:1521:ORCL</url> <user-name>drp1</user-name> <password>drp1</password> </db-info> </config>
/** * 使用dom4j從配置文件裏讀取數據 */ private xmlConfigReader(){ //SAXReader類的read()方法能夠讀入輸入信息,創建一個SAXReader對象 SAXReader reader=new SAXReader(); //經過當前線程類的加載器,來得到引用資源文件的相對路徑,讀入緩衝輸入流 InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml"); try{ //從輸入流中讀取信息 Document doc=reader.read(in); //取得jdbc相關配置信息 Element driverNameEle=(Element)doc.selectObject("/config/db-info/driver-name"); Element urlNameEle=(Element)doc.selectObject("/config/db-info/url"); Element userNameEle=(Element)doc.selectObject("/config/db-info/user-name"); Element passwordEle=(Element)doc.selectObject("/config/db-info/password"); //設置jdbc相關配置信息 jdbcConfig.setDriverName(driverNameEle.getStringValue()); jdbcConfig.setUrlName(urlNameEle.getStringValue()); jdbcConfig.setUserName(userNameEle.getStringValue()); jdbcConfig.setpassword(passwordEle.getStringValue()); }catch(DocumentException e){ e.printStackTrace(); } }a.關於
//文件的上傳路徑 private File uploadPath; //臨時保存路徑 private File tempPath; public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(4096); // the location for saving data that is larger than getSizeThreshold() factory.setRepository(tempPath); ServletFileUpload upload = new ServletFileUpload(factory); // maximum size before a FileUploadException will be thrown upload.setSizeMax(1000000 * 20); try { List fileItems = upload.parseRequest(req); for (Iterator iter = fileItems.iterator(); iter.hasNext();) { FileItem item = (FileItem) iter.next(); //是否爲input="type"輸入域 if (!item.isFormField()) { String fileName = item.getName(); long size = item.getSize(); if ((fileName == null || fileName.equals("")) && size == 0) { continue; } //截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOG fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length()); //item.write(new File(uploadPath + itemNo + ".gif")); item.write(new File(uploadPath, fileName)); //調用網數據庫裏面寫東西的代碼 } } } catch (Exception e) { e.printStackTrace(); } } public void init() throws ServletException { uploadPath = new File(getServletContext().getRealPath("upload")); System.out.println("uploadPath=====" + uploadPath); //若是目錄不存在 if (!uploadPath.exists()) { //建立目錄 uploadPath.mkdir(); } //臨時目錄 tempPath = new File(getServletContext().getRealPath("temp")); if (!tempPath.exists()) { tempPath.mkdir(); } //顯示調用父類的初始化方法 super.init(); }
<!--設置物料查詢Servlet--> <servlet> <servlet-name>SearchItemServlet</servlet-name> <servlet-class>com.bjpowernode.drp.basedata.web.SearchItemServlet</servlet-class> <init-param> <param-name>page-size</param-name> <param-value>3</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SearchItemServlet</servlet-name> <url-pattern>/servlet/item/SearchItemServlet</url-pattern> </servlet-mapping>
<servlet> <servlet-name>FileUploadServlet</servlet-name> <servlet-class>com.bjpowernode.drp.basedata.web.FileUploadServlet</servlet-class> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping> <servlet-name>FileUploadServlet</servlet-name> <url-pattern>/servlet/item/FileUploadServlet</url-pattern> </servlet-mapping>
<error-page> <exception-type>com.bjpowernode.drp.util.ApplicationException</exception-type> <location>/error.jsp</location> </error-page>
<error-page> <error-code>404</error-code> <location>/http_error.jsp</location> </error-page>