- 默認(rèn)上傳
參見UEditor官網(wǎng)說明 - 自定義請求地址
將文件上傳到第三方服務(wù)器并將地址返回編輯器顯示,具體設(shè)置仍請參見官網(wǎng)說明 - 請求地址代碼編寫
此處使用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");
//判斷路徑是否存在,不存在則創(chuàng)建
File dir = new File(tmpPath);
if(!dir.isDirectory())
dir.mkdir();
if(ServletFileUpload.isMultipartContent(request)){
DiskFileItemFactory dff = new DiskFileItemFactory();
///設(shè)置內(nèi)存緩沖區(qū),超過后寫入臨時文件
dff.setSizeThreshold(1024000);
//臨時存放目錄
dff.setRepository(dir);
ServletFileUpload upload = new ServletFileUpload(dff);
FileItemIterator fileItem = upload.getItemIterator(request);
String title = ""; //圖片標(biāo)題
//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,導(dǎo)致不能正常上傳圖片
- 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的文件上傳攔截器已經(jīng)對文件流進(jìn)行了操作,造成上述jsp在執(zhí)行時根本拿不到對應(yīng)的文件流
而問題的發(fā)生的根本原因在于web.xml文件中struts配置的問題,struts2攔截了所有的請求,所以在正常的action文件上傳時沒有問題,但ueditor的jsp文件上傳時就會無法獲取到文件流
//.....默認(rèn)攔截器定義
//錯誤
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//正確
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*action</url-pattern>
</filter-mapping>