1. 程式人生 > >在Liferay中的下載--兩種方式

在Liferay中的下載--兩種方式

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ include file="/html/init.jsp"%>

<portlet:defineObjects />

<!-- 方式一 -->
<portlet:actionURL var="downLoadExcel" name="downLoadExcel"></portlet:actionURL>

<!-- 方式二 -->
<portlet:resourceURL var="download"></portlet:resourceURL>



<p>Liferay中檔案的下載方式:兩種</p>
<p>
	方式一:在portlet:actionURL中進行處理,不能使用ajax,否則彈不出下載的對話方塊,一般通過表單或者超連結實現,其次,需要在瀏覽器設定--“下載前詢問每個檔案的儲存位置”--開啟該選項<br>

	1.通過POI.jar包中的HSSFWorkbook寫入檔案到某個指定的資料夾下面(例如  tempFile資料夾),
	2.然後在將該檔案下載到客戶端,下載時彈出對話方塊,客戶可以自由選擇路徑
	3.刪除tempFile資料夾下生成的檔案
</p>
<p><a href="<%=downLoadExcel %>" style="text-decoration:none;"><input type="button" value="HSF下載按鈕" /></a></p>

<p>===========================</p>

<p>
	方式二:在portlet:resourceURL中進行處理,需要在瀏覽器設定--“下載前詢問每個檔案的儲存位置”--開啟該選項<br>

	將已經存在在檔案下載到客戶端,下載時彈出對話方塊,客戶可以自由選擇路徑
</p>
<p><a href="<%=download %>" style="text-decoration:none;"><input type="button" value="點選下載已有的檔案"/></a></p>

後臺程式碼如下:

package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class DownLoadPortlet
 * 下載測試
 */
public class DownLoadPortlet extends MVCPortlet {

	
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		System.out.println("將E盤中的xls檔案下載到其他位置,原檔案保留,可選下載地址"); 
		String path = "E:\\金石配置資訊.xls";
		//檔名
		String fileName = "金石配置資訊.xls";
		
		//下載
		File file = new File(path);  
        byte[] buff =new byte[(int) file.length()];   
  
        InputStream in = new FileInputStream(file);  
        in.read(buff);  
          
        HttpServletRequest req = PortalUtil.getHttpServletRequest(resourceRequest);  
        HttpServletResponse res = PortalUtil.getHttpServletResponse(resourceResponse); 
		
        ServletResponseUtil.sendFile(req, res, fileName,  
                buff, ContentTypes.APPLICATION_OCTET_STREAM);
		in.close();
        System.out.println("OKOK");
		
		
	}


	public void downLoadExcel(ActionRequest request,
			ActionResponse response) throws IOException, PortletException {
		System.out.println("=======進入=======");
		
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("測試excel");
		sheet.setDefaultColumnWidth(20);
		HSSFCellStyle style = wb.createCellStyle();
		HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setFont(font);
		
		//測試資訊
		sheet.createRow(0).createCell(0).setCellValue("11111");
		sheet.createRow(1).createCell(0).setCellValue("22222");
		sheet.createRow(2).createCell(0).setCellValue("8888");
		System.out.println("1111111111111111111");
		//1.儲存在臨時資料夾裡面
		//資料夾路徑(若沒有tempFile該資料夾,則新建一個)
		//String tempPath = PortalUtil.getHttpServletRequest(request).getSession().getServletContext().getRealPath("")+ File.separator + "tempFile" + File.separator;
		String tempPath = PortalUtil.getHttpServletRequest(request).getSession().getServletContext().getRealPath("")+ File.separator + "tempFile";
		File f = new File(tempPath);
		if(!f.exists()){
			f.mkdirs();
		}//建立tempFile成功
		
		System.out.println("22222222222222222222");

		//2.建立檔案
		String fileName = "測試3-18檔案.xls";	//檔名
		String path = tempPath + File.separator + fileName;	//      D:\liferay\tomcat\tomcat-7.0.42\webapps\TestDemo-portlet\tempFile\測試檔案.xls
		FileOutputStream out = new FileOutputStream(path);
		wb.write(out);
		out.close();
		System.out.println("儲存檔案完畢");
		
		System.out.println("333333333333333333333333");

		File file = new File(path);  
        byte[] buff =new byte[(int) file.length()];   
  
        InputStream in = new FileInputStream(file);  
        in.read(buff); 
        
        HttpServletResponse res = PortalUtil.getHttpServletResponse(response);  
        HttpServletRequest req = PortalUtil.getHttpServletRequest(request);  
		
        ServletResponseUtil.sendFile(req, res, fileName,  
                buff, ContentTypes.APPLICATION_OCTET_STREAM);
		in.close();
		
		System.out.println("444444444444444444444444");

		//3.刪除檔案
		if(file.exists()){
			file.delete();
		}
		
		System.out.println("555555555555555555555");

	}
 

}

下載框如下: