package com.wxh.jQyery;
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;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* jQuery uploadify 实现文件上传
*
* @author wang_xh
*
* SoftWare Group 58156559
*
* 技术交流QQ群,欢迎大家加入
*
*/
public class JQUploayify extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 7942326848801679254L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
// 得到项目路径
String path = this.getServletContext().getRealPath("/");
String fileD = request.getParameter("folder");
path = path + fileD + "/";
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
ServletFileUpload sfu = new ServletFileUpload(new DiskFileItemFactory());
sfu.setHeaderEncoding("UFT-8");
try {
List fileList = sfu.parseRequest(request);
String name = "";
String extName = "";
for (int i = 0; i < fileList.size(); i++) {
FileItem fi = (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("."));
}
Calendar ca = Calendar.getInstance();
DecimalFormat df = new DecimalFormat();
df.setMinimumIntegerDigits(2);
String st = "650000";
if (st != null && st.length() > 6) {
st = st.substring(0, 6);
}
String dateTime = ca.get(Calendar.YEAR) + ""
+ df.format(ca.get(Calendar.MONDAY)) + ""
+ df.format(ca.get(Calendar.DATE)) + ""
+ df.format(ca.get(Calendar.HOUR)) + ""
+ df.format(ca.get(Calendar.MINUTE)) + ""
+ df.format(ca.get(Calendar.SECOND));
String fileName = st + "_SBGL_" + dateTime + extName;
File saveFile = new File(path + fileName);
fi.write(saveFile);
System.out.println(fileName + " 文件上传成功!");
}
}
response.getWriter().println("1");
} catch (FileUploadException e) {
response.getWriter().println("0");
e.printStackTrace();
} catch (Exception e) {
response.getWriter().println("0");
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
前往页