UEditor自定义上传图片路径上传失败

  • 默认上传
    参见UEditor官网说明
  • 自定义请求地址
    将文件上传到第三方服务器并将地址返回编辑器显示,具体设置仍请参见官网说明
  • 请求地址代码编写
    此处使用jsp(也是造成bug的原因之一)
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ page import="org.apache.commons.fileupload.*" %>
    <%@ page import="org.apache.commons.fileupload.util.*" %>
    <%@ page import="org.apache.commons.fileupload.servlet.*" %>
    <%@ page import="org.apache.commons.fileupload.FileItemIterator" %>
    <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
    <%@ page import="java.util.regex.Matcher" %>
    <%@ page import="java.util.regex.Pattern" %>
    <%@ page import="java.util.UUID" %>
    <%@ page import="jcifs.smb.SmbFile" %>
    <%@ page import="jcifs.smb.SmbFileOutputStream" %>
    <%@ page import="java.util.Random" %>
    <%@ page import="org.apache.commons.lang3.StringUtils" %>
    <%@ page import="com.base.utils.SMBUtil" %>
    <%@ page import="java.io.*" %>
    <%@ page import="com.base.commons.Constants" %>
    <%@ page import="java.text.SimpleDateFormat" %>
    <%@ page import="java.util.Date" %>
    
    <%
    //文件临时路径
    String monthFile = new SimpleDateFormat("yyyy-MM").format(new Date());
    String filePath = Constants.uploadimg;
    String tmpPath = request.getRealPath("/") ;
    if(!tmpPath.endsWith(File.separator)){
        tmpPath  = tmpPath + File.separator + filePath;
    }else{
        tmpPath = tmpPath + filePath;
    }
    request.setCharacterEncoding("utf-8");
    //判断路径是否存在,不存在则创建
    File dir = new File(tmpPath);
    if(!dir.isDirectory())
        dir.mkdir();
    if(ServletFileUpload.isMultipartContent(request)){
        DiskFileItemFactory dff = new DiskFileItemFactory();
        ///设置内存缓冲区,超过后写入临时文件
        dff.setSizeThreshold(1024000);
        //临时存放目录
        dff.setRepository(dir);
        ServletFileUpload upload = new ServletFileUpload(dff);
        FileItemIterator fileItem = upload.getItemIterator(request);
        String title = "";   //图片标题
        //String url = "";    //图片地址
        String fileName = "";
        String realFileName ="";
        String originalName = "";
        String state="SUCCESS";
        String ftype = "";

        try{
            while(fileItem.hasNext()){
                FileItemStream stream = fileItem.next();
                
                    if(!stream.isFormField() && stream.getName().length()>0){
                        fileName = stream.getName().toLowerCase();
                        Pattern reg=Pattern.compile("[.](jpg|png|jpeg|gif|flv|swf|mkv|avi|rm|rmvb|mpeg|mpg|ogg|ogv|mov|wmv|mp4|webm|mp3|wav|mid)$");
                        Matcher matcher=reg.matcher(fileName);
                        if(!matcher.find()) {
                            state = "文件类型不允许!";
                            break;
                        }
                        ftype = matcher.group();
                        realFileName = UUID.randomUUID().toString().replace("-","") + ftype;
                        System.out.println(realFileName);
                      
                        SMBUtil.uploadFile(
                                Constants.upload_path + "/" + filePath + "/" + monthFile + "/",
                                stream.openStream(),
                                realFileName);
                        
                    }else{
                        String fname = stream.getFieldName();
                        if(fname.indexOf("fileName")!=-1){
                            BufferedInputStream in = new BufferedInputStream(stream.openStream());
                            byte c [] = new byte[10];
                            int n = 0;
                            while((n=in.read(c))!=-1){
                                originalName = new String(c,0,n);
                                break;
                            }
                            in.close();

                        }
                        if(fname.indexOf("pictitle")!=-1){
                            BufferedInputStream in = new BufferedInputStream(stream.openStream());
                            byte c [] = new byte[10];
                            int n = 0;
                            while((n=in.read(c))!=-1){
                                title = new String(c,0,n);
                                break;
                            }
                            in.close();
                        }
                    }
            }
            
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();         
        }
        title = fileName;
        title = title.replace("&", "&").replace("'", "&qpos;").replace("\"", """).replace("<", "<").replace(">", ">");
        String basePath = Constants.tomfile;
        String filePathModify= basePath + "/" + filePath + "/" + monthFile + "/" + realFileName;
        response.getWriter().print("{\"original\":\""+originalName+"\",\"url\":\""+ filePathModify +"\",\"title\":\""+title+"\",\"state\":\""+state+"\"}");
    }
    %>

在hasNext()判断时,一直是false,导致不能正常上传图片

  • bug原因
    一行日志暴露了可能的原因
DEBUG [org.apache.struts2.dispatcher.Dispatcher.getSaveDir:635] - saveDir=c:\tmp
DEBUG [org.apache.struts2.dispatcher.multipart.MultiPartRequest.parse:94] - Found item upload
DEBUG [org.apache.struts2.dispatcher.multipart.MultiPartRequest.parse:116] - Item is a file upload

struts2的文件上传拦截器已经对文件流进行了操作,造成上述jsp在执行时根本拿不到对应的文件流

而问题的发生的根本原因在于web.xml文件中struts配置的问题,struts2拦截了所有的请求,所以在正常的action文件上传时没有问题,但ueditor的jsp文件上传时就会无法获取到文件流

//.....默认拦截器定义
//错误   

      struts2
      /*
   

//正确

      struts2
      *action
 

你可能感兴趣的:(UEditor自定义上传图片路径上传失败)