简体   繁体   English

使用POST请求将文件发送到REST服务

[英]Sending file to REST Service with POST Request

I'm developing a rest service using RESTEasy, the aim of the service is to get a text file from the POST request and parse it. 我正在使用RESTEasy开发rest服务,该服务的目的是从POST请求中获取文本文件并进行解析。 I have done it in 2 ways: 我以两种方式做到了:

@Path("/HTTPRequestWay")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String uploadFile2(
        @HeaderParam("sourceSystem")String sourceSystem,
        @HeaderParam("payloadType")String payloadType,
        @Context HttpServletRequest request){

    String payloadHTTP = "";
    try {
        payloadHTTP = getBody(request);
    }catch (IOException e){
        e.printStackTrace();
        payloadHTTP = "error";
    }

    return payloadHTTP;
}

@Path("/InputStreamWay")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String uploadFile2(
        @HeaderParam("sourceSystem")String sourceSystem,
        @HeaderParam("payloadType")String payloadType,
        InputStream payload){

    String payloadInputStream = "none";
    try {
        payloadInputStream = IOUtils.toString(payload, "UTF-8");
    }catch (IOException e){
        e.printStackTrace();
        payloadInputStream = "error";
    }
    finally{
        IOUtils.closeQuietly(payload);
    }

    return payloadInputStream ;
}

public static String getBody(HttpServletRequest request) throws IOException {

    String body = null;
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;

    try {
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }
        } else {
            stringBuilder.append("");
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }

    body = stringBuilder.toString();
    return body;
}

i was trying to parse the file either with the HttpServletRequest (getting the body) or putting a parameter in the method (InputStream) .My doubt is about this second case why my method can convert the file i'm sending in the parameter (InputStream payload)? 我试图用HttpServletRequest(获取主体)或将参数放入方法(InputStream)中来解析文件。我的疑问是关于第二种情况,为什么我的方法可以转换我要通过参数(InputStream)发送的文件有效负载)? It looks like magic to me why does it works the second way and which is the better? 对我来说,这看起来像魔术,为什么它能以第二种方式起作用,哪个更好? Thanks 谢谢

It's not magic, it simply injects the InputStream from the HttpServletRequest . 这不是魔术,它只是从HttpServletRequest注入InputStream Take a loot at the docs . 抢劫文档

Resteasy can automatically marshal and unmarshal a few different message bodies. Resteasy可以自动封送和解封一些不同的邮件正文。

One of them is java.io.InputStream . 其中之一是java.io.InputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM