`
hyj0903
  • 浏览: 148827 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

Kindeditor实现图片上传(strut2版)

阅读更多

 平时没有注意

html编辑器上传图片的问题,这次做了个小项目,突然发现了。

html编辑器上传图片的问题,这次做了个小项目,突然发现了。

 

 

 

Strut2 结合kindeditor时出现图片不能上传的问题。

先是在网上找了很多办法,还是先回顾一下流程吧。呵呵

 

1.  换编辑器

先是sinaeditor 这个编辑器简单,可是默认只有asp版本的,后来在csdn上找了一个jsp版本的,还是不能上传图片,同时的错误。

再换,弄一个很大的编辑器FCKeditor ,在他的官方网站上下了java版本的,与struts2整合的时候,发现同样的错误,不能上传图片。

2.  三个编辑器的用过了,错误是同样的,不能上传图片。再搜索过程中发现,kindeditor一个中文版的论坛,下定决心用kindeditor。网址是:http://www.kindsoft.net/,有基论坛上有这样一篇文章http://www.kindsoft.net/view.php?bbsid=5&postid=656&pagenum=29 发现图片上传是通过一个iframe来实现无刷新的文件上传,再图片上传后返回的是json格式的信息,并通过ajax获得这些信息。

 

于是想出了如下解决办法:

1、利用struts2 原始的方法实现图片上传

2、在上传图片后,在action中利用PrintWriter打印json格式的数据。

 

代码如下:

1、demo.jsp

 

 

 

2.EditImageAdd.java

//*******************************************************************//
//
//** 创建人:   何岳军
//
//** 日  期:   2010-02-18
//
//** 描  述:  编辑器图片上传
//
//** 版  本:  
//
//*******************************************************************//

package eshop.action.admin;
import java.util.*;
import java.io.*;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONObject;

import com.opensymphony.xwork2.*;

import eshop.*;
import com.dao.*;

public class EditImageAdd extends ActionSupport {
	
	private String id;
	private String imgTitle;
	private String imgWidth;
	private String imgHeight;
	private String imgBorder;
	
	private List<File> imgFile;				//文件
	private List<String>imgFileFileName; 		//文件名
	private List<String> imgFileContentType; 	//文件路径
	private String fileurl;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getImgTitle() {
		return imgTitle;
	}

	public void setImgTitle(String imgTitle) {
		this.imgTitle = imgTitle;
	}

	public String getImgWidth() {
		return imgWidth;
	}

	public void setImgWidth(String imgWidth) {
		this.imgWidth = imgWidth;
	}

	public String getImgHeight() {
		return imgHeight;
	}

	public void setImgHeight(String imgHeight) {
		this.imgHeight = imgHeight;
	}

	public String getImgBorder() {
		return imgBorder;
	}

	public void setImgBorder(String imgBorder) {
		this.imgBorder = imgBorder;
	}

	public List<File> getImgFile() {
		return imgFile;
	}

	public void setImgFile(List<File> imgFile) {
		this.imgFile = imgFile;
	}

	public List<String> getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(List<String> imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public List<String> getImgFileContentType() {
		return imgFileContentType;
	}

	public void setImgFileContentType(List<String> imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	public String getFileurl() {
		return fileurl;
	}

	public void setFileurl(String fileurl) {
		this.fileurl = fileurl;
	}


	public String execute(){
		
		
		try{
			//上传图片
			
			FileUpTool fileUpTool= new FileUpTool();
			for(int i=0;i<imgFile.size();i++){
				if(fileUpTool.saveFile(imgFile.get(i), imgFileFileName.get(i),"attached")>0){
					this.fileurl=fileUpTool.getFileurl();
					String root = ServletActionContext.getRequest().getRealPath("attached");
					root+="\\";
	                String temp = "parent.KE.plugin[\"image\"].insert(\'"+id+"','/sansui/attached/"+this.fileurl+"','"+imgTitle+"','"+imgWidth+"','"+imgHeight+"','"+imgBorder+"');";
	                StringBuffer sb = new StringBuffer();
	                sb.append("<html>"); 
	                sb.append("<head>");       
	                sb.append("<title>Insert Image</title>");  
	                sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");   
	                sb.append("</head>");            
	                sb.append("<body>");        
	                sb.append("<script type=\"text/javascript\">"+temp+";</script>");   
	                sb.append("</body>");             
	                sb.append("</html>");         
	                String out = sb.toString();
					
	                try {   
	            		PrintWriter pw = null;  
	            		HttpServletResponse response = ServletActionContext.getResponse();
	            		response.setContentType("text/html;charset=gb2312");        
	                    response.setHeader("Cache-Control","no-cache");   
	                	//输出ajax内容            
	                	response.setHeader("Content-Type", "text/html;charset=gb2312");           
	                	response.getWriter().write(out );        
	                	} catch (IOException e) {            
	                		// TODO Auto-generated catch block            
	                		e.printStackTrace();        

	                }
					
					
				}
			}
			return null;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	
}

 

 

说明:其中的FileUpTool 是文件上传的功能类,很简单,就是一些方面的封装,自己实现一下。

 

3.struts.xml

	<!-- 编辑器上图片上传 -->
		<action name="adminEditImageAdd" class="eshop.action.admin.EditImageAdd">
			<interceptor-ref name="fileUpload">
					<param name="maximumSize">10485760</param><!-- maxinumSize: 10M -->
					<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/GIF,image/JPG,image/pjpeg,image/x-png</param>
				</interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>			
				
		</action>

 

 

上传的问题解决了,但是新的问题双来了,就是插入文章中的图片不能删除,kindeditor还没有这个功能。这个也要自己来实现。汗

  • 大小: 49.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics