日历

2008 8.22 Fri
     12
3456789
10111213141516
17181920212223
24252627282930
31      
«» 2008 - 8 «»

文章搜索

日志文章

2007年08月02日 12:54:55

smartupload使用实例

smartupload是一款常见的web文件上传下载工具,首先你得下载它的jar包,将其放在当前项目的lib目录下,或者放在tomcat下的common目录的lib目录下,我这里的附件里面提供下载

本次实例共有两个jsp页面,

1,fileload.jsp 文件选择页面

Copy code
<%@ page language="java" contentType="text/html; charset=gb2312"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="upload.jsp" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>name:
<input type="file" name="file" size="20"></td></tr>


<tr>
<td>
<input type="submit" value="上传">
</td>
</tr>
</table>
</form>
</body>
</html>



2.upload.jsp 真正的处理上传的页面
Copy code
<%@page language="java" contentType="text/html; charset=gb2312"%>
<%@page import="com.jspsmart.upload.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<center>the file is uploading...<br>

<%
try{
mySmartUpload.initialize(pageContext);
mySmartUpload.setMaxFileSize(1024*1024*1024);
//开始上传
mySmartUpload.upload();

File file=mySmartUpload.getFiles().getFile(0);
String fn=file.getFieldName();

//取得文件的扩展名
String ext=file.getFileExt();
if(!file.isMissing()){
//保存的绝对路径
file.saveAs("c:/upload/"+fn+"."+ext);
}

out.println("upload success,please check");

out.println("<a href=c:/upload/"+fn+"."+ext+">here</a>");
}

catch(Exception e)
{
e.printStackTrace();
}
%>
<br>
<a href=fileload.jsp>upload again!</a>

</body>
</html>


注意:我们这里的上传目的文件夹为C盘的upload文件夹,所以,你先得建立此文件夹。
还有,附件中对上传做了限制,我把其文件类型改成了.txt,你下载后再改成.jar包就可以了。



描述: smartuload
附件: jspsmartupload.txt (12 K)

Tags: jsp 文件   smart   upload  

类别: Java Web |  评论(0) |  浏览(8655) |  收藏
发表评论