首页 > 解决方案 > java.nio.file.InvalidPathException: Illegal char <:> at index 4: http:\182.18.177.27:78\PrepaidEMFiles\invoice12.pdf

问题描述

在发送带有附件的电子邮件时,我收到了错误。它适用于本地系统文件位置(例如 C://PrepaidEMFiles//invoice123.pdf),但是当我使用(http://182.18.177.27:78/PrepaidEMFiles/invoice1547033786877.pdf)位置时,我得到了error.below 是我的代码。

try{
 `enter code here`....

  String samplePdf = 
  "http://182.18.177.27:78/PrepaidEMFiles/invoice1547033786877.pdf";

   FileSystemResource file = new FileSystemResource(samplePdf); 
   helper.addAttachment(file.getFilename(), file);

  } catch (MessagingException e) {
   LOGGER.error("Exception in sending mail", e);
   exceptionHandlerDao.exceptionHandlineCode(e, "SENDING EMAIL FOR " + 
   inventoryName + " FOR RECHARGE A DEVICE", "");
   }

    sender.send(message);
    }

我尝试过各种方式来忽略“\”之类的(

eg.http:\\\\182.18.177.27:78\\PrepaidEMFiles\\invoice1547033786877.pdf) but still getting the same error.Could someone please help me with that error.

提前致谢!!

我尝试了各种方法来获取带有 URL url = inputstream 示例的文件。但我将二进制文件作为附件。仍然没有用。

完全错误:java.nio.file.InvalidPathException: Illegal char <:> at index 4:

      http:\182.18.177.27:78\PrepaidEMFiles\invoice1547033786877.pdf
     at 
       sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
       at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
      at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
      at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
      at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
      at java.io.File.toPath(File.java:2234)
      at org.springframework.core.io.FileSystemResource.<init> 
     (FileSystemResource.java:82)
     at

我需要远程定位的 pdf 文件将其发送到电子邮件附件。

标签: java

解决方案


问题是您正在使用 FileSystemResource 而这不在文件系统上。它是一个外部托管的 URL。您可能应该使用 URLResource 而不是 FileResource。

在这里查看更多文档

URLResource 应该能够理解您拥有的 HTTP URL。


推荐阅读