[color=red] 问题描述如下:[/color]
1. Action的配置如下:
<action name="download" class="download">
<interceptor-ref name="defaultStack" />
[color=blue]<interceptor-ref name="downloadInter" />[/color]
[color=blue]<param name="inputPath">/WEB-INF/uploadFiles/article.pdf</param>[/color]
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">
attachment;filename="yourArticle.pdf"
</param>
<param name="bufferSize">2048</param>
</result>
</action>
2. Action部分代码如下:
public class FileDownloadAction extends ActionSupport {
private String inputPath;
//inputPath的getter与setter略
3. 我想在[color=blue]downloadInter[/color]拦截器中设置Action的i[color=blue]nputPath[/color]参数,以便将从数据库中读取的 pdf文件,提供给读者下载。
4. downloadInter代码如下:
[size=x-large] public class FileDownloadInterceptor extends AbstractInterceptor {
private SpringJdbcHelper springJdbcHelper;
private LobHandler lobHandler;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("FileDownloadInterceptor---intercept...");
invocation.addPreResultListener(new PreResultListener() {
@SuppressWarnings("unchecked")
public void beforeResult(ActionInvocation invocation,
String resultCode) {
System.out.println("FileDownloadInterceptor---beforeResult...");
HttpSession session = ActionHelper.getHttpSession();
final String year = (String) session.getAttribute("year");
final String month = (String) session.getAttribute("month");
final String articleNo = (String) session
.getAttribute("articleNo");
@SuppressWarnings("unused")
Map<String, ResultConfig> resultMap = invocation.getProxy()
.getConfig().getResults();
ActionConfig config = invocation.getProxy().getConfig();
ResultConfig finalResultConfig = resultMap.get(resultCode);
final String sqlStr = " SELECT pubchpapername,pubfile FROM periodical_show_article "
+ " WHERE pubyear = ? AND pubmonth = ? AND pubpaperno = ? ";
@SuppressWarnings("unused")
ArrayList<ShowArticle> pdfsList = (ArrayList<ShowArticle>) getSpringJdbcHelper()
.getJdbcTemplate().query(sqlStr,
new Object[] { year, month, articleNo },
new RowMapper() {
public Object mapRow(ResultSet rs,
int rowNum) throws SQLException {
String name = rs.getString(1);
byte[] pubpdf = getLobHandler()
.getBlobAsBytes(rs, 2);
final ShowArticle showArticle = new ShowArticle();
showArticle.setPubChPaperName(name);
showArticle.setPubFile(pubpdf);
return showArticle;
}
});
String dir = null;
File file = null;
String realDir = null;
String newFile = year + month + articleNo + "_"
+ new Date().getTime() + ".pdf";
BufferedOutputStream bos = null;
FileOutputStream fos;
try {
dir = new File(".").getCanonicalPath();
System.out.println("dir = " + dir);
file = new File(dir, newFile);
realDir = file.getCanonicalPath();
System.out.println("realDir = " + realDir);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(pdfsList.get(0).getPubFile());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
[color=blue]// 我想在此设置inputPath参数[/color]
[color=blue]config.addParam("inputPath", realDir);[/color]
finalResultConfig.addParam("contentType", "application/pdf");
finalResultConfig.addParam("contentDisposition", "attachment");
System.out.println("FileDownloadInterceptor---addParam...");
}
});
return invocation.invoke();
}
public SpringJdbcHelper getSpringJdbcHelper() {
return springJdbcHelper;
}
public void setSpringJdbcHelper(SpringJdbcHelper springJdbcHelper) {
this.springJdbcHelper = springJdbcHelper;
}
public LobHandler getLobHandler() {
return lobHandler;
}
public void setLobHandler(LobHandler lobHandler) {
this.lobHandler = lobHandler;
}
}
[/size]
5. [color=red]如果有更好的方案,希望不吝赐教,谢谢![/color]