ckeditor上传文件

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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值