2013-12-09 32 views
0

我有一個portlet,並且使用Spring MVC我想下載一些動態創建的文件。我的意思是我有字符串從liferay portlet下載zip文件夾的xml文件

List<String> xmls 

,其中包含的XML文件中的內容的列表,我有我的看法有些鏈接

<portlet:resourceURL var="exportForms" id="exportForms" /> 

如果用戶點擊該

<a href="${exportForms}">download</a> 

然後,resourceMapping函數必須使xml文件形成字符串列表,並將它們打包成壓縮的zip文件。

@ResourceMapping("exportForms") 
public void exportForms(ResourceRequest request, ResourceResponse response) {...} 

而且我不知道我該怎麼做。我發現,如何下載一個,簡單的文件,但沒有解決方案,如何下載壓縮文件夾的文件,從字符串列表動態創建。

問候

回答

1

假設的Liferay 6.1

進口

import java.io.File; 
import com.liferay.portal.kernel.util.FileUtil; 
import com.liferay.portal.kernel.zip.ZipWriter; 
import com.liferay.portal.kernel.zip.ZipWriterFactoryUtil; 

和你的資源的方法應該是這個樣子

response.setProperty("Content-Disposition", "attachment; filename=myarchive.zip"); 
    response.setContentType("application/zip"); 

    final ZipWriter writer = ZipWriterFactoryUtil.getZipWriter(); 

    for (String filename : xmls) { 
     byte[] file = FileUtil.getBytes(new File("filename")); 
     writer.addEntry(filename, file); 
    } 

    byte[] archive = writer.finish(); 
    response.getPortletOutputStream().write(archive);