gpt4 book ai didi

Java、JBOSS、接受带有西类牙口音的 MULTIPART_FORM_DATA 和 JSON

转载 作者:行者123 更新时间:2023-12-01 10:06:37 24 4
gpt4 key购买 nike

通过 REST google api,我使用 Content-Type: application/x-www-form-urlencoded 发送 POST。

------WebKitFormBoundary
Content-Disposition: form-data; name="model"
Content-type: application/json

{
"placeId":2,
"reportDate":"2016-03-10T05:00:00.000Z",
"form":{
"apply" :"NO",
"microbasin": {
"id": 1,
"name": "Caño Rubiales"
}
}
}
------WebKitFormBoundary--

在我的方法中,我消耗:

    @POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response create (@Context UriInfo uriInfo,
@Context HttpServletRequest req,
MultipartFormDataInput input) throws IOException
{


List<InputPart> l = input.getFormDataMap().get("model");

String str = new String (l.get(0).getBodyAsString().getBytes("iso-8859-1"), "UTF-8");

System.out.println(str);

InputStream file = input.getFormDataPart("file", new GenericType<InputStream>() {});

return null;
}

因此,接收到的 Caño 符号是 Caýýo。我尝试了很多选项,包括所有编码类型,但没有成功。有人可以帮助我,或者给我一些关于如何使用正确的符号以一种方法接受文件和 json 的建议。

最佳答案

两天后我解决了这个问题。

在我的 pom 中,我将依赖项更新为:

   <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>2.3.5.Final</version>
<scope>provided</scope>
</dependency>

然后我创建了该类。

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;

@Provider
@ServerInterceptor
public class ChilaPreProcessInterceptor implements PreProcessInterceptor
{
@Override
public ServerResponse preProcess (HttpRequest request,
ResourceMethod resourceMethod)
throws Failure, WebApplicationException
{
request.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "*/*; charset=UTF-8");
return null;
}
}

方法:

        public String getBodyPartAsString (List<InputPart> parts) throws IOException
{
InputPart part = parts.get(0);
String value = part.getBody(String.class, null);

return value;
}

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response create (@Context UriInfo uriInfo,
@Context HttpServletRequest req,
MultipartFormDataInput input) throws IOException, ParseException
{

Map<String, List<InputPart>> formParts = input.getFormDataMap();

if (!formParts.containsKey("model"))
{
throw new IllegalArgumentException("Cannot create document due to param missing (model)");
}

//Parsea los datos y los pone en el DTO
String str = getBodyPartAsString(formParts.get("model"));
}

关于Java、JBOSS、接受带有西类牙口音的 MULTIPART_FORM_DATA 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36406283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com