Spring MVC 跨服務下載文件

 用戶訪問頁面(服務A) 下載  通過接口 現在 服務B 的文件 並下載到本地

@RequestMapping(value = "/downLoad", method = RequestMethod.GET)
	public void downLoad(Long id, HttpServletResponse response,String plyNo) {
	    InputStream is = null;
	    response.addHeader("pragma","NO-cache");
		response.addHeader("Cache-Control","no-cache");
		response.addDateHeader("Expries",0);
		response.setContentType("application/pdf;charset=utf-8");
		String filename = plyNo+".pdf";//文件名稱
		try {filename = new String(filename.getBytes("UTF-8"), "ISO8859_1");}catch (Exception e) {e.printStackTrace();}
		response.addHeader("Content-Disposition","attachment;filename=" + filename);
		OutputStream out = null;
		try{
            //遠程服務器接口地址
			String toURL = SysConfigUtil.getProperties_1("HT_PLYPDF_DOWN");
            //請求接口 並獲取文件流
			URL url = new URL(toURL+"?policyNo="+plyNo);
			URLConnection conn = url.openConnection();
			is = conn.getInputStream();
			out = response.getOutputStream();
			int length = 0;
			byte buffer[] = new byte[1024];
			while((length = is.read(buffer)) != -1){
				out.write(buffer, 0, length);
			}
		} catch (Exception e) {
	
		} finally{
		//close....
		}
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章