apache檔案上傳元件+oracle+SWFupload實現檔案上傳
上傳頁:
<%@ page language="java" import="java.util.*" pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />
<title>upload page</title>
<link href="../../style/Common.css" rel="stylesheet" type="text/css" />
<link href="../../style/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/SWFUpload.js"></script>
<script type="text/javascript" src="/js/upload_callbacks.js"></script>
<script type="text/javascript">
var swfu;
window.onload = function() {
// Max settings
MAIN_FORM.FILE_CMMT.focus();
swfu = new SWFUpload({
create_ui : true,
debug : false,
upload_script : "../web/es/ES0123_Action.jsp",
target : "SWFUploadTarget",
flash_path : "/js/SWFUpload.swf",
allowed_filesize : 102400, // 30 MB
allowed_filetypes : "*.*",
allowed_filetypes_description : "All files...",
file_upload_limit:10,
browse_link_ID : "browse_link",
upload_link_ID : "upload_link",
flash_loaded_callback : 'swfu.flashLoaded',
upload_file_queued_callback : "fileQueuedByCustomize",
upload_file_start_callback : 'uploadFileStart',
upload_progress_callback : 'uploadProgress',
upload_file_complete_callback : 'uploadFileComplete',
upload_file_cancel_callback : 'uploadFileCancelled',
upload_queue_complete_callback : 'uploadQueueComplete',
upload_error_callback : 'uploadError',
upload_cancel_callback : 'uploadCancel',
auto_upload : false,
upload_file_error_callback:"whenerror"
});
//fix flash refresh;
swfu.loadFlash();
}
</script>
<script type="text/javascript">
function doAction(){
// var listingfiles = document.getElementById("fileList");
// var rowLength = listingfiles.rows.length;
var dis_value = document.getElementById("bg_02").style.display;
if(dis_value=="none"){
alert("There isn't file to upload!");
return ;
}
MAIN_FORM.method="POST";
MAIN_FORM.action="ES0123_Action.jsp";
MAIN_FORM.submit();
}
function keyPress() {
var eventKey = window.event.keyCode ;
if( eventKey == 13 ) {
doAction();
}
}
document.onkeypress = keyPress ;
function validate(){
var author = MAIN_FORM.FILE_WRITER.value;
if(author==""){
alert("The author cann't be null!");
MAIN_FORM.FILE_WRITER.focus();
return ;
}
}
</script>
</head>
<body>
<div id="Layer1" align="center">
<form name="MAIN_FORM"
target="hidden_frame">
<input type="hidden" name="ACTION" value="INDEX" />
<input type="hidden" name="IDX_ID"
value="<%=request.getParameter("IDX_ID")%>" />
<input type="hidden" name="FILE_PATH"
value="<%=request.getParameter("FILE_PATH")%>" />
<input type="hidden" name="USERID"
value="<%=request.getParameter("USERID")%>" />
<input type="hidden" name="NUM" id="NUM_ID"/>
<input type="hidden" name="NAME" id="NAME_ID"/>
<br />
<table width="640" align="center" border="0">
<tr>
<td width="5" height="21">
<img src="../../image/img_title.gif" />
</td>
<td class="title" align="left">
upload file
</td>
<td width="400" align="right">
<a href="#" id="upload_link" ><img
src="../../image/btn/btn_file_upload.gif" width="80"
height="23" border="0" onclick="doAction();"/> </a>
</td>
</tr>
</table>
<table width="602" height="70" cellpadding="5" cellspacing="0"
align="center" bordercolor="dde4f7" border="5"
style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table width="377" border="0" align="left" cellpadding="1"
cellspacing="1">
<tr>
<td align="left">
<label>
SELECT_FILE:
</label>
</td>
<td width="100%" align="left" valign="top">
<div class="but_01" valign="top">
<span id="show_id"></span>
<input type="button" id="browse_link" name="button"
class="input" value="select file" />
</div>
<div id="bg_02" style="display:none">
<div class="note">
limit:all files size is not more 250M all 5 files,one file
is not more 50 M
</div>
<div align="right">
<input type="button" id="clear_all" name="button"
class="input" value="clear files"
onclick="javascript:cancelQueue();" />
</div>
<div class="file">
<span class="font_red" id="fileCount">0</span> file
upload,all
<span class="font_red" id="fileTotleSize">0K</span>
</div>
<div class="table">
<table width="100%" border="0" cellpadding="5"
cellspacing="1">
<tr>
<td bgcolor="#EAEAEA">
<strong>file name</strong>
</td>
<td width="150" bgcolor="#EAEAEA">
<strong>file size</strong>
</td>
<td width="47" bgcolor="#EAEAEA">
<strong>clear</strong>
</td>
</tr>
</table>
<div id="box"
style="overflow:scroll; height:180px; width:100%; font-size: 12px; color: #999999; font-style: normal;">
<table id="fileList" cellpadding="5" cellspacing="1">
</table>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td align="left">
<label>
FILE_CMMT:
</label>
</td>
<td align="left" width="100%">
<label>
<textarea name="FILE_CMMT" rows="4" cols="76.5" class="input" onblur="validate();"></textarea>
</label>
</td>
</tr>
<tr>
<td align="left">
<label>
FILE_WRITER:
</label>
</td>
<td align="left" width="100%">
<label>
<input type="text" size="78" name="FILE_WRITER" class="input" value="<%=request.getParameter("USERID")%>" onblur="validate();"/>
</label>
</td>
</tr>
</table>
</td>
</tr>
</table>
<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>
</form>
</div>
</body>
</html>
上傳處理:
<%@ page language="java" import="java.util.*" pageEncoding="EUC-KR"%>
<%@ page import="com.idhc.base.bean.MapBEAN"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileUploadException"%>
<%
Map map = new HashMap();
MapBEAN map2 = new MapBEAN();
byte[] file = null;
String name = null;
String contentType = request.getContentType();
if(contentType.indexOf("multipart/form-data") >= 0){
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(10 * 1024 * 1024);
fu.setSizeThreshold(1024 * 1024);
Integer cnt = (Integer)session.getAttribute("cnt");
try {
List fileItems = fu.parseRequest(request);
Iterator iterator = fileItems.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
name = item.getName();
name = new String(name.getBytes("ISO-8859-1"),"EUC-KR");
file = item.get();
String FileType=item.getContentType();
String FileSize = String.valueOf(item.getSize());
map.put("FILE_SAV_NM",name);
map.put("FILE_TYPE",FileType);
map.put("FILE_SIZE",FileSize);
map.put("file",file);
if(cnt==null){
cnt = new Integer(1);
}else{
cnt = new Integer(cnt.intValue()+1);
}
session.setAttribute("cnt",cnt);
if(session.getAttribute("map"+cnt)!=null){
session.removeAttribute("map"+cnt);
}
session.setAttribute("map"+cnt,map);
}
}
}catch (FileUploadException e) {
e.printStackTrace();
}
}else{
String IDX_ID = request.getParameter("IDX_ID");
String FILE_PATH = request.getParameter("FILE_PATH");
String USERID = request.getParameter("USERID");
String FILE_WRITER = request.getParameter("FILE_WRITER");
String FILE_CMMT = request.getParameter("FILE_CMMT");
map2.set("IDX_ID",IDX_ID);
map2.set("FILE_PATH",FILE_PATH);
map2.set("USERID",USERID);
map2.set("FILE_WRITER",FILE_WRITER);
map2.set("FILE_CMMT",FILE_CMMT);
if(session.getAttribute("map")!=null){
session.removeAttribute("map");
}
session.setAttribute("map",map2);
}
%>
oracle存入資料庫:
public boolean uploadFile(Connection con, HttpServletRequest request,String NOTICE_ID) throws IOException {
int NUM = Integer.parseInt(request.getParameter("NUM"));
String NAME = request.getParameter("NAME");
NAME = NAME.substring(0,NAME.length()-1);
String names[] = NAME.split(";");
HttpSession session = request.getSession(true);
MapBEAN map = null;
boolean boo = false;
int cnt = 0;
Map file_map = new HashMap();
if(session.getAttribute("map")!=null){
map = (MapBEAN) session.getAttribute("map");
}
for(int i=1;i<=NUM;i++){
if(session.getAttribute("map"+i)!=null){
file_map = (Map) session.getAttribute("map"+i);
String FILE_CMMT = map.getString("FILE_CMMT");
String FILE_WRITER = map.getString("FILE_WRITER");
String FILE_PATH = map.getString("FILE_PATH");
//String IDX_ID = map.getString("IDX_ID");
String AUDIT_ID = map.getString("USERID");
String FILE_SAV_NM = names[i-1];
String FILE_SIZE = (String) file_map.get("FILE_SIZE");
String FILE_TYPE = (String) file_map.get("FILE_TYPE");
byte[] fileContent = (byte[]) file_map.get("file");
SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat time = new SimpleDateFormat("HHmmss");
String AUDIT_DATE = date.format(new Date());
String AUDIT_TIME = time.format(new Date());
PreparedStatement ptmt = null;
StringBuffer query = new StringBuffer();
query.setLength(0);
query
.append("INSERT INTO T_STTL_DOC_LST /n");
query
.append("VALUES ( /n");
query
.append("T_STTL_DOC_LST_SEQ.NEXTVAL /n");
query
.append(",?,?,?,?,?,? /n");
query
.append(",? /n");
query
.append(",?,? ,EMPTY_BLOB(),?,?,'N' /n");
query
.append(" ) ");
try {
ptmt = con.prepareStatement(query.toString());//insert
ptmt.clearParameters();
ptmt.setString(1, FILE_WRITER);
ptmt.setString(2, FILE_SAV_NM);
ptmt.setString(3, AUDIT_DATE);
ptmt.setString(4, FILE_CMMT);
ptmt.setString(5, FILE_PATH);
ptmt.setString(6, NOTICE_ID);
ptmt.setString(7, AUDIT_ID);
ptmt.setString(8, AUDIT_DATE);
ptmt.setString(9, AUDIT_TIME);
ptmt.setString(10, FILE_SIZE);
ptmt.setString(11, FILE_TYPE);
int cnt2 = ptmt.executeUpdate();
if(cnt2!=0){
cnt++;
}
closeStatement(ptmt);
ptmt = con.prepareStatement(" SELECT T_STTL_DOC_LST_SEQ.CURRVAL FROM DUAL ");//lock
ResultSet rs = ptmt.executeQuery();
int sequence = -1;
if (rs.next())
sequence = rs.getInt(1);
closeResultSet(rs);
closeStatement(ptmt);
query.setLength(0);
query.append("SELECT FILE_DAT /n");
query.append("FROM T_STTL_DOC_LST /n");
query.append("WHERE FILE_ID = ? /n");
query.append("FOR UPDATE ");
ptmt = con.prepareStatement(query.toString());//read into db
ptmt.setInt(1, sequence);
ResultSet rs2 = ptmt.executeQuery();
while(rs2.next()){
weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob
OutputStream out = blob.getBinaryOutputStream();
out.write(fileContent);
out.close();
}
closeResultSet(rs2);
closeStatement(ptmt);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ptmt != null) {
closeStatement(ptmt);
}
}
}
if(session.getAttribute("map"+i)!=null){
session.removeAttribute("map"+i);
}
}
if(cnt==NUM){
boo = true;
}
if(session.getAttribute("map")!=null){
session.removeAttribute("map");
}
if(session.getAttribute("cnt")!=null){
session.removeAttribute("cnt");
}
return boo;
}
我用的是weblogic伺服器,所以在上傳的時候blob型別要用weblogic內建的檔案型別而不是oracle的
SWFupload元件我上傳到我的資源中,供大家下載:
http://download.csdn.net/user/guying1028