站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题用JSP/Servlets实现Web方式的文件上传

用JSP/Servlets实现Web方式的文件上传

摘要: 用JSP/Servlets实现Web方式的文件上传



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

<body>




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();
}
//

<form name="uploadform" method="post" target=_self>


<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" </td>
</tr>
<tr>
<td align="center" colspan=2 >
<input type="button" value="upload" onclick="return

<input type="button" value="Cancel" onclick="return
</td>

</tr>
</form>
</table>

close this window



this.focus();
//

</body>

2. JSP File
<%@ page language="java" import="com.jspsmart.upload.*,
java.sql.*,java.util.* "%>
<%-- use SmartUpload bean --%>
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("Upload File Fail!
"JavaScript:window.history.back();">back
");
out.println("Notes:");

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

return;

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

 

%>

3. Servlet File (you can do it yourself)
↑返回目录
前一篇: Java Servlet基本方法介绍
后一篇: Servlet 是什么?