package com.myupload;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("utf-8");
List<String> fileTypes = new ArrayList<String>();
PrintWriter out = resp.getWriter();
String callback = req.getParameter("CKEditorFuncNum");//js验证
//允许文件上传格式
fileTypes.add(".jpg");
fileTypes.add(".jpeg");
fileTypes.add(".bmp");
fileTypes.add(".gif");
fileTypes.add(".png");
/*
*
* 设置上传文件的大小
*
*
*
*/
// super.doPost(req, resp);
// 创建文件项工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();
// 创建用于解析请求数据的组件
ServletFileUpload upload = new ServletFileUpload(factory);
// 开始解析数据
try {
List<FileItem> list = upload.parseRequest(req);
for (int i = 0; i < list.size(); i++) {
// 从列表中遍历每个文件项
System.out.println(list.size());
FileItem item = list.get(i);
if (item.isFormField()) {// 判断文件项是否为表单类容
// 判断是否为指定的表单字段
if ("user".equals(item.getFieldName())) {
// 输出表单字段的值
System.out.println(111);
System.out.println(item.getString());
}
} else {
System.out.println(222);
String fileName = item.getName();
System.out.println(fileName);
// 获取文件扩展名
String extName = fileName.substring(fileName
.lastIndexOf("."));
if(!fileTypes.contains(extName)){
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback
+ ",''," + "'文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)');");
out.println("</script>");
continue;
}
System.out.println(extName);
// 生成UUID作为文件名
String newName = UUID.randomUUID().toString();
// 获取服务器上自定义存放文件的目录
String rootPath = req.getServletContext().getRealPath(
"\\upload");
System.out.println(req.getServletContext());
String newPath = rootPath + "/" + newName + extName;
System.out.println(newPath);
String newImageUrl = req.getContextPath() + "/" + "upload/"
+ newName + extName;
System.out.println("newImageUrl=" + newImageUrl);
item.write(new File(newPath));// 写入文件
System.out.println("文件上传成功");
resp.setContentType("text/html;charset=UTF-8");
out.println("<script type=\"text/javascript\">");
System.out.println("<woooooohgggoo");
out.println("window.parent.CKEDITOR.tools.callFunction("
+ callback + ",'" + newImageUrl + "',''" + ")"); // 相对路径用于显示图片
out.println("</script>");
System.out.println("woooooooo");
out.flush();
out.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("utf-8");
List<String> fileTypes = new ArrayList<String>();
PrintWriter out = resp.getWriter();
String callback = req.getParameter("CKEditorFuncNum");//js验证
//允许文件上传格式
fileTypes.add(".jpg");
fileTypes.add(".jpeg");
fileTypes.add(".bmp");
fileTypes.add(".gif");
fileTypes.add(".png");
/*
*
* 设置上传文件的大小
*
*
*
*/
// super.doPost(req, resp);
// 创建文件项工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();
// 创建用于解析请求数据的组件
ServletFileUpload upload = new ServletFileUpload(factory);
// 开始解析数据
try {
List<FileItem> list = upload.parseRequest(req);
for (int i = 0; i < list.size(); i++) {
// 从列表中遍历每个文件项
System.out.println(list.size());
FileItem item = list.get(i);
if (item.isFormField()) {// 判断文件项是否为表单类容
// 判断是否为指定的表单字段
if ("user".equals(item.getFieldName())) {
// 输出表单字段的值
System.out.println(111);
System.out.println(item.getString());
}
} else {
System.out.println(222);
String fileName = item.getName();
System.out.println(fileName);
// 获取文件扩展名
String extName = fileName.substring(fileName
.lastIndexOf("."));
if(!fileTypes.contains(extName)){
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback
+ ",''," + "'文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)');");
out.println("</script>");
continue;
}
System.out.println(extName);
// 生成UUID作为文件名
String newName = UUID.randomUUID().toString();
// 获取服务器上自定义存放文件的目录
String rootPath = req.getServletContext().getRealPath(
"\\upload");
System.out.println(req.getServletContext());
String newPath = rootPath + "/" + newName + extName;
System.out.println(newPath);
String newImageUrl = req.getContextPath() + "/" + "upload/"
+ newName + extName;
System.out.println("newImageUrl=" + newImageUrl);
item.write(new File(newPath));// 写入文件
System.out.println("文件上传成功");
resp.setContentType("text/html;charset=UTF-8");
out.println("<script type=\"text/javascript\">");
System.out.println("<woooooohgggoo");
out.println("window.parent.CKEDITOR.tools.callFunction("
+ callback + ",'" + newImageUrl + "',''" + ")"); // 相对路径用于显示图片
out.println("</script>");
System.out.println("woooooooo");
out.flush();
out.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}