项目补充了一下,需要从服务器上下载文件(效果如下图),来maker一下。
1.后台JAVA代码
private InputStream downloadFilePath;
private String downloadFileName;
/**
*TODO(根据路径下载pdf)
*@param path
**/
public String
getPdfByUrl(){
String saveRealFilePath = ServletActionContext.getServletContext().getRealPath(
"/doc");
saveRealFilePath=saveRealFilePath+downloadFileName;
System.out.println(saveRealFilePath);
downloadFileName=downloadFileName.replace(
"/",
"");
try {
downloadFileName=URLEncoder.encode(downloadFileName,
"UTF-8");
downloadFilePath=
new FileInputStream(saveRealFilePath);
}
catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
2.Struts配置
<action name="plan_download" class="com.gsafety.plan.module.actions.PreplanAction" method="getPdfByUrl">
<result name="success" type="stream">
<param name="contenType">application/pdf
</param>
<param name="inputName">downloadFilePath
</param>
<param name="contentDisposition">attachment;filename="${downloadFileName}"
</param>
<param name="bufferSize">8192
</param>
</result>
</action>
3.前台请求(仅供参考)
function getPdfByUrl(url){
var form = $(
'<form></form>');
form.attr(
'action',
"${pageContext.request.contextPath}/plan/preplan/plan_download.action");
form.attr(
'method',
'post');
var set_input = $(
'<input type="text" name="downloadFileName" />');
set_input.attr(
'value', url);
form.append(set_input);
$(document.body).append(form);
form.submit();
return false;
}