当前位置 博文首页 > 少年时未觉悟 ,觉悟时不再年少!:项目总结-HttpURLConnection

    少年时未觉悟 ,觉悟时不再年少!:项目总结-HttpURLConnection

    作者:[db:作者] 时间:2021-07-16 18:45

    ??????? 项目需要存储文件,但是由于使用百度云平台的服务器,没有文件写权限,辗转使用中间转发的策略,将文件存储在公司公网服务器上,通过云平台的文件请求都转发到文件服务器。响应转发过程中,总是得不到转发的响应内容。跟踪发现获取到的URLConnection的输入流的available总是返回0,导致转发响应内容为空。调试发现把 URLConnection的disconnect()调用代码注释掉,能够转发成功。

    /**
    	 * 以http方式发送请求,并将请求响应内容以String格式返回
    	 * @param path    请求路径
    	 * @param method  请求方法
    	 * @param body    请求数据
    	 * @return 返回响应的字符串
    	 */
    	public static void httpRequestRediret(HttpServletResponse response,String fileName,String path, String method, String body) {
    		HttpURLConnection conn = null;
    		InputStream inputStream = null;
    		try {
    			URL url = new URL(path);
    			conn = (HttpURLConnection) url.openConnection();
    			conn.setDoOutput(true);
    			conn.setDoInput(true);
    			conn.setUseCaches(false);
    			conn.setRequestMethod(method);
    			if (null != body) {
    				OutputStream outputStream = conn.getOutputStream();
    				outputStream.write(body.getBytes("UTF-8"));
    				outputStream.close();
    			}
    			
    			//代理返回流信息
    			inputStream = conn.getInputStream();
    			response.setContentType(conn.getContentType());
    			FileUtil.responseAttachment(fileName,inputStream, response);
    		} catch (Exception e) {
    			logger.error(e);
    		}finally{
    			if(conn!=null){
    				conn.disconnect();
    			}
    		}
    	}

    FileUtil转发附件

    /**
    	 * Http响应内容为附件
    	 * @param file
    	 * @param response
    	 */
    	public static void responseAttachment(String fileName ,InputStream in,HttpServletResponse response){
    		if(response==null||in==null||fileName==null){
    			return;
    		}
    		
    		BufferedInputStream bis = null;
    		BufferedOutputStream bos = null;
    		byte[] buff = new byte[1024];
    		int bytesRead;
    		
    		try{
    			response.setHeader("Content-disposition", "attachment; filename="
    					+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));
    			response.setHeader("Content-Length", String.valueOf(in.available()));
    			bis = new BufferedInputStream(in);
    			bos = new BufferedOutputStream(response.getOutputStream());
    			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    				bos.write(buff, 0, bytesRead);
    			}
    			
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			if(bis!=null){
    				try {
    					bos.close();
    					bis.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		
    		}
    	}

    ???????????? 结论:URLConnection的响应流InputStream在没有处理之前就关闭connection会导致该输入流信息为空,getInputStream()的available总是为0。猜测disconnection可能执行了输入流的关闭操作了。response响应头如果不设置Content-Length,客户端也能正确获取到响应文件的。网上看到一种说法说如果将输入流关闭后,则对应的URLConnection也会自动关闭了。还有一个问题是如果Connection没有执行disconnect()操作,会有什么后果呢?

    ?

    cs
    下一篇:没有了