掲示板

Download File not working in ACTION CLASS

thumbnail
6年前 に Praveen Mistry によって更新されました。

Download File not working in ACTION CLASS

Junior Member 投稿: 27 参加年月日: 17/03/23 最新の投稿
Hi All,

I`m not able to download File, in
Action Class
but it works for
serveResource
Method.
I think the issue in HttpServletResponse.


HttpServletResponse in serveResource Method:
HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest);
HttpServletResponse response = PortalUtil.getHttpServletResponse(resourceResponse);

HttpServletResponse in Action Class:
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);


The code is below:

HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
FTPClient ftpClient = new FTPClient();
OutputStream outStream = null;
InputStream inputStream = null;
BufferedInputStream in = null;
try {
ftpClient.connect("ftpURL");
ftpClient.login("username", "password");
String remoteFile2 = "/Liferay/sample.html";
inputStream = ftpClient.retrieveFileStream(remoteFile2);
in = new BufferedInputStream(inputStream);

response.setContentType("application/octet-stream");

// set headers for the response
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", "sample.html");
response.setHeader(headerKey, headerValue);
//response.setHeader("Content-Length",String.valueOf(object.getObjectMetadata().getContentLength()));
response.flushBuffer();
outStream = response.getOutputStream();

int numRead;

byte[] buffer = new byte[4096];
int bytesRead = -1;

while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}

outStream.flush();
outStream.close();
inputStream.close();

boolean success = ftpClient.completePendingCommand();
if (success) {
System.out.println("File #2 has been downloaded successfully.");
}

} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {

try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

Thank You!
thumbnail
6年前 に Olaf Kock によって更新されました。

RE: Download File not working in ACTION CLASS (回答)

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Praveen Mistry:
I`m not able to download File, in
Action Class
but it works for
serveResource
Method.
I think the issue in HttpServletResponse.


Wrong: The issue is that, by design, you can't do this from portlet action handlers. Action Handlers do not output anything - they're designed to change a portlet's internal state. Grabbing the HttpServletResponse from there is a hack, not guaranteed to work at all and wreaks havoc all over (Liferay would try to render the page right after you've streamed your downloadable file - something that would just go wrong).

Just be happy that you've already found the correct solution: ServeResource is the way to go.
thumbnail
6年前 に Praveen Mistry によって更新されました。

RE: Download File not working in ACTION CLASS

Junior Member 投稿: 27 参加年月日: 17/03/23 最新の投稿
Thank For Reply Olaf Kock emoticon

So, How can I get AUI:FORM data on ServeResource, by AJAX or any other way?
thumbnail
6年前 に Olaf Kock によって更新されました。

RE: Download File not working in ACTION CLASS (回答)

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Praveen Mistry:
So, How can I get AUI:FORM data on ServeResource, by AJAX or any other way?


Not sure if I understand your question - my answer to this would be:
resourceRequest.getParameter("parameterName")