百度编辑器,图片上传优化

首先有个前提

要先使用nginx,这个东西的用法百度下,在这里它将作为一个共享电脑中图片的工具

 

直接贴我改过后的imageUp.jsp代码

 
<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ page import="ueditor.Uploader"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="com.huobao.web.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.text.SimpleDateFormat" %>
response.setContentType("text/html");
//路径的保存地址 
String rootPath = Constants.IMG_PATH
+ Constants.IMG_ACTIVITY_CONTENT_PATH;
var HOSTURL = "http://127.0.0.1/";
//当前日期
Date data = new Date();
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMdd");
String nowDate = timeFormat.format(data);

//远程访问地址 例如:http://127.0.0.1/...
String url = Constants.IMG_ACTIVITY_CONTENT_PATH;
DiskFileUpload dfu = new DiskFileUpload();
File foler = new File(rootPath+nowDate+"/");
if(!foler.exists()){
	foler.mkdirs();
}
List fileItems = dfu.parseRequest(request);
Iterator it = fileItems.iterator();
String fileUpName = "";

while(it.hasNext()){
	System.out.println("it is ok");
	FileItem file = (FileItem)it.next();
	file.getString("UTF-8");
	if(!file.isFormField()){
		Date dt = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSS");
		String   fileFormatName   =   sdf.format(dt);
		String fileName = file.getName();
		fileUpName = fileFormatName+fileName;
		File finalFile = new File(rootPath+nowDate+"/"+fileUpName);
		file.write(finalFile);
	}
}

response.getWriter().print(
		"{'original':'" + fileUpName + "','url':'"+url+nowDate+"/"+fileUpName+"','title':'" + fileUpName
				+ "','state':'" + "SUCCESS" + "'}");


 

其中上传图片的代码是肯定能上传的,最大的一点就是注意路径的问题,这个是关键!!

你可能感兴趣的:(百度编辑器,图片上传优化)