Thijs Vonk 15 Years Ago Hi Ray,I'm wondering if the last bit of code works. [...]res.addProperty( HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");[...]Because PortletResponseImpl does not implement addProperty properly... public void addProperty(String key, String value) { if (key == null) { throw new IllegalArgumentException(); }}(this code is from branch 5.0.x)(see also LEP-5828) Please sign in to reply. Reply as... Cancel Brian Chan Thijs Vonk 15 Years Ago LEP-5828 is a duplicate and was fixed about 3 weeks ago I think. It'll be in the next 5.0.x release. Please sign in to reply. Reply as... Cancel Ray Augé Thijs Vonk 15 Years Ago Just committed a fix!See http://support.liferay.com/browse/LEP-5828 Please sign in to reply. Reply as... Cancel Vivek N Kapse Ray Augé 15 Years Ago I want to use this but couldn't figure out how to use it in my Struts Portlet. I am using Liferay 5.1.1. Should this be in action class or a jsp servlet or something else? Looking at the URL <portlet:resourceURL> couldn't understand how this would invoke the serveResource method. Can anyone give an elaborate explaination? Please. Please sign in to reply. Reply as... Cancel
Brian Chan Thijs Vonk 15 Years Ago LEP-5828 is a duplicate and was fixed about 3 weeks ago I think. It'll be in the next 5.0.x release. Please sign in to reply. Reply as... Cancel
Ray Augé Thijs Vonk 15 Years Ago Just committed a fix!See http://support.liferay.com/browse/LEP-5828 Please sign in to reply. Reply as... Cancel Vivek N Kapse Ray Augé 15 Years Ago I want to use this but couldn't figure out how to use it in my Struts Portlet. I am using Liferay 5.1.1. Should this be in action class or a jsp servlet or something else? Looking at the URL <portlet:resourceURL> couldn't understand how this would invoke the serveResource method. Can anyone give an elaborate explaination? Please. Please sign in to reply. Reply as... Cancel
Vivek N Kapse Ray Augé 15 Years Ago I want to use this but couldn't figure out how to use it in my Struts Portlet. I am using Liferay 5.1.1. Should this be in action class or a jsp servlet or something else? Looking at the URL <portlet:resourceURL> couldn't understand how this would invoke the serveResource method. Can anyone give an elaborate explaination? Please. Please sign in to reply. Reply as... Cancel
Denis Signoretto 14 Years Ago Can you describe how can be used/overrided the method:public void serveResource(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception { ... }of MyStrutsPortletAction, write jsp tags and use configure Struts to download a file?Thanks,D. Please sign in to reply. Reply as... Cancel
Kuo Hsiung Chong 14 Years Ago i try to set the content-disposition to reflect the filename but it failed.String contentDisposition = "attachment; filename=" + filename;resourceResponse.setProperty(HttpHeaders.CONTENT_DISPOSITION, contentDisposition);Did anyone encounters this problem before? Any solution for it.(I try both setProperty and addProperty but still fail to display the correct filename) Please sign in to reply. Reply as... Cancel
Jose Antonio 14 Years Ago Hi,I have a problem, i don't know how configure the files struts-config.xml and tiles-def.xml in Liferay 5.2.3.I'm using my portlet in EXT enveroiment.Thanks.Jose Antonio. Please sign in to reply. Reply as... Cancel
stefano flores 14 Years Ago How I solvethis problem for JSF-portlet???http://stackoverflow.com/questions/2361765/problem-to-display-a-pdf-from-my-jsf-portlet-of-liferayHEEELLLLPPPPPP!!!! Please sign in to reply. Reply as... Cancel Ray Augé stefano flores 14 Years Ago See http://stackoverflow.com/questions/2361765/problem-to-display-a-pdf-from-my-jsf-portlet-of-liferay/2363832#2363832 Please sign in to reply. Reply as... Cancel
Ray Augé stefano flores 14 Years Ago See http://stackoverflow.com/questions/2361765/problem-to-display-a-pdf-from-my-jsf-portlet-of-liferay/2363832#2363832 Please sign in to reply. Reply as... Cancel
alex wom 13 Years Ago I'm using the example and it work for txt file and not for zip files. My code is in the following, and with zip files I cannot open that after download because the files seem corrupted... String xmlString = FileUtil.read(fileDirectory+fileName); HttpServletResponse response = PortalUtil.getHttpServletResponse(res); in = new ByteArrayInputStream(xmlString.getBytes()); String contentType = MimeTypesUtil.getContentType(fileDirectory+fileName); String extension = FileUtil.getExtension(fileName); if (extension.equalsIgnoreCase("zip")) contentType="application/zip"; res.setContentType(contentType); res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate"); res.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+fileName); res.addProperty(HttpHeaders.CONTENT_ENCODING, "Binary"); OutputStream out = res.getPortletOutputStream(); in = new ByteArrayInputStream(xmlString.getBytes()); if (in == null) { out.close(); } else { byte[] buffer = new byte[4096]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.flush(); in.close(); out.close(); } } Please sign in to reply. Reply as... Cancel Ray Augé alex wom 13 Years Ago You're reading a potentially binary file into a string so yeah. it's gonna be corrupted.Stream the file right into the res, like so:InputStream in = new FileInputStream(new File(fileDirectory+fileName));The rest looks fine! Please sign in to reply. Reply as... Cancel
Ray Augé alex wom 13 Years Ago You're reading a potentially binary file into a string so yeah. it's gonna be corrupted.Stream the file right into the res, like so:InputStream in = new FileInputStream(new File(fileDirectory+fileName));The rest looks fine! Please sign in to reply. Reply as... Cancel
Jiri Vanek 13 Years Ago I'm not able to find dependencies for import javax.portlet.ResourceResponse;import javax.portlet.ResourceRequest;in my portal 2.0, maven, liferay 5.2.3 application Please sign in to reply. Reply as... Cancel Jiri Vanek Jiri Vanek 13 Years Ago and when I use instead of <dependency> <groupId>portlet-api</groupId> <artifactId>portlet-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency>the <dependency> <groupId>javax.portlet</groupId> <artifactId>portlet-api</artifactId> <version>2.0</version> </dependency>Liferay crashes. Please sign in to reply. Reply as... Cancel
Jiri Vanek Jiri Vanek 13 Years Ago and when I use instead of <dependency> <groupId>portlet-api</groupId> <artifactId>portlet-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency>the <dependency> <groupId>javax.portlet</groupId> <artifactId>portlet-api</artifactId> <version>2.0</version> </dependency>Liferay crashes. Please sign in to reply. Reply as... Cancel
Madhura Raut 13 Years Ago I am trying the below code to download PDF, XLS and CSV file from portlet. I am able to download CSV file currently but whenever I download PDF or XLS file I get corruptesd data. The reason for this is that I am trying to download the file in render phase which supports only text/html files. To achieve downloading for PDF and XLS we need to get it done in response phase but I dont know how to achieve this.For that I implemented the serveResource(ResourceRequest req, ResourceResponse res) method and read call the req.getResourceId() method of the ResourceRequest and invoked my business logic (create PDF) and subsequently write it to the output stream.But still I am stuck with the same problem.Currently I am using JSF 2 on portlet. Can anyone help me with the solution? public void DownloadPDF() throws DocumentException, FileNotFoundException, Exception { PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); HttpServletResponse response= com.liferay.portal.util.PortalUtil.getHttpServletResponse(portletResponse); //We must get first our context FacesContext context = FacesContext.getCurrentInstance(); //Then we have to get the Response where to write our file //Now we create some variables we will use for writting the file to the response String filePath = null; int read = 0; byte[] bytes = new byte[1024]; String pathToTheFile="D:/NetbeansProject"; //Be sure to retrieve the absolute path to the file with the required method filePath = pathToTheFile; //Now set the content type for our response, be sure to use the best suitable content type depending on your file //the content type presented here is ok for, lets say, text files and others (like CSVs, PDFs) response.setContentType("application/pdf"); //This is another important attribute for the header of the response //Here fileName, is a String with the name that you will suggest as a name to save as //I use the same name as it is stored in the file system of the server. String fileName="tablePDF.pdf"; response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); //Streams we will use to read, write the file bytes to our response FileInputStream fis = null; OutputStream os = null; //First we load the file in our InputStream try { fis = new FileInputStream(new File(filePath,fileName)); os = response.getOutputStream(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //While there are still bytes in the file, read them and write them to our OutputStream try { while((read = fis.read(bytes)) != -1){ os.write(bytes,0,read); } os.flush(); os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Clean resources //This option isn't quite necessary, It worked for me with or without it FacesContext.getCurrentInstance().responseComplete(); //End of the method } Please sign in to reply. Reply as... Cancel
Ravi Bhaj 11 Years Ago Hi All, How to download a file from portlet using a standalone java class? I was trying this by creating a simple portlet with serveResourse() method. The serveResource() would fetch the file. A standalone java class to hit this resourseURL to download a file. But I am not sure how to hit the resourseURL from a standalone java class.Please let me know if my approach is correct or suggest if there is any better approach.Thanks,Ravi Please sign in to reply. Reply as... Cancel
Praveen Mistry 6 Years Ago Hi RAY AUGÉ,I`m not able to download File, in "Action Class" but it works for "serveResource" Method. Issue is to create "HttpServletResponse" object. I have posted my issue on following thread: https://web.liferay.com/community/forums/-/message_boards/message/93422404 Please sign in to reply. Reply as... Cancel