1.html5 mybatis 实现图片上传(FTP上传和本地Tomcat上传)
首先配置config
CKEDITOR.editorConfig = function( config ) {
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.toolbar = 'Full'; //设置默认为Full
config.toolbar_Full =
[
['-','Save','Preview','-'],
['-','Print'],
['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
[ 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'ImageButton', 'HiddenField'],
['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Link','Unlink'],
['Image'],
['TextColor','BGColor'] ,
['Styles','Format','Font','FontSize'],
];
//config.language = 'zh-cn';
//config.uiColor = '#F7B42C';
config.toolbarCanCollapse = true;//工具栏可以被收缩
config.removeDialogTabs = 'image:advanced';//隐藏上传图片的高级选项
config.removePlugins = 'elementspath';//BODY标签
config.resize_enabled = false;//全屏
config.image_previewText=' '; //预览文本
config.height = 400;//设置编辑器的高
config.removeDialogTabs = 'image:advanced;image:Link'; //去掉上传图片中的链接选项
//上传请求地址/uploadfilehttp://10.5.172.162:9090/开启图片上传?attachType=310&relationId=&orgId=123
// config.filebrowserImageUploadUrl= "http://10.204.129.48:9092/eamproject/upload/uploadFile.do?attachType=310&relationId=&orgId=123";
//测试环境图片上传
config.filebrowserImageUploadUrl= "http://10.204.129.48:9092/eamproject/sysman/Announcement/imageUploadUrl.serv";
//config.filebrowserImageUploadUrl= "http://127.0.0.1:8080/eamproject/uploadfile/uploadFile.do";
//开启附件上传fileUploadAccessory.serv 10.5.172.162:9090
config.filebrowserUploadUrl ="http://10.204.129.48:9092/eamproject/sysman/Announcement/fileUpload.serv";
// http://10.204.129.48:9092/eamproject/upload/uploadFile.do
//测试环境文件上传
};
方法
@RequestMapping("/imageUploadUrl.serv")
@ResponseBody
public void imageUpload(HttpServletRequest request, HttpServletResponse response) {
String DirectoryName = "images";
try {
AnnouncementController.ckeditor(request, response, DirectoryName);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// ********************************//
private static List<String> fileTypes = new ArrayList<String>();
static {
fileTypes.add(".jpg");
fileTypes.add(".jpeg");
fileTypes.add(".bmp");
fileTypes.add(".gif");
fileTypes.add(".png");
fileTypes.add(".rar");
}
public static String upload(HttpServletRequest request, String DirectoryName)
throws IllegalStateException, IOException {
// 创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 图片名称
String fileName = null;
// 判断 request 是否有文件上传,即多部分请求
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 记录上传过程起始时的时间,用来计算上传时间
// int pre = (int) System.currentTimeMillis();
// 取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
// 取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();// 1.jpg
// 如果名称不为“”,说明该文件存在,否则说明该文件不存在
if (myFileName.trim() != "") {
// 获得图片的原始名称
String originalFilename = file.getOriginalFilename();// 1.jpg
// 获得图片后缀名称,如果后缀不为图片格式,则不上传
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();// .jpg
if (!fileTypes.contains(suffix)) {
continue;
}
// 获得上传路径的绝对路径地址(/upload)-->
// ***
// 获得上传路径的绝对路径地址(/upload)-->
String realPath = request.getSession().getServletContext().getRealPath("/");
System.out.println(realPath);// D:\software\apache-tomcat-8.0.9\webapps\eamproject
int web = realPath.lastIndexOf("\\");
String uploadPath = realPath.substring(0, web);
String aString = "\\html5-pc-2016\\assets";
uploadPath += "\\html5-pc-2016\\assets";
System.out.println(uploadPath);// D:\software\apache-tomcat-8.0.9\webapps\html5-pc-2016\assets\images
// ***
realPath = uploadPath + "\\" + DirectoryName;
String realPath1 = request.getSession().getServletContext().getRealPath("/" + DirectoryName);
System.out.println(realPath);
// 如果路径不存在,则创建该路径
File realPathDirectory = new File(realPath);
if (realPathDirectory == null || !realPathDirectory.exists()) {
realPathDirectory.mkdirs();
}
// 重命名上传后的文件名 111112323.jpg
fileName = java.util.UUID.randomUUID().toString() + suffix;
// 定义上传路径 .../upload/111112323.jpg
File uploadFile = new File(realPathDirectory + "\\" + fileName);
System.out.println(uploadFile);
file.transferTo(uploadFile);
}
}
// 记录上传该文件后的时间
// int finaltime = (int) System.currentTimeMillis();
// System.out.println(finaltime - pre);
}
}
return fileName;
}
public static void ckeditor(HttpServletRequest request, HttpServletResponse response, String DirectoryName)
throws IOException {
String fileName = upload(request, DirectoryName);// be5b9756-4840-4dce-b117-7d50c3cea2da.jpg
// 结合ckeditor功能
// imageContextPath为图片在服务器地址,如upload/123.jpg,非绝对路径
String imageContextPath = "\\html5-pc-2016\\assets" + "\\" + DirectoryName + "\\" + fileName;/// eamproject/upload//92ab9c94-5a34-4e3f-91a0-0d8ab8eff83a.jpg
String images = "../../assets/images/" + fileName;
response.setContentType("text/html;charset=UTF-8");
String callback = request.getParameter("CKEditorFuncNum");// 0
PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + images + "',''" + ")");
out.println("</script>");
out.flush();
out.close();
}
2.html5 mybatis 实现FTP上传
String imageContextPath = "ftp://eamftpuser:密码@10.5.172.162/" + fileName;
String images="ftp://eamftpuser:密码@10.5.172.162/" + fileName;
response.setContentType("text/html;charset=UTF-8");
//String callback = request.getParameter("CKEditorFuncNum");// 0
//PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + images + "',''" + ")");
out.println("</script>");
out.flush();
out.close();