java.lang.IllegalArgumentException : Illegal character in path at index 33: https://box. one.th/app/api/上传

标签 java url httprequest illegalargumentexception

我在做什么我正在尝试使用授权制作 FileUploaderClient 我收到如下错误: java.lang.IllegalArgumentException:索引 33 处的路径中存在非法字符:https://box.one.th/app/api/upload

文件上传客户端

   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

最佳答案

堆栈跟踪中给出的 url 末尾有一个空格

HttpPost httppost = new HttpPost("https://box.one.th/app/api/upload"); 请修剪网址。空格对于 URI 是无效的字符。

引用这个在 url URL encoding the space character: + or %20? 中添加特殊字符

关于java.lang.IllegalArgumentException : Illegal character in path at index 33: https://box. one.th/app/api/上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51202773/

相关文章:

.net - 在任何浏览器中将 URL 参数传递给 ClickOnce 应用程序

c# - 如何修改类库中的HttpRequest QueryString值?

scala - 如何在 Spray/Scala/Java 中处理通过 HTML 表单上传的文件?

ruby-on-rails - 使用 Ruby on Rails 的 HTTP GET 请求:OpenSSL::SSL::SSLError

java - 如何在散列之前验证最小、最大密码长度?

java - Socket客户端如何设置SoTimeout

java - 尝试将日期 YYYY-MM-DD 转换为 DD-MMM-YYYY,但该函数始终返回 January 作为月份。

Python 字符串模式

java - 从网上获取图片并设置到ImageView

java - JSF 2.0 + axis2 集成是否可能?