`
eric.zhang
  • 浏览: 124039 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

springmvc+dwz+xheditor实现上传图片及swf 视频

阅读更多
注意:如果使用了apache fileupload上传方法,则springmvc-servlet.xml的配置文件里不能再有multipartResolver的配置,两者不能共存。<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />


1前台JSP代码

$(document).ready(function(){
		//初始化在线编辑器
		//设置在线编辑器上传各类文件对应的url
		$("#newGJFrom #gjnr").xheditor({
			upLinkUrl:'${ctx}/fileupload/doXheditorUpload?flag=zip',
			upLinkExt:'zip,rar,txt',
			upImgUrl:'${ctx}/fileupload/doXheditorUpload?flag=img',
			upImgExt:'jpg,jpeg,gif,png',
			upFlashUrl:'${ctx}/fileupload/doXheditorUpload?flag=swf',
			upFlashExt:'swf',
			tools: 'Cut,Copy,Paste,Pastetext,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,SelectAll,Removeformat,Align,List,Outdent,Indent,Link,Unlink,Img,Flash,Table,Source,Preview,Print,Fullscreen',
			skin: 'vista'
		});
		//删除编辑器的 关于图标
		$("span a[name='About']").remove();
		
		
	});


//其他代码省略
<tr>
				<td colspan="3">
					<form:textarea name="gjnr"  path="gjnr" id="gjnr" rows="20" cols="83"/>
				</td>
			</tr>



兼容IE6,在线编辑器需要用如下的方法去写:
<textarea class="editor" name="gjnr" id="gjnr" name="description" rows="20" cols="83"
tools="Cut,Copy,Paste,Pastetext,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,SelectAll,Removeformat,Align,List,Outdent,Indent,Link,Unlink,Img,Flash,Table,Source,Preview,Print,Fullscreen"
upLinkUrl="${ctx}/fileupload/doXheditorUpload?flag=zip" upLinkExt="zip,rar,txt,doc,docx"
upImgUrl="${ctx}/fileupload/doXheditorUpload?flag=img" upImgExt="jpg,jpeg,gif,png"
upFlashUrl="${ctx}/fileupload/doXheditorUpload?flag=swf" upFlashExt="swf"
skin="vista"></textarea>


2后台controller 处理xheditor的文件上传 也是利用apache的fileupload组件实现



import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javacommon.base.BaseQuery;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/fileupload")
public class FileUploadController {

	
	/**
	 * 在线编辑器 上传单个本地图片 处理类
	 */
	@RequestMapping("/doXheditorUpload")
	public void doImgUpload(ModelMap model, BaseQuery query,HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		DiskFileItemFactory fac = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(fac);
		upload.setHeaderEncoding("utf-8");
		String err = "";
		//用于显示在在线编辑器里,图片的路径
		String newFileName = "";
		//允许的图片格式
		String fileExt = "";
		String flag = request.getParameter("flag");
		long maxSize = 0;
		if("swf".equals(flag)){
			fileExt = "swf";
			maxSize = 1024*1024*10;
		}
		if("img".equals(flag)){
			fileExt = "jpg,jpeg,bmp,gif,png";
			maxSize = 1024*1024;
		}
		if("media".equals(flag)){
			fileExt = "wmv,avi,wma,mp3,mid";
			maxSize = 1024*1024*10;
		}
		if("zip".equals(flag)){
			fileExt = "zip,rar,txt";
			maxSize = 1024*1024*10;
		}
		try {
			List<FileItem> items = upload.parseRequest(request);
			Map<String, Serializable> fields = new HashMap<String, Serializable>();
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (item.isFormField())
					fields.put(item.getFieldName(), item.getString());
				else
					fields.put(item.getFieldName(), item);
			}
			String folder=request.getSession().getServletContext().getRealPath("");
			String savePath = folder+File.separator+"xheditorUpload"+File.separator;
			File f1 = new File(savePath);
			System.out.println("---上传保存的目录:"+savePath);
			if (!f1.exists()) {
				f1.mkdirs();
			}
			/*获取表单的上传文件*/
			FileItem uploadFile = (FileItem)fields.get("filedata");
			/*获取文件上传路径名称*/
			String fileNameLong = uploadFile.getName();
			/*获取文件扩展名*/
			/*索引加1的效果是只取xxx.jpg的jpg*/
			String extensionName = fileNameLong.substring(fileNameLong.lastIndexOf(".") + 1);
			//System.out.println("extensionName:" + extensionName);
			/*检查文件类型*/
			if (("," + fileExt.toLowerCase() + ",").indexOf("," + extensionName.toLowerCase() + ",") < 0){
				printInfo(response, "不允许上传此类型的文件", "");
				return;
			}
			/*文件是否为空*/
			if (uploadFile.getSize() == 0){
				printInfo(response, "上传文件不能为空", "");
				return;
			}
			/*检查文件大小*/
			if (uploadFile.getSize() > maxSize){
				printInfo(response, "上传文件的大小超出限制,最大不能超过"+maxSize+"M", "");
				return;
			}
			/*文件存储的相对路径*/
			String saveDirPath = "/xheditorUpload/";
			/*文件存储在容器中的绝对路径*/
			String saveFilePath = request.getSession().getServletContext().getRealPath("") + saveDirPath;
			/*构建文件目录以及目录文件*/
			File fileDir = new File(saveFilePath);
			if (!fileDir.exists()) {fileDir.mkdirs();}
			/*重命名文件*/
			String filename = UUID.randomUUID().toString();
			File savefile = new File(saveFilePath + filename + "." + extensionName);
			/*存储上传文件*/
			uploadFile.write(savefile);
			//这个地方根据项目的不一样,需要做一些特别的定制。
			newFileName = "/bmxxfb"+saveDirPath + filename + "." + extensionName;		
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			newFileName = "";
			err = "错误: " + ex.getMessage();
		}
		printInfo(response, err, newFileName);
	}
	
	/**
	 * 使用I/O流输出 json格式的数据
	 * @param response
	 * @param err
	 * @param newFileName
	 * @throws IOException
	 */
	public void printInfo(HttpServletResponse response, String err, String newFileName) throws IOException {
		PrintWriter out = response.getWriter();
		//String filename = newFileName.substring(newFileName.lastIndexOf("/") + 1);
		out.println("{\"err\":\"" + err + "\",\"msg\":\"" + newFileName + "\"}");
		out.flush();
		out.close();
	}
	public static String getParameterByName(Iterator<FileItem> it,String key){
		Iterator i = it;
		String res="";
		while (i.hasNext()) {
			FileItem fi = (FileItem) i.next();
			//System.out.println(fi.getFieldName()+":"+fi.getString());
			if (fi.getFieldName().equals(key)) {
				res = fi.getString();
				break;
			}
		}
		return res;
	}
}



下面是xheditor的截图:



  • 大小: 73.5 KB
分享到:
评论
3 楼 kfjihailong 2013-01-07  
269565478@qq.com 求源码
2 楼 a442579302 2012-09-14  
楼主还在吗?
请问,ModelMap model, BaseQuery query  这两个参数是干什么的
1 楼 llww83 2012-06-28  
请问可以发份源码吗? 549135295@qq.com

相关推荐

Global site tag (gtag.js) - Google Analytics