package com.file.management.portlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class FileManagementPortlet extends MVCPortlet {
@Override
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
// TODO Auto-generated method stub
super.doView(renderRequest, renderResponse);
}
@Override
public void processAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
try {
UploadPortletRequest uploadPortletRequest = PortalUtil
.getUploadPortletRequest(actionRequest);
saveTheUploadAttachment(uploadPortletRequest, actionResponse);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
super.processAction(actionRequest, actionResponse);
}
// save or update the attachments
public void saveTheUploadAttachment(
UploadPortletRequest uploadPortletRequest,
ActionResponse actionResponse) throws PortalException,
SystemException, IOException {
// E:/liferay-portal-6.2.10.1-ee-ga1/data/document_library
String fileNames[] = uploadPortletRequest.getFileNames("fileName");
if (fileNames != null) {
File file[] = uploadPortletRequest.getFiles("fileName");
Date date = new Date();
for (int i = 0; i < fileNames.length; i++) {
String fileName = fileNames[i];
if (fileName != null && !"".equals(fileName)) {
String randomSourceFileName = this.getRandomSourceFileName(fileName);
File destination = null;
String filePath = PropsUtil
.get("dl.store.file.system.root.dir");
filePath = filePath
+ new SimpleDateFormat("/yyyy/MM/dd/").format(date);
filePath = filePath.concat(randomSourceFileName);
destination = new File(filePath);
FileUtil.copyFile(file[i], destination);
FileUtil.delete(file[i]);
//只需把filePath保存到数据库即可,其实上传附件就是把相关要上传的文件复制到服务器的相关目录,然后下载时再去访问它的路径
System.out.println("filePath=" + filePath);
}
}
}
}
//Get the randomSourceFileName
public String getRandomSourceFileName(String sourceFileName) {
return this.getRandomString(8)+"_"+sourceFileName;
}
//Generate the random string
private String getRandomString(int length) {
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
//此处获取前台传递过来的文件路径和文件名,读取它,返回给客户端
String filePath = ParamUtil.getString(resourceRequest, "filePath");
String fileName = ParamUtil.getString(resourceRequest, "fileName");
File file = new File(filePath);
HttpServletResponse response = PortalUtil
.getHttpServletResponse(resourceResponse);
response.setContentType(getContentType(fileName));
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024];
long k = 0;
ServletOutputStream sos = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(sos);
while (k < file.length()) {
int j = bis.read(b, 0, b.length);
k += j;
bos.write(b, 0, j);
}
bis.close();
bos.flush();
bos.close();
sos.close();
super.serveResource(resourceRequest, resourceResponse);
}
public String getContentType(String fileName) {
String postfix = "";
postfix = fileName.substring(fileName.lastIndexOf("."),
fileName.length());
postfix = postfix.toUpperCase();
String contentType = "application/xmsdownload";
if (postfix.equals(".XLS") || postfix.equals(".XLSX")
|| postfix.equals(".XLT") || postfix.equals(".XLW")
|| postfix.equals(".CSV")) {
contentType = "application/vnd.ms-excel";
} else if (postfix.equals(".DOC")) {
contentType = "application/msword";
} else if (postfix.equals(".RTF")) {
contentType = "application/rtf";
} else if (postfix.equals(".TEXT")) {
contentType = "text/plain";
} else if (postfix.equals(".XML") || postfix.equals(".TXT")) {
contentType = "";
} else if (postfix.equals(".BMP")) {
contentType = "image/bmp";
} else if (postfix.equals(".JPG") || postfix.equals(".JPEG")) {
contentType = "image/jpeg";
} else if (postfix.equals(".GIF")) {
contentType = "image/gif";
} else if (postfix.equals(".AVI")) {
contentType = "video/x-msvideo";
} else if (postfix.equals(".MP3")) {
contentType = "audio/mpeg";
} else if (postfix.equals(".MPA") || postfix.equals(".MPE")
|| postfix.equals(".MPEG") || postfix.equals(".MPG")) {
contentType = "video/mpeg";
} else if (postfix.equals(".PPT") || postfix.equals(".PPS")) {
contentType = "application/vnd.ms-powerpoint";
} else if (postfix.equals(".PDF")) {
contentType = "application/pdf";
} else if (postfix.equals(".ZIP") || postfix.equals(".RAR")) {
contentType = "application/zip";
}
return contentType;
}
}
Liferay 多文件上传与下载实例
需积分: 31 101 浏览量
2015-12-09
11:49:59
上传
评论
收藏 52KB ZIP 举报
boshi3401
- 粉丝: 0
- 资源: 2
最新资源
- NetOps-py通过sftp替换网络设备启动文件
- STM32单片机FPGA毕设电路原理论文报告任务驱动教学法在单片机课程教学中的应用
- STM32单片机FPGA毕设电路原理论文报告任务驱动法在单片机教学中的应用
- STM32单片机FPGA毕设电路原理论文报告人造金刚石压机智能化压力测控系统设计
- 以某列为依据匹配多项(Excel版)
- STM32单片机FPGA毕设电路原理论文报告人体短臂离心机实验台的显示控制系统
- STM32单片机FPGA毕设电路原理论文报告人工气候室监控系统的环境控制器研究
- STM32单片机FPGA毕设电路原理论文报告染整自动线张力控制系统的设计
- 数据挖掘与机器学习-实验
- 基于Linux系统Nginx的动态网站的LNMP环境源码包
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈