关于图片上传到服务器并保存到数据库然后读出显示到页面上的一系列例子Word下载.docx
《关于图片上传到服务器并保存到数据库然后读出显示到页面上的一系列例子Word下载.docx》由会员分享,可在线阅读,更多相关《关于图片上传到服务器并保存到数据库然后读出显示到页面上的一系列例子Word下载.docx(8页珍藏版)》请在冰豆网上搜索。
DOCTYPEHTMLPUBLIC"
-//W3C//DTDHTML4.01Transitional//EN"
>
html>
head>
%
Stringpath=request.getContextPath();
BookDAObdao=newBookDAO();
Bookb=newBook();
Listbooklist=bdao.findAll();
if(booklist==null||booklist.size()==0){
booklist=newArrayList();
}
script>
functionaddUploadFile(){
var_table=document.getElementByIdx("
uploadTable"
);
varrowIndex=_table.getElementsByTagName_r("
tr"
).length-1;
var_tr=_table.insertRow(rowIndex);
var_td1=_tr.insertCell(0);
var_td2=_tr.insertCell
(1);
var_td3=_tr.insertCell
(2);
_tr.bgColor='
#E1E8F4'
;
_tr.setAttribute("
height"
"
27"
id"
rowIndex.toString());
_td1.setAttribute("
width"
15%"
align"
center"
_td2.setAttribute("
left"
colspan"
2"
_td3.setAttribute("
5%"
vartd1='
图片'
vartd2='
inputtype="
FILE"
name="
file'
+rowIndex.toString()+'
"
size="
20"
style="
width:
100%;
font-size:
12px;
border:
1pxsolid#555555;
'
vartd3='
button"
class="
btnDel"
value="
删除"
onclick="
delUploadFile('
_td1.appendChild(document.createTextNode(td1));
_td2.appendChild(document.createElement(td2));
_td3.appendChild(document.createElement(td3));
functiondelUploadFile(fileIndex){
if(confirm('
确定要删除么?
)){
_table.deleteRow(document.getElementByIdx(fileIndex.toString()).rowIndex);
functionsubmitPage(){
varform=document.getElementByIdx("
form1"
varflag=0;
for(i=0;
i<
input"
).length;
i++){
if("
)[i].type=="
file"
){
)[i].value=="
alert("
请不要有空的上传文件框"
)
flag=1;
}
if(flag==0){
form.submit();
/script>
/head>
body>
formname="
method="
post"
action="
/TianaiSearch/UploadImage"
ENCTYPE="
multipart/form-data"
tablewidth="
100%"
border="
0"
cellspacing="
1"
cellpadding="
bgcolor="
#869DD2"
tr>
td>
tableid="
width="
4"
align="
#ffffff"
tdheight="
40"
colspan="
spanclass="
style1ttstyle1"
strong>
上传文件信息<
/strong>
/span>
/td>
/tr>
trheight="
24"
#EAEEF7"
tdwidth="
对应文档
tdalign="
text"
file_id"
size="
来自书目
selectname="
book_id"
for(inti=0;
booklist.size();
b=(Book)booklist.get(i);
optionvalue="
%=b.getId()%>
%=b.getName()%>
/option>
/select>
开始页码
start_page_no"
#E1E8F4"
图片
upload"
trbgcolor="
tdcolspan="
addUpload"
添加文件"
addUploadFile();
&
nbsp;
smtbtn"
确认上传"
submitPage();
reset"
bt4"
重置"
/table>
/form>
/body>
/html>
二、UploadImage.servletdoPost方法
//实例化上载bean
mySmartUpload=new;
//初始化
mySmartUpload.initialize(this.getServletConfig(),request,response);
request.setCharacterEncoding("
//设置上载的最大值
mySmartUpload.setMaxFileSize(500*1024*1024);
//上载文件
try{
mySmartUpload.upload();
}catch(SmartUploadExceptione){
//TODO自动生成catch块
e.printStackTrace();
//循环取得所有上载的文件
for(inti=0;
i<
mySmartUpload.getFiles().getCount();
i++){
//取得上载的文件
myFile=mySmartUpload.getFiles()
.getFile(i);
if(!
myFile.isMissing()){
//取得上载的文件的文件名
StringmyFileName=myFile.getFileName();
//取得不带后缀的文件名
Stringsuffix=myFileName.substring(0,myFileName
.lastIndexOf