您的位置:首页 > Web前端 > JavaScript

使用smartupload进行文件上传

2011-11-28 22:00 337 查看
     关于文件上传的代码,只有百度一下都能找到一大堆。但是可惜大多数都使用的是jsp来处理上传,而使用servlet处理文件上传的文章是比较少的。但是不管是jsp还是servlet处理都会跳转但是这样就很难看了。这就要使用到js内嵌的框架了。这样页面看起来就不会跳转了。下面就一步一步的来写下这个程序吧。

1.下载smartupload的jar包,不是jspsmartupload.这一步就大家自己弄了,我就不多说了。

2.编写文件上传的表单,enctype不能忽略,提交的方法要选择post方法

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
<script type="text/javascript" language="JavaScript">
function callback(msg)
{

document.getElementById("msg").innerHTML = "<font color=red>"+msg+"</font>";
}
function f()
{
var i=1;
for(i=1;i<6;i++)
{
var s=document.getElementById("pic"+i).value;
if(""==s||s==null){
alert("必须上传5个图片文件");

return false ;
}
}

}

</script>
<body>
<div id="uploadimg">
<form action="CheckUpload" id="form1" name="form1" encType="multipart/form-data"  method="post" target="hidden_frame" onsubmit="return f()">
<label>图片1</label>
<input type="file" id="pic1" name="pic1"/><br>
<label>图片2</label>
<input type="file" id="pic2" name="pic2"/><br>
<label>图片3</label>
<input type="file" id="pic3" name="pic3"/><br>
<label>图片4</label>
<input type="file" id="pic4" name="pic4"/><br>
<label>图片5</label>
<input type="file" id="pic5" name="pic5"/><br>
<font color="blue">支持JPG,JPEG,GIF,BMP,png文件的上传</font>
<iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe>
<span id="msg"></span><br>
<input type="submit" value="上传"/>
<input type="reset" value="取消"/>
</form>
</div>
</body>
</html>


 

这里对上传的文件做了简单判断,这里就多了 <iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe> 以及target="hidden_frame",span就用来显示反馈信息。

3.编写servlet处理文件上传

在servlet可以使用下面的2句取得pagecontext

 JspFactory jspf=JspFactory.getDefaultFactory();

 PageContext context=jspf.getPageContext(this, request, response, null, true, 1024, true);

至于这5个参数大家可以去查文档

 

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import org.lxh.smart.SmartUpload;

public class CheckUpload extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("GBK");
PrintWriter out=response.getWriter();
//新建一个SmartUpload对象
SmartUpload su = new SmartUpload();

//上传初始化
JspFactory jspf=JspFactory.getDefaultFactory();
PageContext context=jspf.getPageContext(this, request, response, null, true, 1024, true);
su.initialize(context);

boolean sign = true;

//4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
try {
su.setDeniedFilesList("exe,bat,jsp,htm,html");

su.upload();
//String name = su.getRequest().getParameter("uname") ;
IPTimeStamp its = new IPTimeStamp() ;	// 取得客户端的IP地址
java.io.File f=new java.io.File(this.getServletContext().getRealPath("/")+"images");

if(!f.exists())
{
f.mkdirs();
}
for(int x=0;x<su.getFiles().getCount();x++){
String ext = su.getFiles().getFile(x).getFileExt() ;	// 扩展名称
String fileName = its.getIPTimeRand() + "." + ext ;

su.getFiles().getFile(x).saveAs(f+java.io.File.separator + fileName) ;
}
//将上传文件保存到指定目录

} catch (Exception e) {
out.print("<script>parent.callback('文件类型不支持或者上传失败')</script>");
sign = false;
}

if(sign==true)
{

out.print("<script>parent.callback('文件上传成功')</script>");

}
}

}

 

下面先来看下效果截图,这里我选择了一个exe文件



下面是上传成功的截图



 

到这里文章就写好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息