how to download pdf file from external site via actionrequest

Scarletake Bwi, modified 3 Years ago. Expert Posts: 327 Join Date: 12/20/10 Recent Posts

hi 

i'd like to let user read pdf when they click icon

<liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>"  url="http://external/?doc=xxx />

it works.

but i'd like to know, how to do it via actionrequest. i changed my code

<portlet:actionURL var="efgpExportDocFunctionURL" name="exportDoc">
	<portlet:param name="<%=Constant.EFGP_EXPORT_DOC_NO%>" value="<%=docInfoVO.getDocNo()%>" />
</portlet:actionURL>

<liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>"  url="<%=efgpExportDocFunctionURL.toString() %>" />
	public void exportDoc(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {
		StringBuffer messageSB = new StringBuffer();
		User currentUser = (User) actionRequest.getAttribute(WebKeys.USER);
		String docNo = ParamUtil.getString(actionRequest, Constant.EFGP_EXPORT_DOC_NO);
		try {
			StringBuffer docLink = new StringBuffer();
			docLink.append("http://").append(Constant.EFGP_DOC_SERVER).append("/?docNumber=").append(docNo);
			LOGGER.info(docLink.toString());
			HttpClient httpClient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(docLink.toString());
			HttpResponse responset = httpClient.execute(httpGet);
			HttpEntity entity = responset.getEntity();
			HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
			response.setContentType(Constant.HTTP_HEADERS_CONTENT_TYPE_APPLICATION_PDF);
			response.setHeader(HttpHeaders.CONTENT_DISPOSITION, Constant.HTTP_HEADERS_CONTENT_DISPOSITION_CONTENT_PREFIX_ATTACHMENT_FILENAME + StringPool.QUOTE + docNo + ".PDF" + StringPool.QUOTE);
			OutputStream out = response.getOutputStream();
			entity.writeTo(out);
			out.flush();
			out.close();
		} catch (Exception ex) {
			LOGGER.equals(ex);
			messageSB.append("hit exception:").append(ex.getClass().getName()).append(" message:").append(ex.getMessage()).append(Constant.HTML_SNAP);
			SessionErrors.add(actionRequest, ex.getClass().getName(), ex);
		}
	}

but it doesn't work, it seens get the file already, but in borwser, it shows


 

please let me know how to make it right or any suggestion, thank you very much in advance.

thumbnail
Dominik Marks, modified 3 Years ago. Regular Member Posts: 149 Join Date: 8/29/12 Recent Posts

At first you should have a look at ResourceRequests. Downloading of files is typically done with ResourceRequests and not with ActionRequests...

Scarletake Bwi, modified 3 Years ago. Expert Posts: 327 Join Date: 12/20/10 Recent Posts

hi Dominik

thank you for reply. 

yes, you are right.

but i want the pdf directly be opened on a new window, such like target='_blank". 

i do not know how to do it.

thumbnail
Mohammed Yasin, modified 3 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts

Hi, 

You can use below Utility for fetching the document url and use it in anchor tag with target="blank".

DLURLHelperUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), themeDisplay, StringPool.BLANK)

 

Scarletake Bwi, modified 3 Years ago. Expert Posts: 327 Join Date: 12/20/10 Recent Posts

hi Mohammed

thank you, thank you very much for reply. you really open my eyes.

i don't know this before and i tried. but unfortunately, it seems like download from external site to server first, and display to user.

i do not want to save it. the origional code is

<a href="http://xxx/?doc=<%=docNo%>" target="_blank">

so i make it as

<a href="http://xxx/doc?=<%=docNo%>" target="_blank"><liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>" /></a>

it works now.