Read a resource from URL and return directly those bytes as response of REST request, with no memory storing with Java 7 and spring MVC 3.2

huangapple go评论109阅读模式
英文:

Read a resource from URL and return directly those bytes as response of REST request, with no memory storing with Java 7 and spring MVC 3.2

问题

我有一个REST端点,需要访问以检索资源(图像、文档...)。

@RequestMapping(value = "/attachement", method = RequestMethod.GET)
@ResponseBody
public Object getTrademarkAttachement(HttpServletResponse response, HttpServletRequest request) {
    //TODO : 从微服务URL检索字节
    //TODO : 将字节发送到前端页面
}

为了检索此文档,我希望通过流式传输来实现。我不想在内存中存储信息。我希望在获取信息的同时,将字节作为响应发送。我的Spring MVC版本是Spring MVC 3.2,我的Java版本是Java 7。能否实现这一点?你能给一些开始调查的线索吗?我知道我在提供有关实现的细节很少,但我从这一点开始,希望能从你那里得到一些想法。

编辑1:

我已经解决了问题的一半。检索URL的不同块。我使用了以下代码:

@Override
public byte[] getTrademarkAttachement() {
    String urlSample = "http://testUrl.com";
    HttpURLConnection httpConn = null;
    String line = null;
    try {
        httpConn = (HttpURLConnection) new URL(urlSample).openConnection();
        InputStream ins = httpConn.getInputStream();
        BufferedReader is = new BufferedReader(new InputStreamReader(ins));
        while ((line = is.readLine()) != null) {
            System.out.println(line);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpConn.disconnect();
    }
    return null;
}

能够访问输入流后,剩下的部分是返回我正在读取的每一行,以便我可以流式传输响应。我必须寻找Spring MVC中提供部分响应的方法。

英文:

I have a REST endpoint that has to be accessed to retrieve a resource (image, document, ...).

@RequestMapping(value = "/attachement", method = RequestMethod.GET)
@ResponseBody
public Object getTrademarkAttachement(HttpServletResponse response, HttpServletRequest request) {
    //TODO : Retrieve bytes from microservice url
    //TODO : Send bytes to frontend page
}

For retrieving this document, I want to do it via streaming . I don't want to store in memory the info . I want to , as I get the info, send the bytes as a response . My version of spring MVC is Spring MVC 3.2 and my version of java is java 7 . Is it possible to achieve this ? could you give any clue to start investigating ? . I know I'm giving little details about implementation but I'm starting with this point and I would want to get some ideas from you .

EDIT 1 :

I have achieved half of the problem . Retrieving different blocks of the url . I have used the following code

 @Override
public byte[] getTrademarkAttachement() {
    String urlSample = "http://testUrl.com";
    HttpURLConnection httpConn = null;
    String line = null;
    try {
        httpConn = (HttpURLConnection) new URL(urlSample).openConnection();
        InputStream ins = httpConn.getInputStream();
        BufferedReader is = new BufferedReader(new InputStreamReader(ins));
        while ((line = is.readLine()) != null) {
            System.out.println(line);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpConn.disconnect();
    }
    return null;
}

Being able to have access to the inputstream , the part that is left is returning each of this lines that I'm reading , so I can stream the response . I have to look for a method in spring MVC that gives a partial response .

答案1

得分: 1

因为您可以获得InputStream,所以您应该能够将OutputStream作为对请求的响应返回。请查看这个链接(https://stackoverflow.com/a/27742486/):

@RequestMapping(value = "/attachement", method = RequestMethod.GET)
@ResponseBody
public void getAttachment(OutputStream out) {

    InputStream in = ; // 从HTTP获取的InputStream,根据您提供的示例设置这个
    byte[] buffer = new byte[1024]; // 您将需要一个小的缓冲区
    int len;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    in.close();
    out.flush();
}
英文:

Since you can get the InputStream, you should be able to return an OutputStream as a response to the request. Take a look at this (https://stackoverflow.com/a/27742486/):

@RequestMapping(value = "/attachement", method = RequestMethod.GET)
@ResponseBody
public void getAttachment(OutputStream out) {

    InputStream in = ; // Set this to the InputStream from HTTP as your provided example
    byte[] buffer = new byte[1024]; // You will need a small buffer mem though
    int len;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    in.close();
    out.flush();
}

答案2

得分: 0

好的,这是翻译后的内容:

好的,我已经解决了我的问题。我附上了解决方案。也许对任何人都有用。

Controller

@RequestMapping(value="/eutm/{trademarkId}/snapshots/{historyId}/attachements/{attachementId}", method = RequestMethod.GET)
@ResponseBody
public void getTrademarkAttachement(HttpServletResponse response, @PathVariable String trademarkId, @PathVariable String historyId, @PathVariable String attachementId) {
    try {
        registerService.getTrademarkAttachement(trademarkId, historyId, attachementId, LanguageController.getLocale(), response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Service

@Override
public void getTrademarkAttachement(String trademarkId, String historyId, String attachementId, Locale locale, ServletOutputStream outputStream) {
    URI uri = loadHistoryUri(generateUri(REGISTER_BASE_MS_URL, REGISTER_HISTORY_ENTRY_TM_ATTACHEMENT_WS_URL, trademarkId, historyId, attachementId), locale.getLanguage());
    HttpURLConnection httpConn = null;
    String line = null;
    InputStream ins = null;
    try {
        httpConn = (HttpURLConnection) new URL(uri.toString()).openConnection();
        ins = httpConn.getInputStream();
        BufferedReader is = new BufferedReader(new InputStreamReader(ins));

        while ((line = is.readLine()) != null) {
            outputStream.write(line.getBytes());
        }
        outputStream.flush();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpConn.disconnect();

        if(ins != null){
            try {
                ins.close();
            } catch (IOException e) {
                logger.error("关闭 inputStream ins 时出现问题");
            }
        }
    }
}

这样,在从输入流(通过GET连接检索的URL)中读取行时,它会直接通过输出流将其写入响应。它不会像响应式模式中一样逐位发送,因此用户不会直接获取信息,但我认为使用Spring MVC 3.2和Java 7是避免内存中元素的最接近方法。

英文:

Ok , I have solved my problem . I attach the solution . Maybe it's useful to anybody.

<b>Controller</b>

@RequestMapping(value=&quot;/eutm/{trademarkId}/snapshots/{historyId}/attachements/{attachementId}&quot;, method = RequestMethod.GET)
@ResponseBody
public void getTrademarkAttachement(HttpServletResponse response, @PathVariable String trademarkId, @PathVariable String historyId, @PathVariable String attachementId) {
    try {
        registerService.getTrademarkAttachement(trademarkId, historyId, attachementId, LanguageController.getLocale(), response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

<b>Service</b>

 @Override
 public void getTrademarkAttachement(String trademarkId, String historyId, String attachementId, Locale locale, ServletOutputStream outputStream) {
    URI uri = loadHistoryUri(generateUri(REGISTER_BASE_MS_URL, REGISTER_HISTORY_ENTRY_TM_ATTACHEMENT_WS_URL, trademarkId, historyId, attachementId), locale.getLanguage());
    HttpURLConnection httpConn = null;
    String line = null;
    InputStream ins = null;
    try {
        httpConn = (HttpURLConnection) new URL(uri.toString()).openConnection();
        ins = httpConn.getInputStream();
        BufferedReader is = new BufferedReader(new InputStreamReader(ins));

        while ((line = is.readLine()) != null) {
            outputStream.write(line.getBytes());
        }
     outputStream.flush();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpConn.disconnect();
        
        if(ins != null){
        try {
            ins.close();
        } catch (IOException e) {
            logger.error(&quot;Bad close of inputStream ins&quot;);
        }
        }
    }
}

This way, as it reads lines from inputStream ( url to retrieve via GET connection ), it writes it directly to the response via outputStream . It doesn't send bit to bit as in reactive mode , so the user is not getting the info directly, but I think that with Spring MVC 3.2 and Java 7 is the most approximate way to avoid elements in memory .

huangapple
  • 本文由 发表于 2020年1月30日 19:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定