使用的libraries有commons-fileupload-1.0.jar,和Oracle的jdbc驱动.
- <!--$Header: ProcessFileUpload.jsp 1.0.0 2004/10/22 15:10:19 pkm ship $-->
- <%@ page contentType="text/html;charset=GB2312"%>
- <%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
- <%@ page import="org.apache.commons.fileupload.FileItem"%>
- <%@ page import="java.util.*"%>
- <%@ page import="java.io.File"%>
- <%@ page import="java.sql.*" %>
- <html>
- <head>
- <style>
- .NButton
- {
- cursor:hand;
- width: 87px;
- height: 20px;
- font-family: 宋体;
- font-size: 12px;
- text-align:center ;
- background-image: url(btn_bkg.gif);
- border:0px;
- }
- </style>
- <%!
- // Connect to Oracle database and Insert into cux_upload_files
- public void dbInsert(String p_c_file_name,String p_c_path,String p_s_file_name,String p_s_path) {
- Connection conn = null;
- String connStr;
- try {
- connStr="jdbc:oracle:thin:@local:1521:orcl";
- DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
- conn = DriverManager.getConnection(connStr,"apps", "apps");
- // Insert into table
- conn.setAutoCommit(false);
- 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) "
- + " VALUES (cux.cux_upload_files_s.nextval,?,?,?,?,?,SYSDATE) ");
- //insertCUF.setInt(1,2);
- insertCUF.setString(1,p_c_file_name);
- insertCUF.setString(2,p_c_path);
- insertCUF.setString(3,p_s_file_name);
- insertCUF.setString(4,p_s_path);
- insertCUF.setString(5,"XXX");
- insertCUF.executeUpdate();
- conn.commit();
- conn.setAutoCommit(true);
- conn.close();
- }catch(SQLException ex) { // Handle SQL errors
- System.out.println("Error in Connecting to the Database "+'\n'+ex.toString());
- }
- }
- //
- String getCurDate(){
- GregorianCalendar gcDate = new GregorianCalendar();
- int year = gcDate.get(GregorianCalendar.YEAR);
- int month = gcDate.get(GregorianCalendar.MONTH);
- int day = gcDate.get(GregorianCalendar.DAY_OF_MONTH);
- return "" + year + "-" + month + "-" + day;
- }
- %>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" >
- <title>Process File Upload</title>
- </head>
- <body>
- <table width="800" border bordercolor="#0000FF">
- <tr bgcolor="#66CCFF">
- <td colspan=1 rowspan=1 align=left valign=top>
- <strong><font size=2 face="宋体" color=#000000>
- <nobr>客户端文件</nobr>
- </font>
- </strong></td>
- <td colspan=1 rowspan=1 align=left valign=top>
- <strong><font size=2 face="宋体" color=#000000>
- <nobr>服务器文件</nobr>
- </font>
- </strong></td>
- <td colspan=1 rowspan=1 align=left valign=top>
- <strong><font size=2 face="宋体" color=#000000>
- <nobr>上传用户</nobr>
- </font>
- </strong></td>
- </tr>
- <%
- //out.println("Content Type ="+request.getContentType());
- DiskFileUpload fu = new DiskFileUpload();
- // If file size exceeds, a FileUploadException will be thrown
- fu.setSizeMax(1000000);
- // maximum size that will be stored in memory
- fu.setSizeThreshold(4096);
- // the location for saving data that is larger than getSizeThreshold()
- //fu.setRepositoryPath("/tmp/");
- List fileItems = fu.parseRequest(request);
- Iterator itr = fileItems.iterator();
- int i = 0;
- while(itr.hasNext()) {
- FileItem fi = (FileItem)itr.next();
- i++;
- //Check if not form field so as to only handle the file inputs
- //else condition handles the submit button input
- if (!fi.isFormField()) {
- String filename = fi.getName();
- long filesize = fi.getSize();
- String pUserName = "XIAOHUIPING";
- if((filename==null||filename.equals("")) && filesize==0)
- continue;
- // 注意fi.getName()
- // 会返回上载文件在客户端的完整路径名称,这似乎是一个BUG。
- // 为解决这个问题,这里使用了fullFile.getName()。
- filename=filename.replace('\\','/');
- //new String(filename.getBytes("ISO-8859-1"),"UTF-8");
- File fullFile = new File(filename);
- // 指定fullFile.getName() = "Works.txt";
- File savedFile= new File(application.getRealPath("/Download/"), fullFile.getName());
- fi.write(savedFile);
- // 上传文件成功后写入数据库表
- dbInsert(fullFile.getName(),filename.replace('/','\\'),fullFile.getName(),savedFile.getAbsolutePath());
- if ((i%2) == 0) {
- // 文件上载成功提示,以表格形式打印
- 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>");
- //out.println("<br>" + "Local Filename = " + "\"" + filename.replace('/','\\') + "\"" + " Upload To \"" + savedFile.getAbsolutePath() + "\"" + " Successful!!");
- }
- else if ((i%2) == 1) {
- 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>");
- }
- }
- }
- %>
- </table>
- <table width="800" border bordercolor="#0000FF">
- <tr>
- <td height="20" align="center" nowrap="nowrap">
- <DIV align="center">
- <input class="NButton" type="button" value="Back" onClick="javascript:history.back()"/>
- <input class="NButton" type="button" value="Close" onClick="javascript:window.close()"/>
- </DIV>
- </td>
- </tr>
- </table>
- </body>
- </html>
- <!--$Header: index.jsp 1.0.0 2004/10/22 15:10:19 pkm ship $-->
- <%@ page contentType = "text/html;charset=gb2312" %>
- <html>
- <head>
- <title>数据文件上传</title>
- <style>
- BODY
- {
- FONT-FAMILY: 宋体;
- FONT-SIZE: 10pt;
- background-color: #F6F6F6;
- margin-top: 10px;
- margin-right: 50px;
- margin-bottom: 50px;
- margin-left: 10px;
- margin-top: 0px
- SCROLLBAR-FACE-COLOR: #D0E5FF;
- SCROLLBAR-HIGHLIGHT-COLOR: #F5F9FF;
- SCROLLBAR-SHADOW-COLOR: #828282;
- SCROLLBAR-3DLIGHT-COLOR: #828282;
- SCROLLBAR-ARROW-COLOR: #797979;
- SCROLLBAR-TRACK-COLOR: #ECECEC;
- SCROLLBAR-DARKSHADOW-COLOR: #ffffff
- }
- TABLE
- {
- FONT-FAMILY: 宋体;
- FONT-SIZE: 10pt
- }
- .HeaderTitle{
- font-family: 黑体;
- font-size: 30px;
- font-weight: bolder;
- color: #041986;
- }
- .TitleBar
- {
- BACKGROUND-COLOR: #E5EAED;
- Color:#565656;
- FONT-FAMILY: 宋体;
- font-weight:bold;
- FONT-SIZE: 11pt;
- }
- .TextBox
- {
- FONT-FAMILY: 宋体;
- FONT-SIZE: 10pt;
- height: 20px;
- BORDER-BOTTOM: 1pt solid #C6C6C6;
- BORDER-LEFT: 1pt solid #C6C6C6;
- BORDER-RIGHT: 1pt solid #C6C6C6;
- BORDER-TOP: 1pt solid #C6C6C6;
- }
- .InputGridTable{
- FONT-FAMILY: 宋体;
- FONT-SIZE: 10pt;
- border-collapse: collapse;
- border-color:#C6C6C6;
- border-style: solid;
- border-width: 1;
- padding: 0;
- }
- .TitleColumn{
- background-color: #E8ECF0;
- nowrap="nowrap";
- HEIGHT: 20px
- }
- .NButton
- {
- cursor:hand;
- width: 87px;
- height: 20px;
- font-family: 宋体;
- font-size: 12px;
- text-align:center ;
- background-image: url(btn_bkg.gif);
- border:0px;
- }
- </style>
- <script language="javascript">
- var count = 1;
- function delAttacheFile(){
- var targetRow = event.srcElement.parentElement.parentElement;
- InputTable.deleteRow(targetRow.rowIndex);
- }
- function addAttacheFile(){
- count ++;
- var row = InputTable.insertRow(InputTable.rows.length);
- var firstCell = row.insertCell(0);
- firstCell.className = "TitleColumn";
- firstCell.width = "10%";
- firstCell.height = "20";
- firstCell.innerHTML = "<strong> 附件 " + count+ " :</strong>";
- var lastCell = row.insertCell(1);
- lastCell.height = "20";
- lastCell.innerHTML = "<input type='file' name='attacheFile" + count + "' size='50' class='TextBox'> <input type='button' value='删除附件 " + count + "' onclick='delAttacheFile();' class='NButton'>";
- }
- </script>
- </head>
- <body>
- <form name="filesForm" method="POST" action="ProcessFileUpload.jsp" enctype="multipart/form-data">
- <table id="InputTable" border="1" cellpadding="0" cellspacing="0" class="InputGridTable" width="100%" height="40" >
- <tr>
- <td nowrap="nowrap" height="20" colspan="3" class="TitleColumn"><div align="center"><strong>附件列表:</strong></div></td>
- </tr>
- <tr>
- <td nowrap="nowrap" width="10%" height="20" class="TitleColumn"><strong>附件 1 :</strong></td>
- <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>
- </tr>
- </table>
- <table id="SubmitTable" border="1" cellpadding="0" cellspacing="0" class="InputGridTable" width="100%" height="20" >
- <tr>
- <td height="20" align="center" nowrap="nowrap">
- <input type="submit" name="close" value="Close" onClick="self.close();" class="NButton"/>
- <input type="reset" name="reset" value="Clear" class="NButton"/>
- <input type="submit" name="Submit" value="Upload" class="NButton"/>
- </td>
- </tr>
- </table>
- </form>
- <p> </p>
- </body>
- </html>

