熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

使用JSP/Servlet上載文件

2013-11-15 12:03:09  來源: JSP教程 

  使用 JSP/ Servlet上載文件正成為一項常用的任務以下是一個簡單的例程使用了jspsmart的一個免費的組件你可以在JSPSMART站點進行下載

Html File
<html>
<body>
<script LANGUAGE="javascript" SRC="JSFunctionjs">
</script>
<script language="JavaScript">
<!
function checkForm(){

if (documentuploadformadsImagevalue==""){
alert("You must choose what image file to upload!");
return false;
}
documentuploadformsubmit();
}
function cancelUpload(){
windowclose();
windowopenerfocus();
}
//>
</script>
<form name="uploadform" method="post"
action="uploadImagejsp" ENCTYPE="multipart/formdata"
target=_self>
<center>
<table border="" width="%">
<tr>
<td colspan= nowrap align=center>廣告圖片上載</td>

</tr>
<tr>
<td width="%" nowrap >廣告圖片:</td>
<td width="%" nowrap><input type="file"
name="adsImage" size=""></td>
</tr>
<tr>
<td align="center" colspan= >
<input type="button" value="upload" onclick="return
checkForm();" >
   
<input type="button" value="Cancel" onclick="return
cancelUpload();">
</td>

</tr>
</form>
</table>
<!
<a href="Javascript:windowclose();">close this window</a>
>
<script language="JavaScript">
<!
thisfocus();
//>
</script>
</body>

JSP File
<%@ page language="java" import="comjspsmartupload*
javasql*javautil* "%>
<% use SmartUpload bean %>
<jsp:useBean id="mySmartUpload" scope="page"
class="comjspsmartuploadSmartUpload" />

<%

//Initialization
mySmartUploadinit(config);
mySmartUploadservice(requestresponse);

//Set Restriction
mySmartUploadsetAllowedFilesList("gifbmpjpegjpg");
mySmartUploadsetTotalMaxFileSize();



// Upload
try{
mySmartUploadupload();
}
catch(Exception e){
outprintln("<font color=red>Upload File Fail!</font>
<a href=
"JavaScript:windowhistoryback();">back</a><br>");
outprintln("<UI>Notes:</UI>");

outprintln("<UL>You must set correct File
Name</UL>");
outprintln("<UL>You file size must be less than
K</UL>");
outprintln("<UL>You can only upload Gif jpeg jpg and
bmp files</UL>");

return;

}
//save file to disk
mySmartUploadgetFiles()getFile()saveAs("/" +
"FileName");



%>



Servlet File (you can do it yourself)

 


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20172.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.