Use JSP and Servlet to Upload file to server is becoming a
common task now.
Below is a simple example use the jspsmart free component which
implements it.
You can download the free package from 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" onclick="return
checkForm();" >
<input type="button" value="Cancel" onclick="return
cancelUpload();">
</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)
common task now.
Below is a simple example use the jspsmart free component which
implements it.
You can download the free package from 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" onclick="return
checkForm();" >
<input type="button" value="Cancel" onclick="return
cancelUpload();">
</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)
解决方案 »
- 关于jsp修改Oracle数据库中记录的问题
- spring + hibernate 取得的primary key值比数据库里的值小1
- sql server 2000中的blob类型
- 如何辨认网站是用ASP.NET还是用JSP编写的.
- 登录页面的问题
- 近期看到许多朋友在问数据库连接池的问题,所以特地总结了一下.
- 我在翻译文章关于电子商务的,向大家请教
- (急得要命!)oracle数据库表中某一LONG字段内容是word类型的文档(ole形式嵌入),如何取出该字段内嵌的word文档内容?
- 求,帮助
- 在swing小程序中有一个jtree,怎样叶结点的单击事件中调用一个jsp页面(当然是在另一个fram中)
- 请问IIS和jsp服务器如何分工
- 你们是这样子编程吗?程序员是这样子吗?迷茫中............
common task now.
Below is a simple example use the jspsmart free component which
implements it.
You can download the free package from 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" onclick="return
checkForm();" >
<input type="button" value="Cancel" onclick="return
cancelUpload();">
</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)
http://www.jspsmart.com/