美文网首页
Springboot从URL获取数据流并写入httpRespon

Springboot从URL获取数据流并写入httpRespon

作者: 我爱吃蛋糕_ab5e | 来源:发表于2018-05-15 18:20 被阅读0次
    //获取输入流
    public static InputStream getInputStream() {
            InputStream inputStream = null;
            HttpURLConnection httpURLConnection = null;
            try {
                URL url = new URL("http://10.110.200.157:8080/services/attachment/file/download/AttachmentDownload?id=2429");
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(3000);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setRequestMethod("GET");
                int responseCode = httpURLConnection.getResponseCode();
                if (responseCode == 200) {
                    inputStream = httpURLConnection.getInputStream();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return inputStream;
        }
    
    //测试:写接口将输入流写入httpResponse
    @GET
        @Path(value = "/findFile/{id}")
        @Operation(code = Operation.READ, desc = Operation.READ_DESC)
        public String findFile(@PathParam("id") Long id) throws BusinessAccessException {
    
            String url = "http://10.110.200.157:8080/services/attachment/file/download/AttachmentDownload?id=2429";
            ServletOutputStream outputStream = null;
            InputStream decryptInputStream = getInputStream();
            try{
                httpResponse.reset();
                outputStream = httpResponse.getOutputStream();
                // 在http响应中输出流
                byte[] cache = new byte[1024];
                int nRead = 0;
                while ((nRead = decryptInputStream.read(cache)) != -1) {
                    outputStream.write(cache, 0, nRead);
                    outputStream.flush();
                }
                outputStream.flush();
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    

    相关文章

      网友评论

          本文标题:Springboot从URL获取数据流并写入httpRespon

          本文链接:https://www.haomeiwen.com/subject/cpyydftx.html