当前位置: 首页 > 图文教程 > 网络编程 > JSP > JSP处女作:commons-fileupload-1.0.jar + Oracle数据库文件上传

JSP
GET 方式提交的含有特殊字符的参数
java big5到gb2312的编码转换
java Lucene 中自定义排序的实现
hibernate中的增删改查实现代码
jsp 定制标签(Custom Tag)
jsp基础速成精华讲解
IE cache缓存 所带来的问题收藏
关于JSP的一点疑问小结
JSP 多条SQL语句同时执行的方法
jsp include文件时的一个乱码解决方法
在JSTL EL中处理java.util.Map,及嵌套List的情况
jsp 页面显示的一些用法
根据Hibernte的cfg文件生成sql文件
五种 JSP页面跳转方法详解
JSP 防范SQL注入攻击分析
JSP 连接MySQL配置与使用
java eclipse 启动参数
jsp 页面上图片分行输出小技巧
解决jsp开发中不支持EL问题
JSP 页面中使用FCKeditor控件(js用法)

JSP处女作:commons-fileupload-1.0.jar + Oracle数据库文件上传


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 119 ::
收藏到网摘: n/a

目前想用Spring+Hibernate+Struts修改可是还没有完成,先共享代码出来:
使用的libraries有commons-fileupload-1.0.jar,和Oracle的jdbc驱动.
  1. <!--$Header: ProcessFileUpload.jsp 1.0.0 2004/10/22 15:10:19 pkm ship  $-->
  2. <%@ page contentType="text/html;charset=GB2312"%>
  3. <%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
  4. <%@ page import="org.apache.commons.fileupload.FileItem"%>
  5. <%@ page import="java.util.*"%>
  6. <%@ page import="java.io.File"%>
  7. <%@ page import="java.sql.*" %>
  8. <html>
  9. <head>
  10. <style>
  11. .NButton
  12. {
  13.     cursor:hand;
  14.     width: 87px;
  15.     height: 20px;
  16.     font-family: 宋体;
  17.     font-size: 12px;
  18.     text-align:center ;
  19.     background-image: url(btn_bkg.gif);
  20.     border:0px;
  21. }
  22. </style>
  23. <%!
  24. // Connect to Oracle database and Insert into cux_upload_files
  25. public void dbInsert(String p_c_file_name,String p_c_path,String p_s_file_name,String p_s_path) {
  26.     Connection conn = null;
  27.     String connStr;
  28.   try {
  29.     connStr="jdbc:oracle:thin:@local:1521:orcl";
  30.     DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  31.     conn = DriverManager.getConnection(connStr,"apps""apps");
  32.     // Insert into table
  33.     conn.setAutoCommit(false); 
  34.     PreparedStatement insertCUF = conn.prepareStatement("INSERT INTO cux.cux_upload_files(file_id,client_file_name,client_path,server_file_name,server_path,created_by,creation_date) "
  35.     + " VALUES (cux.cux_upload_files_s.nextval,?,?,?,?,?,SYSDATE) ");
  36.   
  37.     //insertCUF.setInt(1,2);
  38.     insertCUF.setString(1,p_c_file_name);
  39.     insertCUF.setString(2,p_c_path);
  40.     insertCUF.setString(3,p_s_file_name);
  41.     insertCUF.setString(4,p_s_path);
  42.     insertCUF.setString(5,"XXX");
  43.     insertCUF.executeUpdate();
  44.   
  45.     conn.commit();
  46.   
  47.     conn.setAutoCommit(true);
  48.   
  49.     conn.close();
  50.   }catch(SQLException ex) { // Handle SQL errors
  51.       System.out.println("Error in Connecting to the Database "+'\n'+ex.toString());
  52.   }
  53. }
  54. //
  55. String getCurDate(){
  56.     GregorianCalendar gcDate = new GregorianCalendar(); 
  57.     int year  = gcDate.get(GregorianCalendar.YEAR);
  58.     int month = gcDate.get(GregorianCalendar.MONTH);
  59.     int day   = gcDate.get(GregorianCalendar.DAY_OF_MONTH);
  60.     return "" + year + "-" + month + "-" + day;
  61. }
  62. %>
  63. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" >
  64. <title>Process File Upload</title>
  65. </head>
  66. <body>
  67. <table width="800" border bordercolor="#0000FF">
  68. <tr bgcolor="#66CCFF">
  69. <td colspan=1 rowspan=1 align=left valign=top>
  70.     <strong><font size=2 face="宋体" color=#000000>
  71.     <nobr>客户端文件</nobr>
  72.     </font>
  73.     </strong></td>
  74. <td colspan=1 rowspan=1 align=left valign=top>
  75.     <strong><font size=2 face="宋体" color=#000000>
  76.     <nobr>服务器文件</nobr>
  77.     </font>
  78.     </strong></td>
  79. <td colspan=1 rowspan=1 align=left valign=top>
  80.     <strong><font size=2 face="宋体" color=#000000>
  81.     <nobr>上传用户</nobr>
  82.     </font>
  83.     </strong></td>
  84. </tr>
  85. <%
  86.     //out.println("Content Type ="+request.getContentType());
  87.     
  88.     DiskFileUpload fu = new DiskFileUpload();
  89.     // If file size exceeds, a FileUploadException will be thrown
  90.     fu.setSizeMax(1000000);
  91.     // maximum size that will be stored in memory
  92.     fu.setSizeThreshold(4096);
  93.     // the location for saving data that is larger than getSizeThreshold()
  94.     //fu.setRepositoryPath("/tmp/");
  95.     
  96.     
  97.     List fileItems = fu.parseRequest(request);
  98.     Iterator itr = fileItems.iterator();
  99.     int i = 0;
  100.   
  101.     while(itr.hasNext()) {
  102.       FileItem fi = (FileItem)itr.next();
  103.       i++;
  104.       //Check if not form field so as to only handle the file inputs
  105.       //else condition handles the submit button input
  106.       if (!fi.isFormField()) {
  107.             String filename = fi.getName();
  108.             long filesize = fi.getSize();
  109.       String pUserName = "XIAOHUIPING";
  110.             if((filename==null||filename.equals("")) && filesize==0)
  111.             continue;
  112.             // 注意fi.getName()
  113.             // 会返回上载文件在客户端的完整路径名称,这似乎是一个BUG。
  114.             // 为解决这个问题,这里使用了fullFile.getName()。
  115.             filename=filename.replace('\\','/');
  116.             //new String(filename.getBytes("ISO-8859-1"),"UTF-8");
  117.             File fullFile = new File(filename);
  118.             // 指定fullFile.getName() = "Works.txt";
  119.             File savedFile= new File(application.getRealPath("/Download/"), fullFile.getName());
  120.             fi.write(savedFile);
  121.             // 上传文件成功后写入数据库表
  122.             dbInsert(fullFile.getName(),filename.replace('/','\\'),fullFile.getName(),savedFile.getAbsolutePath());
  123.       if ((i%2) == 0) {
  124.             // 文件上载成功提示,以表格形式打印
  125.       out.println("<tr bgcolor=\"#CCCCCC\"><td colspan=1 rowspan=1 align=left valign=top><font size=2 face=\"宋体\" color=#000000><nobr>" + fullFile.getName() + "</nobr></font></td><td colspan=1 rowspan=1 align=left valign=top><font size=2 face=\"宋体\" color=#000000><nobr>" + fullFile.getName() + "</nobr></font></td><td colspan=1 rowspan=1 align=left valign=top><font size=2 face=\"宋体\" color=#000000><nobr>" + pUserName + "</nobr></font></td></tr>");
  126.             //out.println("<br>" + "Local Filename = " + "\"" + filename.replace('/','\\') + "\"" + " Upload To \""  + savedFile.getAbsolutePath() + "\"" + " Successful!!");
  127.             }
  128.       else if ((i%2) == 1) {
  129.       out.println("<tr><td colspan=1 rowspan=1 bgcolor=#ffffff align=left valign=top><font size=2 face=\"宋体\" color=#000000><nobr>" + fullFile.getName() + "</nobr></font></td><td colspan=1 rowspan=1 bgcolor=#ffffff align=left valign=top><font size=2 face=\"宋体\" color=#000000><nobr>" + fullFile.getName() + "</nobr></font></td><td colspan=1 rowspan=1 bgcolor=#ffffff align=left valign=top><font size=2 face=\"宋体\" color=#000000><nobr>" + pUserName + "</nobr></font></td></tr>");
  130.       }
  131.       }
  132.     }
  133. %>
  134. </table>
  135. <table width="800" border bordercolor="#0000FF">
  136.   <tr>
  137.     <td height="20" align="center" nowrap="nowrap">
  138.       <DIV align="center">
  139.         <input class="NButton" type="button" value="Back" onClick="javascript:history.back()"/>
  140.         <input class="NButton" type="button" value="Close" onClick="javascript:window.close()"/>
  141.       </DIV>
  142.     </td>
  143.   </tr>
  144. </table>
  145. </body>
  146. </html>



  1. <!--$Header: index.jsp 1.0.0 2004/10/22 15:10:19 pkm ship  $-->
  2. <%@ page contentType = "text/html;charset=gb2312" %>
  3. <html>
  4. <head>
  5. <title>数据文件上传</title>
  6. <style>
  7. BODY
  8. {
  9.   FONT-FAMILY: 宋体;
  10.   FONT-SIZE: 10pt;
  11.     background-color: #F6F6F6;
  12.     margin-top: 10px;
  13.     margin-right: 50px;
  14.     margin-bottom: 50px;
  15.     margin-left: 10px;
  16.     margin-top: 0px
  17.     SCROLLBAR-FACE-COLOR: #D0E5FF;
  18.     SCROLLBAR-HIGHLIGHT-COLOR: #F5F9FF;
  19.     SCROLLBAR-SHADOW-COLOR: #828282;
  20.     SCROLLBAR-3DLIGHT-COLOR: #828282;
  21.     SCROLLBAR-ARROW-COLOR: #797979;
  22.     SCROLLBAR-TRACK-COLOR: #ECECEC;
  23.     SCROLLBAR-DARKSHADOW-COLOR: #ffffff
  24. }
  25. TABLE
  26. {
  27.   FONT-FAMILY: 宋体;
  28.   FONT-SIZE: 10pt
  29. }
  30. .HeaderTitle{
  31.     font-family: 黑体;
  32.     font-size: 30px;
  33.     font-weight: bolder;
  34.     color: #041986;
  35. }
  36. .TitleBar
  37. {
  38.   BACKGROUND-COLOR: #E5EAED;
  39.   Color:#565656;
  40.   FONT-FAMILY: 宋体;
  41.   font-weight:bold;
  42.   FONT-SIZE: 11pt;
  43. }
  44. .TextBox
  45. {
  46.     FONT-FAMILY: 宋体;
  47.     FONT-SIZE: 10pt;
  48.     height: 20px;
  49.     BORDER-BOTTOM: 1pt solid #C6C6C6;
  50.     BORDER-LEFT: 1pt solid #C6C6C6;
  51.     BORDER-RIGHT: 1pt solid #C6C6C6;
  52.     BORDER-TOP: 1pt solid #C6C6C6;
  53. }
  54. .InputGridTable{
  55.   FONT-FAMILY: 宋体;
  56.   FONT-SIZE: 10pt;
  57.     border-collapse: collapse;
  58.     border-color:#C6C6C6;
  59.     border-style: solid;
  60.     border-width: 1;
  61.     padding: 0;
  62. }
  63. .TitleColumn{
  64.     background-color: #E8ECF0;
  65.     nowrap="nowrap";
  66.   HEIGHT: 20px
  67. }
  68. .NButton
  69. {
  70.     cursor:hand;
  71.     width: 87px;
  72.     height: 20px;
  73.     font-family: 宋体;
  74.     font-size: 12px;
  75.     text-align:center ;
  76.     background-image: url(btn_bkg.gif);
  77.     border:0px;
  78. }
  79. </style>
  80. <script language="javascript">
  81. var count = 1;
  82. function delAttacheFile(){
  83.     var targetRow = event.srcElement.parentElement.parentElement;
  84.     InputTable.deleteRow(targetRow.rowIndex);
  85. }
  86. function addAttacheFile(){
  87.     count ++;
  88.     var row = InputTable.insertRow(InputTable.rows.length);
  89.     var firstCell = row.insertCell(0);
  90.     firstCell.className = "TitleColumn";
  91.     firstCell.width = "10%";
  92.     firstCell.height = "20";
  93.     firstCell.innerHTML = "<strong> 附件 " + count+ " :</strong>";
  94.     var lastCell = row.insertCell(1);
  95.     lastCell.height = "20";
  96.     lastCell.innerHTML = "<input type='file' name='attacheFile" + count + "' size='50' class='TextBox'> <input type='button' value='删除附件 " + count + "' onclick='delAttacheFile();' class='NButton'>";
  97. }
  98. </script>
  99. </head>
  100. <body>
  101.   <form name="filesForm" method="POST" action="ProcessFileUpload.jsp" enctype="multipart/form-data">
  102.     <table id="InputTable" border="1" cellpadding="0" cellspacing="0" class="InputGridTable" width="100%" height="40" >
  103.       <tr>
  104.         <td nowrap="nowrap" height="20" colspan="3" class="TitleColumn"><div align="center"><strong>附件列表:</strong></div></td>
  105.       </tr>
  106.       <tr>
  107.         <td nowrap="nowrap" width="10%" height="20" class="TitleColumn"><strong>附件 1 :</strong></td>
  108.         <td height="20" nowrap="nowrap"><input type="file" class="TextBox" name="attacheFile1" size="50"> <input name="adfile" type="button" class="NButton" onClick="addAttacheFile();" value="添加附件"></td>
  109.       </tr>
  110.     </table>
  111.     <table id="SubmitTable" border="1" cellpadding="0" cellspacing="0" class="InputGridTable" width="100%" height="20" >
  112.       <tr>
  113.         <td height="20" align="center" nowrap="nowrap">
  114.           <input type="submit" name="close" value="Close" onClick="self.close();" class="NButton"/>
  115.           <input type="reset" name="reset" value="Clear" class="NButton"/>
  116.           <input type="submit" name="Submit" value="Upload" class="NButton"/> 
  117.         </td>
  118.       </tr>
  119.     </table>
  120.   </form>
  121.     <p> </p>
  122. </body>
  123. </html>