读书人

liferay5.2.3 附件下传

发布时间: 2013-03-21 10:08:17 作者: rapoo

liferay5.2.3 附件上传

1、首先要在liferay项目中的service.xml? 中找到需要关联附件的表对应的entity标签,,在</entity>之前加上

2、就是对jsp(或者html)中的表单加上一个属性enctype="multipart/form-data"

3.就是保存表单提交给服务器的处理了

首先就是要获得一个uploadRequest 对象

?

4.ContractLocalServiceImpl中的实现

try {

}

contractPersistence.update(contract);

5.附件的显示

"get.contract.annex.file" />

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);

??? ??? ??? }

??? ??? }

??? }

???

?

?

?

?

附注,因为在程序涉及到了比较多的逻辑处理,这里写是最主要的过程,具体的逻辑处理可以自己发挥!

读书人网 >开源软件

热点推荐