
liferay的上传与下载.docx
9页上传一. 一种是可以用 document_library 的 api,但是这种方法过程会比较复杂,会涉及多个表的存储,而且文件会被改变名称存储在 tomcat/data/document目录下二. 第二种是在 liferay 官网论坛上看到的,相对来说比较简单,也有很多人在使用,好处是可以指定上传文件的目录,可以指定文件名具体实现方法如下:1. JSP端:function add(url) {$("#message").dialog({modal : true,buttons : {提交 : function() {// $.post(url,function(data){$("#upload").submit();}}});}var flag = "${flag}";if ("" != flag && null != flag) {if (flag != '0') {alert("添加成功");//window.location.reload();} else {alert("添加失败");}}这样做的效果是如下图所示:实现端:@RenderMapping(params = "action=upload")public ModelAndView upload(Model model, RenderRequest request)throws PortalException, SystemException, IOException {ModelAndView mav = new ModelAndView("/document_management/view");UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);boolean type = false;String sourceFileName = uploadPortletRequest.getFileName("photo");System.out.println("获取name" + sourceFileName);String contentType = uploadPortletRequest.getContentType("photo");System.out.println("文件名称是" + sourceFileName + "." + contentType);long size = uploadPortletRequest.getSize("photo");//获取文件大小User user = PortalUtil.getUser(request);long userid = user.getUserId();// 用户idlong groupId = user.getGroupId();// 仓储式idlong folderId = DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT;// 文件夹idInputStream is = uploadPortletRequest.getFileAsStream("photo");ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), request);//下面注释是用document_libraryAPI 进行上传// InputStream is = uploadPortletRequest.getFileAsStream("photo");// String result=zcglManager.upload(userid,groupId, folderId,// sourceFileName, contentType, "upload demo title21",// "upload demo description", "edit log", is, size, serviceContext);File tempFile = uploadPortletRequest.getFile("photo");File destination = null;// request.getPortletSession().getPortletContext().getRealPath("");String path = "D:\\liferayDownload\\";// dirPath = path.substring(0,// path.lastIndexOf(StringPool.BACK_SLASH)+1);String fileNm = uploadPortletRequest.getFileName("photo");Date date = new Date();path = path + new SimpleDateFormat("yyyy\\MM\\dd\\").format(date);if (Validator.isNotNull(fileNm)) {path = path.concat(fileNm);destination = new File(path);FileUtil.copyFile(tempFile, destination);FileUtil.delete(tempFile);}return mav;}下载JSP 端:downloadfunction download(){window.open("");}实现端:@ResourceMappingpublic void download(ResourceRequest request, ResourceResponse response)throws IOException {String url = ParamUtil.getString(request, "url");if (url == null || url.equals("") || url == "") {// url="D:\\liferayDownload\\2013\\11\\27EXP_PDF.DLL";ServletResponseUtil.write(PortalUtil.getHttpServletResponse(response), "文件不存在");}else{File file = new File(url);// downloadServletResponseUtil.sendFile(PortalUtil.getHttpServletRequest(request),PortalUtil.getHttpServletResponse(response), file.getName(),FileUtil.getBytes(file),"application/octet-stream");//"application/octet-stream" 表示直接下载//当然,如果需要直接显示或者其他方式操作,修改参数即可,默认是显示内容//使用浏览器自带的PDF阅读器打开( 参数设置为: "application/pdf"),或者其//他方式等等//参数定义文件: // 源文件 /portal-service/src/com.liferay.portal.kernel.util.ContentTypes//抑或本地文件 source.ContentType.java};。
