umeditor的jsp版本更改图片上传路径的方法

网上找了很多资料,貌似很多都不是说明的很清楚了,为了方便自己今后使用,在这里记录一下:

1、下载umeditor的jsp包

2、将umeditor整个放入工程自定义位置

3、将Uploader.java放到工程自定义位置

4、修改imageUp.jsp中导入的代码的位置:

由原先的

<%@ page import="com.baidu.ueditor.um.Uploader" %>
修改为

<%@ page import="自定义位置.Uploader" %>

5、修改umeditor/dialogs/image/image.js

由原先的

callback: function (editor, $w, url, state) {

            if (state == "SUCCESS") {
                //显示图片计数+1
                Upload.showCount++;
                <span style="color:#ff6666;">var $img = $("<img src='" + editor.options.imagePath + url + "' class='edui-image-pic' />"),</span>
                    $item = $("<div class='edui-image-item edui-image-upload-item'><div class='edui-image-close'></div></div>").append($img);

                if ($(".edui-image-upload2", $w).length < 1) {
                    $(".edui-image-content", $w).append($item);

                    Upload.render(".edui-image-content", 2)
                        .config(".edui-image-upload2");
                } else {
                    $(".edui-image-upload2", $w).before($item).show();
                }

                $img.on("load", function () {
                    Base.scale(this, 120);
                    Base.close($(this));
                    $(".edui-image-content", $w).focus();
                });

            } else {
                currentDialog.showTip( state );
                window.setTimeout( function () {

                    currentDialog.hideTip();

                }, 3000 );
            }

            Upload.toggleMask();

        }
修改为

callback: function (editor, $w, url, state) {

            if (state == "SUCCESS") {
                //显示图片计数+1
                Upload.showCount++;
                <span style="color:#ff6666;">var $img = $("<img src='" + url + "' class='edui-image-pic' />"),</span>
                    $item = $("<div class='edui-image-item edui-image-upload-item'><div class='edui-image-close'></div></div>").append($img);

                if ($(".edui-image-upload2", $w).length < 1) {
                    $(".edui-image-content", $w).append($item);

                    Upload.render(".edui-image-content", 2)
                        .config(".edui-image-upload2");
                } else {
                    $(".edui-image-upload2", $w).before($item).show();
                }

                $img.on("load", function () {
                    Base.scale(this, 120);
                    Base.close($(this));
                    $(".edui-image-content", $w).focus();
                });

            } else {
                currentDialog.showTip( state );
                window.setTimeout( function () {

                    currentDialog.hideTip();

                }, 3000 );
            }

            Upload.toggleMask();

        }
标红处为修改点

6、修改Uploader.java

由原有的

public void upload() throws Exception {
		boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
		if (!isMultipart) {
			this.state = this.errorInfo.get("NOFILE");
			return;
		}
		DiskFileItemFactory dff = new DiskFileItemFactory();
		String savePath = this.getFolder(this.savePath);
		dff.setRepository(new File(savePath));
		try {
			ServletFileUpload sfu = new ServletFileUpload(dff);
			sfu.setSizeMax(this.maxSize * 1024);
			sfu.setHeaderEncoding("utf-8");
			FileItemIterator fii = sfu.getItemIterator(this.request);
			while (fii.hasNext()) {
				FileItemStream fis = fii.next();
				if (!fis.isFormField()) {
					this.originalName = fis.getName().substring(fis.getName().lastIndexOf(System.getProperty("file.separator")) + 1);
					if (!this.checkFileType(this.originalName)) {
						this.state = this.errorInfo.get("TYPE");
						continue;
					}
					this.fileName = this.getName(this.originalName);
					this.type = this.getFileExt(this.fileName);
					this.url = savePath + "/" + this.fileName;
					BufferedInputStream in = new BufferedInputStream(fis.openStream());
					File file = new File(this.getPhysicalPath(this.url));
					FileOutputStream out = new FileOutputStream( file );
					BufferedOutputStream output = new BufferedOutputStream(out);
					Streams.copy(in, output, true);
					this.state=this.errorInfo.get("SUCCESS");
					this.size = file.length();
					//UE中只会处理单张上传,完成后即退出
					break;
				} else {
					String fname = fis.getFieldName();
					//只处理title,其余表单请自行处理
					if(!fname.equals("pictitle")){
						continue;
					}
                    BufferedInputStream in = new BufferedInputStream(fis.openStream());
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuffer result = new StringBuffer();  
                    while (reader.ready()) {  
                        result.append((char)reader.read());  
                    }
                    this.title = new String(result.toString().getBytes(),"utf-8");
                    reader.close();  
                    
				}
			}
		} catch (SizeLimitExceededException e) {
			this.state = this.errorInfo.get("SIZE");
		} catch (InvalidContentTypeException e) {
			this.state = this.errorInfo.get("ENTYPE");
		} catch (FileUploadException e) {
			this.state = this.errorInfo.get("REQUEST");
		} catch (Exception e) {
			this.state = this.errorInfo.get("UNKNOWN");
		}
	}
修改为

public void upload() throws Exception
	{
		boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
		if (!isMultipart)
		{
			this.state = this.errorInfo.get("NOFILE");
			return;
		}
		InputStream is = null;// 附件输入流
		try
		{
			<span style="color:#ff6666;">String path = PropertiesUtil.getProperty("picture_path_ueditor") + PropertiesUtil.getProperty("healthPic");
			String realPath = request.getSession().getServletContext().getRealPath("/") + PropertiesUtil.getProperty("picture_path") + PropertiesUtil.getProperty("healthPic");</span>
		    
		    File file = new File(realPath + File.separator);
	        // 判断文件夹是否存在,如果不存在则创建文件夹
	        if (!file.exists()) {
	            file.mkdirs();
	        }
			MultipartResolver resolver = new CommonsMultipartResolver(
				this.request.getSession().getServletContext());
			MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
			CommonsMultipartFile orginalFile = (CommonsMultipartFile) multipartRequest.getFile("upfile");
			this.originalName = orginalFile.getOriginalFilename();
			if (!this.checkFileType(this.originalName))
			{
				this.state = this.errorInfo.get("TYPE");
				return;
			}
			this.type = this.getFileExt(this.originalName);
			this.size = orginalFile.getSize();
			
			String timestamp = System.currentTimeMillis() + "";
			String newIdPicFileName = this.originalName.substring(0, this.originalName.lastIndexOf(".")) + timestamp + this.type;
            String fullName = realPath + File.separator + newIdPicFileName;
            DataOutputStream out = new DataOutputStream(new FileOutputStream(fullName));// 存放文件的绝对路径
            
            is = orginalFile.getInputStream();
            byte[] b=new byte[is.available()];
            is.read(b);
            out.write(b);
            
            if (is != null) {
				is.close();
			}
			if (out != null) {
				out.close();
			}
			this.url = path+newIdPicFileName;
			this.state = this.errorInfo.get("SUCCESS");
		}
		catch (Exception e)
		{
			this.state = this.errorInfo.get("UNKNOWN");
		}
	}

其中标红的需要注意一下,path是图片读取的路径,realPath是图片文件需要上传到的物理路径

7、至此已经完成


参照:http://www.w2bc.com/Article/86325



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值