使用JSP/Servlet上傳檔案

來源:互聯網
上載者:User
js|servlet
使用 JSP/ Servlet上傳檔案正成為一項常用的任務。以下是一個簡單的常式,使用了jspsmart的一個免費的組件。你可以在JSPSMART網站進行下載。

1. Html File
<html>
<body>
<script LANGUAGE="javascript" SRC="JSFunction.js">
</script>
<script language="JavaScript">
<!--
function checkForm(){

if (document.uploadform.adsImage.value==""){
alert("You must choose what image file to upload!");
return false;
}
document.uploadform.submit();
}
function cancelUpload(){
window.close();
window.opener.focus();
}
//-->
</script>
<form name="uploadform" method="post"
action="uploadImage.jsp" ENCTYPE="multipart/form-data"
target=_self>
<center>
<table border="1" width="65%">
<tr>
<td colspan=2 nowrap align=center>廣告圖片上傳</td>

</tr>
<tr>
<td width="15%" nowrap >廣告圖片:</td>
<td width="50%" nowrap><input type="file"
name="adsImage" size="20"></td>
</tr>
<tr>
<td align="center" colspan=2 >
<input type="button" value="upload" >
   
<input type="button" value="Cancel" >
</td>

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

2. JSP File
<%@ page language="java" import="com.jspsmart.upload.*,
java.sql.*,java.util.* "%>
<%-- use SmartUpload bean --%>
<jsp:useBean id="mySmartUpload" scope="page"
class="com.jspsmart.upload.SmartUpload" />

<%

//Initialization
mySmartUpload.init(config);
mySmartUpload.service(request,response);

//Set Restriction
mySmartUpload.setAllowedFilesList("gif,bmp,jpeg,jpg");
mySmartUpload.setTotalMaxFileSize(51200);



// Upload
try{
mySmartUpload.upload();
}
catch(Exception e){
out.println("<font color=red>Upload File Fail!</font>
<a href=
"JavaScript:window.history.back();">back</a><br>");
out.println("<UI>Notes:</UI>");

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

return;

}
//save file to disk
mySmartUpload.getFiles().getFile(0).saveAs("/" +
"FileName");

 

%>



3. Servlet File (you can do it yourself)


資料來源:碧波山莊Visual Basic http://ctsvb.126.com


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.