附件上传
首选说一下文件的存放位置,存放目录大致如下:
{bundles.home}/data/document_library/10115/0/customerDirName/0/fileName/1.0
这里主要是利用liferay现有的文档库,当然这些文件在文档库是看不到的!
下面说一下具体的实现:
1、首先要在liferay项目中的service.xml 中找到需要关联附件的表对应的entity标签,,在</entity>之前加上
<reference package-path="com.liferay.documentlibrary" entity="DL" />
大致表示的的是与文档库对应的接口相关联,只有这样才能在自己的实体类对应的service层的实现类中调用文档库提供的dlService对象,下面会看到的具体的使用的
例如:
<entity name="Contract" local-service="true" remote-service="false">
<column name="contractId" type="long" primary="true" />
<column name="companyId" type="long" />
<column name="name" type="String" />
<column name="haveAuthorizationBook" type="boolean" /><!-- 这里是用来标示是不是有附件的一个字段-->
<column name="authorizationBookName" type="String" />
<!-- 这里是有附件文件的名字的字段-->
<finder name="CompanyId" return-type="Collection">
<finder-column name="companyId" />
</finder>
<!-- References -->
<reference package-path="com.liferay.documentlibrary" entity="DL"></reference>
</entity>
2、就是对jsp(或者html)中的表单加上一个属性enctype="multipart/form-data"
(就是上传文件所需要的表单属性,你懂的)
<form action="" method="post" enctype="multipart/form-data">
这里有需要上传文件的选择框
<inputname="AuthorizationBook" size="70" type="file" />
</form>
3.就是保存表单提交给服务器的处理了
首先就是要获得一个uploadRequest 对象
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
之后传递过来的参数都需要通过uploadRequest 对象才能够获得哦!
如:String name = ParamUtil.getString(uploadRequest, "name");
下面就是获得要上传的文件了
File file = uploadRequest.getFile(fileParamName);
String fileName = uploadRequest.getFileName(fileParamName);
byte[] bytes = FileUtil.getBytes(file);
String dirName = TextUtil.getAnnexPath("contract_authorization_book", contract.getCreateUserId(), contract.getContractId());// 这个是用来获得附加要存放的在文档库中的路径的,可以自行定义,这里获得结果是使用字符串拼成的路径,规则是:三个参数相连接。这只是个例子,可以自由发挥的!!
contract.setAuthorizationBookName(fileName);
merge = contract.getHaveAuthorizationBook();
contract.setHaveAuthorizationBook(true);
ContractLocalServiceUtil.updateContractRealtionFile(contract, dirName, fileName,bytes, merge); //这个方法是在ContractLocalServiceImpl中实现的
4.ContractLocalServiceImpl中的实现
publicvoid updateContractRealtionFile(Contract contract,String dirName,String fileName,byte[] bytes,boolean hadDirectory) throws SystemException, PortalException{
long companyId=contract.getCompanyId();
//下面三个是liferay中的几个常量。
String portletId = CompanyConstants.SYSTEM_STRING;
long groupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
long repositoryId = CompanyConstants.SYSTEM;
if(hadDirectory){
try {
dlService.deleteDirectory(companyId, portletId, repositoryId, dirName);
}catch (NoSuchDirectoryException nsde) {
if (_log.isDebugEnabled()) {
_log.debug(nsde.getMessage());
}
}
}
dlService.addDirectory(companyId, repositoryId, dirName);
try {
dlService.addFile(companyId, portletId, groupId, repositoryId,dirName + File.separator + fileName, 0, StringPool.BLANK,
contract.getCreateDate(), new String[0], new String[0],bytes);
}catch (DuplicateFileException dfe) {
if (_log.isDebugEnabled()) {
_log.debug(dfe.getMessage());
}
}
contractPersistence.update(contract);
}
在这个方法中含有相应的逻辑处理,除了上面标红的部分要注意外,对于dlService方法的调用可以自由发挥的
5.附件的显示
<c:if test="<%=contract.getHaveAuthorizationBook() %>">
<li class="shortItem">
<span>
<liferay-ui:message key="authorization.book.file" />
</span>
<label>
<a href="<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
<portlet:param name="<%=ActionRequest.ACTION_NAME %>" value="get.contract.annex.file" />
<portlet:param name="contractId" value="<%= String.valueOf(contract.getContractId()) %>" />
<portlet:param name="flagDirectory" value="<%=Constants.CONTRACT_ANNEX_AUTHORIZATION_BOOK %>" />
</portlet:actionURL>">
<%=contract.getAuthorizationBookName() %>
</a></label>
</li>
</c:if>
6、下载
5中已经把下载的连接写出来了,这里就是后台的处理了,如下:
String fileName = contract.getAuthorizationBookName();
String filePath = TextUtil.getAnnexPath("contract_authorization_book
", createUserId, contractId)+separator+fileName;//这个与上面保存文件使用的是同一个方法获得路径
HttpServletResponse response=PortalUtil.getHttpServletResponse(portletResponse);
InputStream is = null;
try {
is = DLLocalServiceUtil.getFileAsStream(
companyId, CompanyConstants.SYSTEM, filePath);
int contentLength = (int)DLServiceUtil.getFileSize(
companyId, CompanyConstants.SYSTEM, filePath);
response.setHeader(
HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_PUBLIC_VALUE);
response.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_PUBLIC_VALUE);
String contentDisposition =
"attachment; filename=\"" + new String(fileName.getBytes("UTF-8"),"ISO-8859-1") + "\"";
response.setContentLength(contentLength);
response.setHeader(
HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
write(response, is, contentLength);
}
finally {
ServletResponseUtil.cleanUp(is);
}
下面是就是附带的几个下载的时候需要的方法
publicstaticvoid write(
HttpServletResponse response, InputStream is, int contentLength)
throws IOException {
OutputStream os = null;
try {
if (!response.isCommitted()) {
if (contentLength > 0) {
response.setContentLength(contentLength);
}
os = new BufferedOutputStream(response.getOutputStream());
int c = is.read();
while (c != -1) {
os.write(c);
c = is.read();
}
}
}
finally {
cleanUp(os, is);
}
}
publicstaticvoid cleanUp(OutputStream os, InputStream is) {
cleanUp(os);
cleanUp(is);
}
publicstaticvoid cleanUp(OutputStream os) {
try {
if (os != null) {
os.flush();
}
}
catch (Exception e) {
if (_log.isWarnEnabled()) {
_log.warn(e);
}
}
try {
if (os != null) {
os.close();
}
}
catch (Exception e) {
if (_log.isWarnEnabled()) {
_log.warn(e);
}
}
}
publicstaticvoid cleanUp(InputStream is) {
try {
if (is != null) {
is.close();
}
}
catch (Exception e) {
if (_log.isWarnEnabled()) {
_log.warn(e);
}
}
}
附注,因为在程序涉及到了比较多的逻辑处理,这里写是最主要的过程,具体的逻辑处理可以自己发挥!
相关推荐
Liferay5.2.3的样例,通过war.bat打包后发布到相应的liferay环境即可。 另外,需要配置context,tomcat中的代码如下: </Context>
liferay5.2.3 tomcat6 eclipse ide 开发文档
Liferay5.2.3和CAS配置成SSO
Liferay 5.2.3 开发环境环境搭建.doc eclipse 环境下。 ext 路径生成
Liferay 5.2.3 二次开发环境搭建 自己编写的开发环境搭建手册 基于 Myeclipse 6 Tomcat 6.0.24 搭建开发环境
liferay 5.2.3 可實際用於建構 jbpm workflow portlet的 jbpm檔。 用這個檔案加上mule再參考網路上的設定文件,即可輕易的在5.2.3上完成workflow portlet的建置... mule檔太大,請在liferay網路上下載即可
Liferay 是一个完整的门户解决方案,基于J2EE的应用,使用了EJB以及JMS等技术,前台界面部分使用Struts MVC 框架,基于XML的portlet配置文件可以自由地动态扩展,使用了Web服务来支持一些远程信息的获取,使用 ...
没有分的请到这里,免费的 http://www.chinasb.org/65.html
下载Liferay 5.2.3源码,安装Eclipse开发环境,导入源码,修改配置,搭建二次开发环境,是一个很好的入门教程。
Liferay_5.2.3_开发环境环境搭建
这个事liferay 来开发portlet的sdk,版本是5.2.3。
Liferay-Portlet-SDK 5.2.3操作说明.ppt
Support For Liferay 5.2.3 & Web space 10.0 U4 Support For both NetBeans 6.5.x & 6.7 Page Navigation Fix For Visual JSF Portlet
最近在研究liferay protal,苦于没有相关的资料.于是在网上买了本电子书.此书为pdf格式,500多页,2009年7月出版,基于liferay 5.2.3.虽然是英文版,但也易懂.希望对朋友们有帮助.
Liferay多文件上传与下载实例,前端包含多文件上传JS,可以动态追加文件,删除文件,还有文件类型与大小验证JS。后端可以批文件上传。 下载可以实现弹出页另存为。避免了Liferay自身下载PDF和图片时在当前页面打开的...
portlet下开发的基于liferay的上传下载功能,原创可用
Liferay Portal Liferay IDE
liferay-portal-sql-oracle-10-5.2.3.dmp
描述了liferay的数据库表结构以及关联关系 示例: 社区、组织、地区、用户组、用户与角色之间的关系 1、表Groups_Roles 主要功能:存储社区与角色的关系; 存储组织与角色的关系; 存储地区与角色的关系; 存储用户...
Liferay中的AUI框架,官方叫做Alloy,是一个UI超框架,能为浏览器的三方面,结构,风格和表现建立网络应用提供连续的简单的API。 它融合了三种设计语言:HTML, CSS 和 JAVA。 准确来说AUI是Liferay在YUI的基础上...