最近一个系统使用了百度的编辑器,使用的时候发现不能上传图片,一开始是不能加载出上传路径,我一看后台报错说找不到Uploader.class,我到编辑器路径/ueditor/jsp/下面找到了这个类,把这个类放到了源文件下面,问题解决。
但是接着上传完成后提示上传成功,但图片显示不存在,是一个叉,网上看到一个解决方案如下,按这个方法处理果然可以上传图片了:
所有的工作都做完的情况下,上传图片就是不成功,捕获异常呢也捕获不到,最后设置断点之后才知道fii.hasNext()返回为false,根本原因就是:
因为我用的是S2SH框架,在web.xml中struts2过滤器中把*.jsp去掉,如下代码应该去掉,那就OK了:
1 <filter-mapping> 2 <filter-name>struts2</filter-name> 3 <url-pattern>*.jsp</url-pattern> 4 </filter-mapping>
1 public void upload() throws Exception{ 2 boolean isMultipart = ServletFileUpload.isMultipartContent(this.request); 3 if (!isMultipart) { 4 this.state = this.errorInfo.get("NOFILE"); 5 return; 6 } 7 DiskFileItemFactory dff = new DiskFileItemFactory(); 8 String savePath = this.getFolder(this.savePath); 9 dff.setRepository(new File(savePath)); 10 try { 11 ServletFileUpload sfu = new ServletFileUpload(dff); 12 sfu.setSizeMax(this.maxSize * 1024); 13 sfu.setHeaderEncoding("gbk"); 14 FileItemIterator fii = sfu.getItemIterator(this.request); 15 while (fii.hasNext()) { 16 FileItemStream fis = fii.next(); 17 if (!fis.isFormField()) { 18 this.originalName = fis.getName().substring(fis.getName().lastIndexOf(System.getProperty("file.separator")) + 1); 19 if (!this.checkFileType(this.originalName)) { 20 this.state = this.errorInfo.get("TYPE"); 21 continue; 22 } 23 this.fileName = this.getName(this.originalName); 24 this.type = this.getFileExt(this.fileName); 25 this.url = savePath + "/" + this.fileName; 26 BufferedInputStream in = new BufferedInputStream(fis.openStream()); 27 FileOutputStream out = new FileOutputStream(new File(this.getPhysicalPath(this.url))); 28 BufferedOutputStream output = new BufferedOutputStream(out); 29 Streams.copy(in, output, true); 30 this.state=this.errorInfo.get("SUCCESS"); 31 //UE中只会处理单张上传,完成后即退出 32 break; 33 } else { 34 String fname = fis.getFieldName(); 35 //只处理title,其余表单请自行处理 36 if(!fname.equals("pictitle")){ 37 continue; 38 } 39 BufferedInputStream in = new BufferedInputStream(fis.openStream()); 40 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 41 StringBuffer result = new StringBuffer(); 42 while (reader.ready()) { 43 result.append((char)reader.read()); 44 } 45 this.title = new String(result.toString().getBytes(),"gbk"); 46 reader.close(); 47 } 48 } 49 } catch (Exception e) { 50 e.printStackTrace(); 51 this.state = this.errorInfo.get("UNKNOWN"); 52 } 53 }