jquery uploadify 使用心得

1导出commmons-fileupload和commmons-io jar包

2index.jsp

<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() +"://"
   +request.getServerName() + ":" + request.getServerPort()
   + path +"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN">
<html>
 <head>
  <basehref="<%=basePath%>">

  <title>MyJSP 'index.jsp' startingpage</title>
  <metahttp-equiv="pragma" content="no-cache">
  <metahttp-equiv="cache-control" content="no-cache">
  <metahttp-equiv="expires" content="0">
  <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  <metahttp-equiv="description" content="This is mypage">
  <linkhref="JS/jquery.uploadify-v2.1.0/example/css/default.css"
   rel="stylesheet"type="text/css" />
  <linkhref="JS/jquery.uploadify-v2.1.0/uploadify.css"rel="stylesheet"
   type="text/css"/>

  <scripttype="text/javascript"
   src="JS/jquery.uploadify-v2.1.0/jquery-1.3.2.min.js"></script>

  <scripttype="text/javascript"
   src="JS/jquery.uploadify-v2.1.0/swfobject.js"></script>

  <scripttype="text/javascript"
   src="JS/jquery.uploadify-v2.1.0/jquery.uploadify.v2.1.0.min.js"></script>
       <script type="text/javascript">
        $(function(){
           $("#uploadify").uploadify({
              "uploader":"JS/jquery.uploadify-v2.1.0/uploadify.swf",
              "script":"UploadServlet",//请求后台方法的路径,我这里是servlet
              "cancelImg":'JS/jquery.uploadify-v2.1.0/cancel.png',
              "folder":"UploadFile", //文件上传路径
              "queueID":"fileQueue", //div层的id
              "auto":false, //是否立即上传
              "multi":true,  //是否支持多文件上传
              //"queueSizeLimit":10, //最多上传文件个数
              //"sizeLimit":10240, //文件的最大值
              //"fileExt":"*.jpg,*.png,*.gif,*.jpeg", //文件的格式
              //"fileDesc":"请选择jpg,png,gif,jpeg格式的文件" //设置文件上传格式显示文字
           });
        });
       </script>

 </head>

 <body>
  <divid="fileQueue"></div>
   <input type="file" name="uploadify" id="uploadify"/>
   <p>
     <ahref="javascript:$('#uploadify').uploadifyUpload()">上传</a>|
     <ahref="javascript:$('#uploadify').uploadifyClearQueue()">取消上传</a>
   </p>

 </body>
</html>
3UploadServlet.java

package com;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {

 
 public UploadServlet() {
  super();
 }

 
 public void destroy() {
  super.destroy(); // Just puts"destroy" string in log
  // Put your code here
 }

 public void doGet(HttpServletRequest request,HttpServletResponse response)
   throwsServletException, IOException {

  doPost(request,response);
 }

 public void doPost(HttpServletRequest request,HttpServletResponse response)
   throwsServletException, IOException {

  response.setContentType("text/html");
  response.setCharacterEncoding("utf-8");
  Stringpath=this.getServletContext().getRealPath("/");
  StringfileD=request.getParameter("folder");
  path=path+fileD+"/";
  File file=new File(path);
  if(!file.exists()){
   file.mkdirs();
  }
  ServletFileUpload sfu=newServletFileUpload(new DiskFileItemFactory());
  sfu.setHeaderEncoding("UTF-8");
  try{
   ListfileList=sfu.parseRequest(request);
   Stringname="";
   StringextName="";
   for(inti=0;i<fileList.size();i++){
    FileItemfi=(FileItem)fileList.get(i);
    if(!fi.isFormField()){
     name=fi.getName();
     if(name==null|| "".equals(name.trim())){
      continue;
     }
     if(name.lastIndexOf(".")>=0){
      extName=name.substring(name.lastIndexOf("."));
     }
     Calendarca=Calendar.getInstance();
     DecimalFormatdf=new DecimalFormat();
     df.setMinimumIntegerDigits(2);
     Stringst="650000";
     if(st!=null&&st.length()>6){
      st=st.substring(0,6);
     }
     StringdateTime=ca.get(Calendar.YEAR)+""
     +df.format(ca.get(Calendar.MONTH))+""
     +df.format(ca.get(Calendar.DATE))+""
     +df.format(ca.get(Calendar.HOUR))+""
     +df.format(ca.get(Calendar.MINUTE))+""
     +df.format(ca.get(Calendar.SECOND));
     StringfileName=st+"_hdn_"+dateTime+extName;
     FilesaveFile=new File(path+fileName);
     fi.write(saveFile);
     System.out.println(fileName+"文件上传成功!");
    }
   }
   response.getWriter().println("1");
  }catch(Exception e){
   e.printStackTrace();
   response.getWriter().println("0");
  }
 }

 
 public void init() throws ServletException{
  // Put your code here
 }

}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值