下载ueditor富文本编辑器中的视频,文档。ssm

该文章解决在使用ueditor上传文件时,上传的文件会保存到webapps下,直接使用http://192.168.0.27:8080/xx/media/video/yy.doc访问文件下载时会报404的错误。

思路是改变文件上传时的路径,应为webapps下的文件不能直接访问。

1.config.json 增加两个配置项,用于控制是否启用虚拟路径配置 和 虚拟路径映射的实际物理目录,修改加粗。(以文件上传为例)

/* 上传文件配置 */
    "fileActionName": "uploadfile", /* controller里,执行上传视频的action名称 */
    "fileFieldName": "upfile", /* 提交的文件表单名称 */
    "fileUsingVirtualPath": "yes",
    "fileRealMappingPath": "F:/dest/upload",
    "filePathFormat": "/media/video/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
    "fileUrlPrefix": "http://192.168.0.27:8080/download", /* 文件访问路径前缀 */
    "fileMaxSize": 51200000, /* 上传大小限制,单位B,默认50MB */
    "fileAllowFiles": [
        ".png", ".jpg", ".jpeg", ".gif", ".bmp",
        ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
        ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
        ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
        ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
    ], /* 上传文件格式显示 */

2.修改配置管理类 com.baidu.ueditor.ConfigManager.java 的getConfig方法

public Map<String, Object> getConfig ( int type ) {
		
		Map<String, Object> conf = new HashMap<String, Object>();
		String savePath = null;
		boolean virtualPath = false;
		switch ( type ) {
		
			case ActionMap.UPLOAD_FILE:
				conf.put( "isBase64", "false" );
				conf.put( "maxSize", this.jsonConfig.getLong( "fileMaxSize" ) );
				conf.put( "allowFiles", this.getArray( "fileAllowFiles" ) );
				conf.put( "fieldName", this.jsonConfig.getString( "fileFieldName" ) );
		//for virtual path mapping   
                String filePathFormat = this.jsonConfig.getString("filePathFormat");  
                String fileUsingVirtualPath = this.jsonConfig.getString("fileUsingVirtualPath");  
                if("yes".equalsIgnoreCase(fileUsingVirtualPath)){  
                    String fileRealMappingPath = this.jsonConfig.getString("fileRealMappingPath");  
                    savePath = fileRealMappingPath + filePathFormat;  
                    virtualPath = true;  
                    conf.put( "realMappingPath", fileRealMappingPath);
                }
				//savePath = this.jsonConfig.getString( "filePathFormat" );
				break;
				
			case ActionMap.UPLOAD_IMAGE:
				conf.put( "isBase64", "false" );
				conf.put( "maxSize", this.jsonConfig.getLong( "imageMaxSize" ) );
				conf.put( "allowFiles", this.getArray( "imageAllowFiles" ) );
				conf.put( "fieldName", this.jsonConfig.getString( "imageFieldName" ) );
				savePath = this.jsonConfig.getString( "imagePathFormat" );
				break;
				
			//其他略.... 
				
			case ActionMap.LIST_FILE:
				conf.put( "allowFiles", this.getArray( "fileManagerAllowFiles" ) );
				conf.put( "dir", this.jsonConfig.getString( "fileManagerListPath" ) );
				conf.put( "count", this.jsonConfig.getInt( "fileManagerListSize" ) );
				break;
				
		}
		
		conf.put( "savePath", savePath );
		conf.put( "rootPath", this.rootPath );
		conf.put( "virtualPath", virtualPath );//add put不要弄掉了
		return conf;
		
	}

3.最后要修改上传类com.baidu.ueditor.upload.BinaryUploader save方法.(以下代码可以直接粘贴到项目中

public static final State save(HttpServletRequest request,
			Map<String, Object> conf) {
		FileItemStream fileStream = null;
		boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;

		if (!ServletFileUpload.isMultipartContent(request)) {
			return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
		}

		ServletFileUpload upload = new ServletFileUpload(
				new DiskFileItemFactory());

        if ( isAjaxUpload ) {
            upload.setHeaderEncoding( "UTF-8" );
        }

		try {
			FileItemIterator iterator = upload.getItemIterator(request);

			while (iterator.hasNext()) {
				fileStream = iterator.next();

				if (!fileStream.isFormField())
					break;
				fileStream = null;
			}

			if (fileStream == null) {
				return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
			}

			String savePath = (String) conf.get("savePath");
			String originFileName = fileStream.getName();
			String suffix = FileType.getSuffixByFilename(originFileName);

			originFileName = originFileName.substring(0,
					originFileName.length() - suffix.length());
			savePath = savePath + suffix;

			long maxSize = ((Long) conf.get("maxSize")).longValue();

			if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
				return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
			}

			savePath = PathFormat.parse(savePath, originFileName);
	    boolean virtualPath = (boolean)conf.get("virtualPath");
			String physicalPath = (String) conf.get("rootPath") + savePath;
	    //启用虚拟路径时,不再使用 rootPath  
            if(virtualPath)  
            {  
                //此时savePath已含有实际的映射物理路径  
                physicalPath = savePath;//no rootPath  
            }
			InputStream is = fileStream.openStream();
			State storageState = StorageManager.saveFileByInputStream(is,
					physicalPath, maxSize);
			is.close();

			if (storageState.isSuccess()) {
				storageState.putInfo("url", PathFormat.format(savePath));
				if(virtualPath){  
                    String temp = (String) conf.get("realMappingPath");  
                    storageState.putInfo("url", PathFormat.format(savePath.substring(temp.length())));  
                }
				storageState.putInfo("type", suffix);
				storageState.putInfo("original", originFileName + suffix);
			}

			return storageState;
		} catch (FileUploadException e) {
			return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
		} catch (IOException e) {
		}
		return new BaseState(false, AppInfo.IO_ERROR);
	}

	private static boolean validType(String type, String[] allowTypes) {
		List<String> list = Arrays.asList(allowTypes);

		return list.contains(type);
	}

4.tomcat配置。在D:\tomcat7\conf\Catalina\localhost目录下创建一个xml文件:download.xml

(注意:此文件名要与访问前缀一致。"fileUrlPrefix": "http://192.168.0.27:8080/download", /* 文件访问路径前缀 */)

<?xml version="1.0" encoding="UTF-8"?>
<Context  reloadable="true" path="/" docBase="F://dest/upload" crossContext="true">
</Context>
5.修改 tomcat/conf/web.xml 的参数:
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>

上面默认的是 false,需要修改为 true。


6.http://192.168.0.177:8080/download/media/video/3658d1a3b2cb4a17a4907ba46854e9ca.mp4,如果不出现404证明上述配置正确,搞定。

7.前端访问地址。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页