Java URL:未知协议“C” [英] Java URL: Unknown Protocol "C"

查看:172
本文介绍了Java URL:未知协议“C”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在这个问题上有类似的问题(比如

我正在使用 iText 库从Swing应用程序中生成PDF。 iText的 Jpeg 类需要一个 URL 在其构造函数中找到要添加到PDF文件的图像/ jpg。



当我把这个 URL 设置为我的JPG文件的绝对文件路径时,我得到了一个 MalformedURLException 声明未知协议:c (c是我的本地磁盘上的 C:\ 驱动器)。 b
$ b

是否存在任何黑客/绕行行为,或者 是否在某处放置此JPG,并且 URL 通过网络找到它?这是失败的代码:

  try {
String imageUrl =C:\Users\MyUser \image.jpg;
Jpeg image = new Jpeg(new URL(imageUrl));
} catch(Exception exc){
System.out.println(exc.getMessage());

请注意: URL 不会正确地转义字符串(因此\会转换为\ \等)。

谢谢您需要将路径转到image.jpg文件到 file:// URL ,如下所示:

  String imageUrl =file:/// C:/Users/MyUser/image.jpg; 

否则它会将C解释为URL协议。


I know there are similar questions to this one on SO (like this one), however, after reading through the list of "Questions with similar titles", I still feel strongly that this is unique.

I am working with the iText library to generate PDFs from inside a Swing application. iText's Jpeg class requires a URL in its constructor to locate an image/jpg that you want to add to the PDF file.

When I set this URL to the absolute file path of my JPG file, I get a MalformedURLException claiming unknown protocol: c ("c" being the C:\ drive on my local disk).

Is there any hack/circumvention to this, or do I have to host this JPG somewhere and have the URL find it over the net? Here is the code that is failing:

try {
    String imageUrl = "C:\Users\MyUser\image.jpg";
    Jpeg image = new Jpeg(new URL(imageUrl));
} catch(Exception exc) {
    System.out.println(exc.getMessage());
}

Please note: The URL does properly escape the string (thus "\" are converted to "\ \", etc.).

Thanks in advance!

解决方案

You need to turn the path to the image.jpg file into a file:// URL, like this:

String imageUrl = "file:///C:/Users/MyUser/image.jpg";

Otherwise it interprets the C as the URL protocol.

这篇关于Java URL:未知协议“C”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆