java.lang.IllegalArgumentException:索引33路径中的非法字符:https://box.one.th/app/api/upload

时间:2018-07-06 03:51:20

标签: java url httprequest illegalargumentexception

我在做什么:我正在尝试使FileUploaderClient具有授权 我收到以下错误消息: java.lang.IllegalArgumentException:索引33:https://box.one.th/app/api/upload路径中的非法字符

FileUploaderClient

   public class FileUploaderClient {

    public static void main(String[] args) {

        // the file we want to upload
        File inFile = new File("C://Users//BallZaR5R5//Desktop//nanana.docx");
        System.out.println(inFile.getAbsolutePath());
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(inFile.getAbsolutePath());

             CloseableHttpClient httpclient = HttpClientBuilder.create().build();   

            // server back-end URL
            HttpPost httppost = new HttpPost("https://box.one.th/app/api/upload ");
            MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 


            // set the file input stream and file name as arguments
            builder.addPart("file", new InputStreamBody(fis, inFile.getName()));
            HttpEntity entity = builder.build();
            httppost.setHeader(HttpHeaders.AUTHORIZATION,  "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9");
            httppost.setEntity(entity);


            // execute the request
            HttpResponse response = httpclient.execute(httppost);

            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity responseEntity = response.getEntity();
            String responseString = EntityUtils.toString(responseEntity, "UTF-8");

            System.out.println("[" + statusCode + "] " + responseString);

        } catch (ClientProtocolException e) {
            System.err.println("Unable to make connection");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Unable to read file");
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) fis.close();
            } catch (IOException e) {}
        }
    }


}

错误我的控制台错误

    Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in path at index 33: https://box.one.th/app/api/upload 
    at java.net.URI.create(Unknown Source)
    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:73)
    at chaichana.sitat.test.FileUploaderClient.main(FileUploaderClient.java:37)
Caused by: java.net.URISyntaxException: Illegal character in path at index 33: https://box.one.th/app/api/upload 
    at java.net.URI$Parser.fail(Unknown Source)
    at java.net.URI$Parser.checkChars(Unknown Source)
    at java.net.URI$Parser.parseHierarchical(Unknown Source)
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)
    ... 3 more

1 个答案:

答案 0 :(得分:2)

URL的末尾有一个空格,如堆栈跟踪中所给出的

<div class="card-body"> <input type="text" class="form-control input-default " placeholder="Search Main Task" id="search1" onkeyup="search();"> <h5>Main Task</h5> <div class="table-responsive"> <table class="table" id="mastertable"> <thead> <tr> <th>#</th> <th>TASK#</th> <th>Task Name</th> <th>Prefix</th> <th>Sub</th> </tr> </thead> <tbody> {% set ns = namespace(num=1) %} {% for task in tasklist %} <tr> <td>{{ ns.num }}</td> {% for i in range(task|count) %} <td>{{ task[i] }}</td> {% endfor %} <td style="'float: center"><a href="javascript:myFunction();" ><i class="fa fa-eye"></i>View</a></td> </tr> {% set ns.num = ns.num+1 %} {% endfor %} </tbody> </table> </div> </div> </div> <div class="card" style="width:49%;float: right;overflow-y: auto; height: 400px;"> <div class="card-body"> <input type="text" class="form-control input-default " placeholder="Search Sub Task" id="search2" onkeyup="search1();"> <h5>Sub Task</h5> <div class="table-responsive"> <table class="table" id="mastertable1"> <thead> <tr> <th>#</th> <th>Sub task#</th> <th>Task Name</th> <th>task</th> </tr> </thead> <tbody> {% set ns = namespace(num=1) %} {% for subtask in subtasklist %} <tr> <td>{{ ns.num }}</td> {% for i in range(subtask|count-1) %} <td>{{ subtask[i] }}</td> {% endfor %} <td id="{{ subtask[2] }}">{{ subtask[2] }}</td> </tr> {% set ns.num = ns.num+1 %} {% endfor %} </tbody> </table> </div> </div> </div> 请修整网址。空格是URI的无效字符。

请参考此命令以在网址URL encoding the space character: + or %20?中添加特殊字符