繁体   English   中英

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

[英]Cannot send both string and file as parameters to a web service simultaneously in Apache CXF + JAX-RS

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

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

java.lang.IllegalArgumentException:URLDecoder:转义(%)模式中的非法十六进制字符-对于输入字符串:“&'”

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

    @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")用于内容主体。 为我工作。

暂无
暂无

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

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