DOC

2943.docjava

By Valerie Warren,2014-10-12 14:16
15 views 0
2943.docjava

    由于程序的原因?本文件未被完整保存。

    首页 ? 研究文集 ? JSP/Servlet/JSF

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

    Sunny.Xiao 原创 (参与分?3746?专家分?181) 发表?2005-03-21 20:52 版本?1.0 阅读?674

    目前想用Spring+Hibernate+Struts修改可是还没有完成,先共享代码出来:

    使用的librariescommons-fileupload-1.0.jar,Oraclejdbc驱动. 1. <!--$Header: ProcessFileUpload.jsp 1.0.0 2004/10/22 15:10:19 pkm ship $--> 2.

    3. <%@ page contentType="text/html;charset=GB2312"%>

    4. <%@ page import="org.apache.commons.fileupload.DiskFileUpload"%> 5. <%@ page import="org.apache.commons.fileupload.FileItem"%> 6. <%@ page import="java.util.*"%>

    7. <%@ page import="java.io.File"%>

    8. <%@ page import="java.sql.*" %>

    9. <html>

    10.<head>

    11.<style>

    12..NButton

    13.{

    14. cursor:hand; 15. width: 87px;

    16. height: 20px;

    17. font-family: 宋体;

    18. font-size: 12px;

    19. text-align:center ;

    20. background-image: url(btn_bkg.gif);

    21. border:0px;

    22.}

    23.</style>

    24.

    25.<%!

    26.// Connect to Oracle database and Insert into cux_upload_files 27.public void dbInsert(String p_c_file_name,String p_c_path,String p_s_file_name,String p_s_path) {

    28. Connection conn = null;

    29. String connStr;

    30. try {

    31. connStr="jdbc:oracle:thin:@local:1521:orcl";

    32. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 33. conn = DriverManager.getConnection(connStr,"apps", "apps"); 34. // Insert into table

    35. conn.setAutoCommit(false);

36. 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) "

    37. + " VALUES (cux.cux_upload_files_s.nextval,?,?,?,?,?,SYSDATE) ");

    38.

    39. //insertCUF.setInt(1,2);

    40. insertCUF.setString(1,p_c_file_name); 41. insertCUF.setString(2,p_c_path);

    42. insertCUF.setString(3,p_s_file_name); 43. insertCUF.setString(4,p_s_path);

    44. insertCUF.setString(5,"XXX");

    45. insertCUF.executeUpdate();

    46.

    47. conn.commit();

    48.

    49. conn.setAutoCommit(true);

    50.

    51. conn.close();

    52. }catch(SQLException ex) { // Handle SQL errors 53. System.out.println("Error in Connecting to the Database "+'\n'+ex.toString());

    54. }

    55.}

    56.

    57.//

    58.String getCurDate(){

    59. GregorianCalendar gcDate = new GregorianCalendar(); 60. int year = gcDate.get(GregorianCalendar.YEAR); 61. int month = gcDate.get(GregorianCalendar.MONTH); 62. int day = gcDate.get(GregorianCalendar.DAY_OF_MONTH); 63. return "" + year + "-" + month + "-" + day; 64.}

    65.%>

    66.<meta http-equiv="Content-Type" content="text/html; charset=gb2312" >

    67.<title>Process File Upload</title>

    68.</head>

    69.<body>

    70.<table width="800" border bordercolor="#0000FF"> 71.<tr bgcolor="#66CCFF">

    72.<td colspan=1 rowspan=1 align=left valign=top> 73. <strong><font size=2 face="宋体" color=#000000> 74. <nobr>客户端文件</nobr>

    75. </font>

    76. </strong></td>

    77.<td colspan=1 rowspan=1 align=left valign=top> 78. <strong><font size=2 face="宋体" color=#000000> 79. <nobr>服务器文件</nobr>

80. </font>

    81. </strong></td>

    82.<td colspan=1 rowspan=1 align=left valign=top>

    83. <strong><font size=2 face="宋体" color=#000000>

    84. <nobr>上传用户</nobr>

    85. </font>

    86. </strong></td>

    87.</tr>

    88.<%

    89. //out.println("Content Type ="+request.getContentType());

    90.

    91. DiskFileUpload fu = new DiskFileUpload();

    92. // If file size exceeds, a FileUploadException will be thrown93. fu.setSizeMax(1000000);

    94. // maximum size that will be stored in memory

    95. fu.setSizeThreshold(4096);

    96. // the location for saving data that is larger than getSizeThreshold() 97. //fu.setRepositoryPath("/tmp/");

    98.

    99.

    100. List fileItems = fu.parseRequest(request);

    101. Iterator itr = fileItems.iterator();

    102. int i = 0;

    103.

    104. while(itr.hasNext()) {

    105. FileItem fi = (FileItem)itr.next();

    106. i++;

    107. //Check if not form field so as to only handle the file inputs 108. //else condition handles the submit button input 109. if (!fi.isFormField()) {

    110. String filename = fi.getName();

    111. long filesize = fi.getSize();

    112. String pUserName = "XIAOHUIPING";

    113. if((filename==null||filename.equals("")) && filesize==0) 114. continue;

    115. // 注意fi.getName()

    116. // 会返回上载文件在客户端的完整路径名称?这似乎是一个BUG 117. // 为解决这个问题?这里使用了fullFile.getName() 118. filename=filename.replace('\\','/');

    119. //new String(filename.getBytes("ISO-8859-1"),"UTF-8"); 120. File fullFile = new File(filename);

    121. // 指定fullFile.getName() = "Works.txt"; 122. File savedFile= new File(application.getRealPath("/Download/"), fullFile.getName());

    123. fi.write(savedFile);

124. // 上传文件成功后写入数据库表

    125. dbInsert(fullFile.getName(),filename.replace('/','\\'),fullFile.getName(),savedFile.getAbsolutePath()); 126. if ((i%2) == 0) {

    127. // 文件上载成功提示?以表格形式打印

    128. 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>"); 129. //out.println("<br>" + "Local Filename = " + "\"" + filename.replace('/','\\') + "\"" + " Upload To \"" + savedFile.getAbsolutePath() + "\"" + " Successful!!");

    130. }

    131. else if ((i%2) == 1) {

    132. 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>");

    133. }

    134. }

    135. }

    136. %>

    137. </table>

    138.

    139. <table width="800" border bordercolor="#0000FF">

    140. <tr>

    141. <td height="20" align="center" nowrap="nowrap">

    142. <DIV align="center">

    143. <input class="NButton" type="button" value="Back" onClick="javascript:history.back()"/>

    144. <input class="NButton" type="button" value="Close" onClick="javascript:window.close()"/>

    145. </DIV>

    146. </td>

    147. </tr>

    148. </table>

    149.

    150. </body>

    151. </html>

1. <!--$Header: index.jsp 1.0.0 2004/10/22 15:10:19 pkm ship $-->

    2.

    3. <%@ page contentType = "text/html;charset=gb2312" %>

    4. <html>

    5. <head>

    6. <title>数据文件上传</title>

    7. <style>

    8. BODY

9. {

    10. FONT-FAMILY: 宋体;

    11. FONT-SIZE: 10pt;

    12. background-color: #F6F6F6; 13. margin-top: 10px;

    14. margin-right: 50px; 15. margin-bottom: 50px; 16. margin-left: 10px;

    17. margin-top: 0px

    18. SCROLLBAR-FACE-COLOR: #D0E5FF; 19. SCROLLBAR-HIGHLIGHT-COLOR: #F5F9FF;

    20. SCROLLBAR-SHADOW-COLOR: #828282; 21. SCROLLBAR-3DLIGHT-COLOR: #828282; 22. SCROLLBAR-ARROW-COLOR: #797979; 23. SCROLLBAR-TRACK-COLOR: #ECECEC; 24. SCROLLBAR-DARKSHADOW-COLOR: #ffffff

    25.}

    26.TABLE

    27.{

    28. FONT-FAMILY: 宋体;

    29. FONT-SIZE: 10pt

    30.}

    31.

    32..HeaderTitle{

    33. font-family: 黑体;

    34. font-size: 30px;

    35. font-weight: bolder; 36. color: #041986;

    37.}

    38.

    39..TitleBar

    40.{

    41. BACKGROUND-COLOR: #E5EAED; 42. Color:#565656;

    43. FONT-FAMILY: 宋体;

    44. font-weight:bold;

    45. FONT-SIZE: 11pt;

    46.}

    47.

    48..TextBox

    49.{

    50. FONT-FAMILY: 宋体;

    51. FONT-SIZE: 10pt;

    52. height: 20px;

    53. BORDER-BOTTOM: 1pt solid #C6C6C6; 54. BORDER-LEFT: 1pt solid #C6C6C6; 55. BORDER-RIGHT: 1pt solid #C6C6C6; 56. BORDER-TOP: 1pt solid #C6C6C6; 57.}

    58.

    59..InputGridTable{

    60. FONT-FAMILY: 宋体;

    61. FONT-SIZE: 10pt;

    62. border-collapse: collapse; 63. border-color:#C6C6C6; 64. border-style: solid; 65. border-width: 1;

    66. padding: 0;

    67.}

    68.

    69.

    70..TitleColumn{

    71. background-color: #E8ECF0; 72. nowrap="nowrap";

    73. HEIGHT: 20px

    74.}

    75.

    76..NButton

    77.{

    78. cursor:hand;

    79. width: 87px;

    80. height: 20px;

    81. font-family: 宋体;

    82. font-size: 12px;

    83. text-align:center ; 84. background-image: url(btn_bkg.gif);

    85. border:0px;

    86.}

    87.</style>

    88.

    89.<script language="javascript"> 90.var count = 1;

    91.function delAttacheFile(){ 92. var targetRow = event.srcElement.parentElement.parentElement;

    93. InputTable.deleteRow(targetRow.rowIndex);

    94.}

    95.function addAttacheFile(){ 96. count ++;

97. var row = InputTable.insertRow(InputTable.rows.length);

    98. var firstCell = row.insertCell(0);

    99. firstCell.className = "TitleColumn";

    100. firstCell.width = "10%";

    101. firstCell.height = "20";

    102. firstCell.innerHTML = "<strong> 附件 " + count+ " :</strong>";

    103. var lastCell = row.insertCell(1);

    104. lastCell.height = "20";

    105. lastCell.innerHTML = "<input type='file' name='attacheFile" + count + "' size='50' class='TextBox'> <input type='button' value='删除附

     " + count + "' onclick='delAttacheFile();' class='NButton'>";

    106. }

    107.

    108.

    109. </script>

    110. </head>

    111.

    112. <body>

    113. <form name="filesForm" method="POST" action="ProcessFileUpload.jsp" enctype="multipart/form-data"> 114. <table id="InputTable" border="1" cellpadding="0" cellspacing="0" class="InputGridTable" width="100%" height="40" > 115. <tr>

    116. <td nowrap="nowrap" height="20" colspan="3" class="TitleColumn"><div align="center"><strong>附件列表:</strong></div></td>

    117. </tr>

    118. <tr>

    119. <td nowrap="nowrap" width="10%" height="20" class="TitleColumn"><strong>附件 1 :</strong></td> 120. <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>

    121. </tr>

    122. </table>

    123. <table id="SubmitTable" border="1" cellpadding="0" cellspacing="0" class="InputGridTable" width="100%" height="20" > 124. <tr>

    125. <td height="20" align="center" nowrap="nowrap">

    126. <input type="submit" name="close" value="Close" onClick="self.close();" class="NButton"/> 127. <input type="reset" name="reset" value="Clear" class="NButton"/>

    128. <input type="submit" name="Submit" value="Upload" class="NButton"/> 129. </td>

    130. </tr>

    131. </table>

    132. </form>

    133. <p> </p>

    134. </body>

    135. </html>

    版权声明 给作者写信 本篇文章对您是否有帮助? 投票? 投票结果? 作者其它文章?

    ; 利用AntXDoclet自动产生映射文件例子

    作者全部文章

     评论人?chenkw 参与分: 7879 专家分: 150 发表时间: 2005-03-22 11:57 附上源代包是不是?

     评论人?lovevrc 参与分: 7597 专家分: 505 发表时间: 2005-04-18 11:07 我对Spring+Hibernate不是很熟悉?Struts还好?

    你能不能发一个最简单但能运行的例子?关于Spring?或Hibernate?三者结合也可以?在此表示感谢?我的油箱是:missvrc@yahoo.com.cn

这个文章共有 2 条评论?共 1

    上一篇文章 下一篇文章 返回〔JSP/Servlet/JSF

Report this document

For any questions or suggestions please email
cust-service@docsford.com