java下載文件java實現代碼

	/**
	 * 
	 * @Title: downloadExcelTemplate
	 * @Description:下載指定路徑文件
	 * @param response
	 * @param request
	 * @param templeteName
	 * @throws IOException
	 * 
	 */
	public void downloadExcelTemplate(HttpServletResponse response, String templeteName)
			throws IOException {
		// TODO Auto-generated method stub
		OutputStream outp = null;
		FileInputStream in = null;
		try {
			// 要下載的模板所在的絕對路徑
			response.reset();
			response.addHeader("Content-Disposition", "attachment; filename=" + templeteName);
			response.setContentType("application/octet-stream;charset=UTF-8");
			outp = response.getOutputStream();
			in = new FileInputStream(templeteName);
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = in.read(b)) > 0) {
				outp.write(b, 0, i);
			}
			outp.flush();
		} catch (Exception e) {
			System.out.println("Error!");
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
				in = null;
			}
			if (outp != null) {
				outp.close();
				outp = null;
			}
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章