kindeditor上传图片问题!急在线等!

ap2008354121 2011-06-10 01:41:21
下了一个kindeditor配置好可以用了,远程图片没问题,上传本地图片报:上传目录不存在。
看到网上说的很多,可都不打适用我的项目,麻烦大家给看一下。struts2+kindeditor
upload_json.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.json.simple.*" %>
<%

//文件保存目录路径
String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
//文件保存目录URL
String saveUrl = request.getContextPath() + "/attached/";
//定义允许上传的文件扩展名
String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
//最大文件大小
long maxSize = 1000000;

response.setContentType("text/html; charset=UTF-8");

if(!ServletFileUpload.isMultipartContent(request)){
out.println(getError("请选择文件。"));
return;
}
//检查目录
File uploadDir = new File(savePath);
if(!uploadDir.isDirectory()){
out.println(getError("上传目录不存在。"));
return;
}
//检查目录写权限
if(!uploadDir.canWrite()){
out.println(getError("上传目录没有写权限。"));
return;
}
//创建文件夹
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String ymd = sdf.format(new Date());
savePath += ymd + "/";
saveUrl += ymd + "/";
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
String fileName = item.getName();
long fileSize = item.getSize();
if (!item.isFormField()) {
//检查文件大小
if(item.getSize() > maxSize){
out.println(getError("上传文件大小超过限制。"));
return;
}
//检查扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.<String>asList(fileTypes).contains(fileExt)){
out.println(getError("上传文件扩展名是不允许的扩展名。"));
return;
}

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
try{
File uploadedFile = new File(savePath, newFileName);
item.write(uploadedFile);
}catch(Exception e){
out.println(getError("上传文件失败。"));
return;
}

JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl + newFileName);
out.println(obj.toJSONString());
}
}
%>
<%!
private String getError(String message) {
JSONObject obj = new JSONObject();
obj.put("error", 1);
obj.put("message", message);
return obj.toJSONString();
}
%>
有人说是拦截器的问题,web.xml:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/action/*</url-pattern>
</filter-mapping>
images.html
var imageUploadJson = (typeof KE.g[id].imageUploadJson == 'undefined') ? '../../jsp/upload_json.jsp' : KE.g[id].imageUploadJson;
...全文
5050 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
昼与夜 2012-10-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

呵呵,忘了已经解决了!
<%@ page language="java" pageEncoding="GBK"%>
<%@page
import="java.util.*,java.io.*,
org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper,
java.util……
[/Quote]


好文啊 ,搞了这么久终于搞出来了!!!谢谢了 !!!

Lv792877883 2012-08-29
  • 打赏
  • 举报
回复
楼主求修改好的kindeditor,怎么改本地上传都是服务器故障,我是菜鸟,把你的发一下吧,792877883@qq.com,谢谢,感激不尽啊
淡轻语 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

呵呵,忘了已经解决了!
<%@ page language="java" pageEncoding="GBK"%>
<%@page
import="java.util.*,java.io.*,
org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper,
java.util……
[/Quote]确实可以,我前面也是用你的第一种方法,他就建了个文件夹没保存图片,换了下面的方法可以了
ss348277454 2012-03-05
  • 打赏
  • 举报
回复
把你的包给我发一个 看看 ,谢谢楼主了 ,,348277454@qq.com
achieve91 2012-03-02
  • 打赏
  • 举报
回复
请问你是怎么解决的??能把这个文件包发给我吗??465225193@qq.com 谢谢了
ap2008354121 2011-11-24
  • 打赏
  • 举报
回复
呵呵,忘了已经解决了!
<%@ page language="java" pageEncoding="GBK"%>
<%@page
import="java.util.*,java.io.*,
org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper,
java.util.concurrent.locks.*"%>
<%
MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;
String fileName = wrapper.getFileNames("imgFile")[0];
File file = wrapper.getFiles("imgFile")[0];
final Lock lock = new ReentrantLock();
String newName = null;
lock.lock();
try {
newName = System.currentTimeMillis()
+ fileName.substring(fileName.lastIndexOf("."),
fileName.length());
}finally {
lock.unlock();
}
FileOutputStream fos = new FileOutputStream(request.getSession()
.getServletContext().getRealPath("/")
+ "kindeditor/attached\\" + newName);
String newFileName = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ request.getContextPath() + "/kindeditor/attached/" + newName;
byte[] buffer = new byte[1024];
InputStream in = new FileInputStream(file);
try {
int num = 0;
while ((num = in.read(buffer)) > 0) {
fos.write(buffer, 0, num);
}
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
in.close();
fos.close();
}
//发送给KE
out.println("<html><head><title>Insert Image</title><meta http-equiv='content-type' content='text/html; charset=gbk'/></head><body>");
out.println("<script type='text/javascript'>");
out.println("parent.parent.KE.plugin['image'].insert('"
+ wrapper.getParameter("id") + "','" + newFileName + "','"
+ wrapper.getParameter("imgTitle") + "','"
+ wrapper.getParameter("imgWidth") + "','"
+ wrapper.getParameter("imgHeight") + "','"
+ wrapper.getParameter("imgBorder") + "','"
+ wrapper.getParameter("align") + "');</script>");
out.println("</body></html>");
%>
希望能帮助后面的朋友!
福来哥 2011-07-14
  • 打赏
  • 举报
回复
把这段改一下,如果上传目录不存,则新建一个就是了。

//检查目录
File uploadDir = new File(savePath);
if(!uploadDir.isDirectory()){
uploadDir.mkdirs();
//out.println(getError("上传目录不存在。"));
return;
}
xiaolu211 2011-07-14
  • 打赏
  • 举报
回复
//文件保存目录路径
String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
//文件保存目录URL
String saveUrl = request.getContextPath() + "/attached/";

更改成绝对路径试试

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧