`
a124116186
  • 浏览: 9372 次
  • 来自: 武汉
社区版块
存档分类
最新评论

struts1 +java 下载pdf

    博客分类:
  • java
 
阅读更多
 public ActionForward readPDF(ActionMapping mapping, ActionForm form, HttpServletRequest request,
	    HttpServletResponse response) throws Exception {
    	String newsFilePath = request.getParameter("newsFilePath");
    	String newsTitle = request.getParameter("newsTitle");
    	
		response.setContentType("application/pdf");
		response.setHeader("Content-disposition",  "attachment; filename=" + URLEncoder.encode(newsTitle, "gb2312"));	
		
		
		String pdfBasePath = constant.getPdfDirectory() + newsFilePath;
		OutputStream outputStream = response.getOutputStream();
		InputStream inputStream = new FileInputStream(pdfBasePath);
		try {
			    byte[] buffer = new byte[1024];
			    int i = -1;
			    while ((i = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, i);
		    }
		    outputStream.flush();
		} finally {
			    if (outputStream != null) {
				outputStream.close();
		    }
			    if (inputStream != null) {
				inputStream.close();
		    }
		}
		
		return null;
		
		
    }



function downNewsPdf(bean){
	var pdfPath = $(bean).attr("pdfPath");
	if(!pdfPath){
		alert("请先选择新闻");
	}
	var curNewName = $("#article_news_title").html();
	window.open('/articlePdf.do?method=readPDF&newsFilePath='+ pdfPath+"&newsTitle="+curNewName)
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics