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
}
}