簡體   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