response下载文件pdf

public void getIdentificationBarFile(String formdataid, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> searchParam = new HashMap<String, Object>();
		searchParam.put("form_data_id", formdataid);
		String pdfPath = null;
		try {
			pdfPath = taskinformationservice.getIdentificationBarFile(searchParam);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if (pdfPath != null) {

			File pdf = new File(pdfPath);
			response.setContentType("text/html;charset=" + "UTF-8");
			try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfPath));
					BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());) {
				//必写,流必须关闭,java7中关闭流可以写在try里,就不用手动finally里close了
				response.setContentType("application/pdf");// 不同类型的文件对应不同的MIME类型 // 重点突出
				response.setHeader("Content-disposition",
						"attachment; filename=" + new String("标识条.pdf".getBytes("UTF-8"), "ISO8859-1"));
				response.setHeader("Content-Length", String.valueOf(pdf.length()));
				//bis.available()返回缓冲区中要读取的剩余字节数的总和
				 byte[] buffer = new byte[bis.available()];
				 bis.read(buffer);
				 bos.write(buffer);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/*response.setContentType("text/html;charset=" + "UTF-8");
		try {
			Map<String, Object> searchParam = new HashMap<String, Object>();
			searchParam.put("form_data_id", formdataid);
			String pdfPath = taskinformationservice.getIdentificationBarFile(searchParam);
			if(pdfPath!=null){
				
				File pdf = new File(pdfPath);
				response.setContentType("application/x-msdownload;");// 不同类型的文件对应不同的MIME类型 // 重点突出
				response.setHeader("Content-disposition","attachment; filename=" + new String("标识条.pdf".getBytes("UTF-8"), "ISO8859-1"));
				response.setHeader("Content-Length", String.valueOf(pdf.length()));
				
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfPath));//这里不是文件pdf,而是文件地址
				BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
				byte[] buffer = new byte[2048];
				int bytesRead;//获得buffer的长度,没有值返回-1
				while (-1 != (bytesRead = bis.read(buffer, 0, buffer.length))) {
					bos.write(buffer, 0, bytesRead);
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//必写,流必须关闭,java7中关闭流可以写在try里,就不用手动finally里close了
			if (bis != null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (bos != null)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}*/
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值