gpt4 book ai didi

java - 无法在 Apache CXF + JAX-RS 中同时将字符串和文件作为参数发送到 Web 服务

转载 作者:行者123 更新时间:2023-12-01 15:18:35 25 4
gpt4 key购买 nike

我正在尝试创建一个文件上传 Web 服务,它还需要一些字符串作为我项目的一小部分作为参数传递...

到目前为止,我已经成功创建了一个文件上传网络服务。但仅当我仅传递要作为输入上传的文件时,它才有效。现在我修改了网络服务以接受一些参数。但这给了我错误:

java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "&'"

我不会在这里复制整个堆栈跟踪,我觉得这毫无意义。这是我的网络服务:

    @POST 
@Path("/postdata")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void postData3(List<Attachment> atts, @FormParam("username") final String username, @Context HttpServletRequest request)
{
System.out.println(username);
String home = "/home/yashdosi/s";
for(Attachment att : atts)
{
DataHandler dataHandler = att.getDataHandler();
try
{
InputStream stream = dataHandler.getInputStream();
MultivaluedMap map = att.getHeaders();
OutputStream out = new FileOutputStream(new File(home + "//" + getFileName(map)));

int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
stream.close();
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

这是我的 Java 客户端:

        HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://localhost:8080/fileupload-ws/services/postdata");

FileBody img = new FileBody(new File("/home/yashdosi/1.jpg"));
FileBody html = new FileBody(new File("/home/yashdosi/hotmail.html"));
StringBody contentBody = new StringBody("some.email@gmail.com");

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("image", img);
reqEntity.addPart("html", html);
reqEntity.addPart("username", contentBody);

httppost.setEntity(reqEntity);
//httppost.setHeader("Content-Type", "multipart/form-data");

System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
}

有什么想法导致此错误吗?或者我应该如何解决这个问题!?

最佳答案

尝试使用 @MultiPart(value="image") 表示图像,@MultiPart(value="html") 表示 html 和 @MultiPart(value ="username") 用于内容正文。为我工作。

关于java - 无法在 Apache CXF + JAX-RS 中同时将字符串和文件作为参数发送到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11293553/

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