需要组件jquery.uploadify.v2.1.4.js,上传的核心文件,支持上传各种文件
<link rel="stylesheet" type="text/css" href="common/css/uploadify.css"/>
<script type="text/javascript" src="common/js/jquery-1.6.js"></script>
<script type="text/javascript" src="common/js/swfobject.js"></script>
<script type="text/javascript" src="common/js/jquery.uploadify.v2.1.4.js"></script>
<script type="text/javascript">
jQuery.noConflict(); var $Upl = jQuery; $Upl(document).ready(function() { $Upl("#uploadify").uploadify({ 'uploader' : 'common/images/uploadify.swf', 'script' : '/CYJS/servlet/UploadFileServlet', //提交后台上传servlet或者action 'cancelImg' : 'common/images/cancel.png', 'folder' : 'upload', 'queueID' : 'custom-queue',//'fileDesc' : '支持格式:doc、pdf、txt', //支持的格式
//'fileExt' : '*.doc;*.pdf;*.txt', //不支持的格式 'auto' : true, 'multi' : false, 'removeCompleted': false, 'sizeLimit' : 10485799, 'simUploadLimit' : 1, 'buttonText' : '添加', 'onCancel' : function(event,queueId,fileObj,data) {//取消单个事件 $Upl("#fileSize").val(data.allBytesTotal); }, 'onSelectOnce' : function(event,data) {//选择文件事件 $Upl("#fileSize").val(data.allBytesTotal); }, 'onComplete' : function(event,queueId,fileObj,response,data) {//完成一次上传事件 var filePN = response.split(";");//取回后台传来的值 if(filePN[4]!=""){ $Upl("#uploadify" +queueId).find(".hcicsContent").text(filePN[4]); } addcontractelectronfile('#uploadify'+queueId,filePN[0],filePN[1],filePN[2],filePN[3],filePN[4]); }, 'onAllComplete' : function(event,data) {//完成所有文件上传事件 $Upl("#fileSize").val("0");//所有上传完成重置剩余文件大小 } }); });</script> <form id="addressForm" name="addressForm" method="post" action="" > <table class="stable tp0" cellSpacing="0" cellPadding="0" border="0" > <tr> <td width="15%" align="left" colspan="4" style="padding-left:158px" > <input type="hidden" id="fileSize" value="0"/> <div id="custom-queue"></div> 附件:<input id="uploadify" type="file" name="Filedata" /> </td></tr>
</table>
</form>java后台文件
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { try { String temp=request.getParameter("folder");//System.out.println("temp==="+temp);//上传文件的临时目录String tempDir=request.getSession().getServletContext().getRealPath("uploads");File tFile=new File(tempDir);if(!tFile.exists())tFile.mkdir();//创建缓冲区DiskFileItemFactory factory=new DiskFileItemFactory(); //设置缓冲区大小 1*1024*1024 设置1Mbfactory.setSizeThreshold(1*1024*1024);//设置缓冲临时目录factory.setRepository(new File(tempDir)); ServletFileUpload upload=new ServletFileUpload(factory); //设置编码upload.setHeaderEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8");//设置文件最大值,这里设置5Mb,5*1024*1024;upload.setSizeMax(5*1024*1024);FileItemIterator fileItem=upload.getItemIterator(request); //得到所有的文件while(fileItem.hasNext()){ FileItemStream fi= fileItem.next(); //获得文件流if(!fi.isFormField()&&fi.getName().length()>0){ //过滤非文件域String fileName=fi.getName(); //获取文件名称String fName=tempDir+"\\"+fileName; //文件最终存放目录 File file=new File(fName);FileOutputStream out=new FileOutputStream(file);out.write(fi.toString().getBytes());out.flush();out.close();}}Random r = new Random(10);response.getWriter().write(""+r.nextInt());} catch (Exception e) { System.out.println(e.getMessage());} }protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // TODO Auto-generated method stubdoGet(request,response); //默认调用POST}