Foros de discusión

Files download from custom portlet

thumbnail
Venkateswara Raju Vidyadharani, modificado hace 10 años.

Files download from custom portlet

Junior Member Mensajes: 30 Fecha de incorporación: 17/02/13 Mensajes recientes
Hi

am using Struts2 Porlets in Liferay. and i want download files from my custom folder

is there any code to download a file from struts2 portlet

i followed this code

1.http://www.mkyong.com/struts2/struts-2-download-file-example

2.http://hiralbarott.blogspot.in/2012/08/uploaddownload-file-on-file-system-in.html

but it depreciated in liferay 6.1 and not working in Struts2 Portlet,

Could please help me out.....
thumbnail
Prakash Khanchandani, modificado hace 10 años.

RE: Files download from custom portlet

Expert Mensajes: 329 Fecha de incorporación: 10/02/11 Mensajes recientes
Which code you followed? can you update your question with the code?
thumbnail
Venkateswara Raju Vidyadharani, modificado hace 10 años.

RE: Files download from custom portlet

Junior Member Mensajes: 30 Fecha de incorporación: 17/02/13 Mensajes recientes
in jsp

<portlet:resourceURL var="filedownloadURL" >
<portlet:param name="struts.portlet.action"
value="/view/fileDownload.action" />
</portlet:resourceURL>
<s:form>
<s:url action="fileDownload.action" var="download" />
<s:a href="%{download}" >Download</s:a> // struts2 tag url
<a href="<%=filedownloadURL.toString()%>" id="dwn">Download</a> //porltet resource url
</s:form>

in struts.xml
<action name="fileDownload" class="com.edvie.admin.action.FileDownloadAction">
<result name="success" type="stream">
<param name="contentType">text/html,text/plain,application/msword,application/pdf,application/octet-stream,
application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">250000</param>
</result>
</action>

in action class
FileDownloadAction.java
String filePath = getText("struts.multipart.saveDir"); // C:\\Documents and Settings\\Raju\\Desktop\\Edvie Upload Documents
String fileName="08052013.txt";
inputStream=new FileInputStream(new File(filePath+"\\"+fileName)); //setters and getters

file downloading with struts url tag but content changing with html code.
where as resource url giving exception.
could you please help me out required code..........
thumbnail
Venkateswara Raju Vidyadharani, modificado hace 10 años.

RE: Files download from custom portlet

Junior Member Mensajes: 30 Fecha de incorporación: 17/02/13 Mensajes recientes
i solved this issue. thank you for liferay forum and members
thumbnail
Priyanka Dhingra, modificado hace 10 años.

RE: Files download from custom portlet

Liferay Master Mensajes: 501 Fecha de incorporación: 20/12/11 Mensajes recientes
Please mention how you solved the issue, so as to help others who might land up to the same issue.
thumbnail
Venkateswara Raju Vidyadharani, modificado hace 10 años.

RE: Files download from custom portlet

Junior Member Mensajes: 30 Fecha de incorporación: 17/02/13 Mensajes recientes
Hi

Here is the code that i developed the uploading and downloading files.

For uploading with struts tags

I followed this link http://www.mkyong.com/struts2/struts-2-file-upload-example/

[b]For Filedownloading

in jsp page

<a href="#" id="download">Download File</a>

<script>

$("#download").click(function() {
var fileName="something.pdf" // any file
window.location.href = '<%=renderResponse.encodeURL(renderRequest.getContextPath())+"/DownloadFile?fileName="+fileName %>'; // calling servlet and passing file name as parameter

});

</script>

and configure ur servlet in web.xml

<servlet>
<servlet-name>DownloadFile</servlet-name>
<servlet-class>com.edvie.admin.action.DownloadFile</servlet-class> // your custom servlet
</servlet>
<servlet-mapping>
<servlet-name>DownloadFile</servlet-name>
<url-pattern>/DownloadFile</url-pattern>
</servlet-mapping>

Servlet Class

public class DownloadFile extends HttpServlet{

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

String fName=request.getParameter("fileName"); // getting file name from request parameter
System.out.println("fileName from request Object"+fName);

File file = new File("C:\\Documents and Settings\\Raju\\Desktop\\Edvie Upload Documents\\"+fName); // set ur file location

response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));

response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");


BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
input = new BufferedInputStream(new FileInputStream(file));
output = new BufferedOutputStream(response.getOutputStream());

byte[] buffer = new byte[8192];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
}catch(Exception e){
e.printStackTrace();
}
finally {
if (output != null) try { output.close(); } catch (IOException ignore) {}
if (input != null) try { input.close(); } catch (IOException ignore) {}
}
}

}


This works fine for me.......Thank you .......

if any doubt post ur doubt in this forum............
David Gitonga, modificado hace 10 años.

RE: Files download from custom portlet

Junior Member Mensajes: 63 Fecha de incorporación: 26/07/13 Mensajes recientes
Hi,
Have been trying to download file and my problem is My class is extending MVCPortlet so my method is accepting ActionRequest and ActionResponse

e.g.

public void downloadFile(ActionRequest request ,ActionResponse response){
}
How do download my file.
thumbnail
Priyanka Dhingra, modificado hace 10 años.

RE: Files download from custom portlet

Liferay Master Mensajes: 501 Fecha de incorporación: 20/12/11 Mensajes recientes
You can use
 UploadPortletRequest uploadrequest = PortalUtil.getUploadPortletRequest(actionRequest);
            InputStream[] inputStream = uploadrequest.getFilesAsStream("fileupload");
            for(InputStream fileObj:inputStream){
            File file = FileUtil.createTempFile(fileObj);
            } 
thumbnail
Olaf Kock, modificado hace 10 años.

RE: Files download from custom portlet

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
Have been trying to download file and my problem is My class is extending MVCPortlet so my method is accepting ActionRequest and ActionResponse


Try the same not in an action handler, but in a resource handler. The method looks similar:

public void downloadFile(ResourceRequest request, ResourceResponse response) {
  ...
}


To get the URL, you obviously can't use <portlet:actionURL/> but have to use <portlet:resourceURL/> instead. Everything else is similar. ResourceResponse is very similar to a HttpServletResponse - you get direct access to the output stream, can set content type and http headers etc.
Jörg Larbig, modificado hace 10 años.

RE: Files download from custom portlet

New Member Mensajes: 10 Fecha de incorporación: 9/08/12 Mensajes recientes
If it is an option to use vaadin instead of struts please have a look at

https://github.com/Union-Investment/vfs2-file-explorer

It is a portlet using apache vfs2 lib and it could be configured
to display / upload / delete files from file system, ftp and sftp

Regards
Jörg