liferay上传图片并展示,guest可见

1.jsp中的form的 enctype=”multipart/form-data”

2.后台接收参数

UploadPortletRequest uploadRequest = PortalUtil
				.getUploadPortletRequest(actionRequest);
		String siteName = ParamUtil.getString(uploadRequest, "siteName", "");

要是用UploadPortletRequest否则接收不到form中的文件以外的其他字段的值。

3.

File file = uploadRequest.getFile("logo");
		try {
			bytes = FileUtil.getBytes(file);

		} catch (IOException e2) {
			e2.printStackTrace();// TODO: handle exception
		}
		ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),
				actionRequest);
		serviceContext.setAddGuestPermissions(true);
		FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(theme
				.getUserId(), theme.getLayout().getGroupId(),
				DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT,
				file.getName(), MimeTypesUtil.getContentType(file),
				sourcefileName, "", "", bytes,
				serviceContext);

要记得在调用DLApplocalServiceUtil.addFileEntry之前要获取ServiceContxt 并且要setAddGuestPermissionws(true);否则未登录用户无法在前台页面中显示上传的图片。

4.在前台页面展示时使用<img src=””/ >

src的属性的获取方法为

public String getImageSrc(long fileEntryId){
		DLFileEntry fileEntry = null;
		try {
			fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(fileEntryId);
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (null != fileEntry) {
			return "/documents/"
					+ fileEntry.getRepositoryId()
					+ "/"
					+ fileEntry.getFolderId()
					+ "/"
					+ HttpUtil.encodeURL(
							HtmlUtil.unescape(fileEntry.getTitle()), true)
					+ "/" + fileEntry.getUuid();
		} else {
			return StringPool.BLANK;
		}
	}

把方法的返回值填入src属性中即可。