struts1 +java 下载pdf

 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)
}

你可能感兴趣的:(struts1)