【问题标题】:Publish iCalendar Feed with Java使用 Java 发布 iCalendar 提要
【发布时间】:2019-07-25 19:11:55
【问题描述】:

Microsoft Outlook 和其他日历客户端可以订阅“Internet 日历”。

对于 Outlook,它接受 URL(http: 或 webcal:)。正确配置后,Outlook 客户端中显示的“Internet 日历”会保持最新状态。

我想知道如何自己发布“互联网日历”。我正在使用 Java。我已经在使用iCal4j 库创建“.ics 文件”事件。我隐约假设我需要创建一个发送ics 事件流的servlet。

任何帮助我入门的示例或参考文档将不胜感激。

【问题讨论】:

  • 什么是“ICS 事件”/“ICS 事件”?请定义和/或链接诸如此类的首字母缩略词。您是指rfc 2445 中提到的“.ics”文件吗?
  • 您好 Frederic,我也在寻找解决此问题的指针。我可以实现 Zapier 来为我做这件事,但我宁愿构建自己的 servlet 来处理提要。你还有解决办法吗?最好的问候,托马斯
  • 我做不到。我没有进一步研究它。我最终通过 SMTP 发送了这些日历邀请,这已经足够了。
  • 您的问题到底是什么?使用 iCal4j,您应该能够创建包含多个事件的日历文件,您可以将其保存到文件或通过 HTTP 从服务器应用程序流式传输。您甚至可以将它直接写入您的 Web 服务器上的静态资源文件 myCalendar.ics。如果日历中的任何内容发生变化,只需覆盖该文件。任何用户都可以将其 URL 添加到他的日历应用程序中。只要您不希望用户拥有对日历的写入权限,这种方法就不会有任何问题。如果这有帮助,请告诉我,我会写一个答案。
  • 我有一个允许下载 mycalendar.ics 的 servlet,但是当我将此 http url 放入 Outlook 的 Internet 日历订阅时,它无法导入文件而不会引发任何错误。但是,当我直接从“打开日历”选项“打开”ics 文件时,它就会显示出来。我不确定这里到底是什么问题。

标签: java outlook icalendar webcal


【解决方案1】:

我不知道您的实现是什么样的,但是当我尝试使用 Spring Boot 进行一些虚拟实现时,我能够完成这项工作。这是我使用的实现:

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Controller
public class CalendarController {

  @GetMapping("/calendar.ics")
  @ResponseBody
  public byte[] getCalendars() throws IOException {
    ClassPathResource classPathResource = new ClassPathResource("Test.ics");
    InputStream inputStream = classPathResource.getInputStream();
    byte[] bytes = inputStream.readAllBytes();
    inputStream.close();
    return bytes;
  }

  @GetMapping("/downloadCalendar.ics")
  public ResponseEntity<Resource> downloadFile(HttpServletRequest request) {
    Resource resource = new ClassPathResource("Test.ics");
    String contentType = null;
    try {
      contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
    } catch (IOException ex) {
      log.info("Could not determine file type.");
    }

    if(contentType == null) {
      contentType = "application/octet-stream";
    }

    return ResponseEntity.ok()
        .contentType(MediaType.parseMediaType(contentType))
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
        .body(resource);
  }
}

如果我尝试添加 http://localhost:8080/calendar.icshttp://localhost:8080/downloadCalendar.ics,这两种方法在 Outlook 2016 及更高版本中都可以正常工作。 我要返回的Test.ics 只是从我的日历中以 ics 格式导出的约会。 另外作为旁注,这里是随 Outlook 请求一起发送的标头:

headers = [
    accept: "*/*", 
    user-agent: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Tablet PC 2.0; Microsoft Outlook 16.0.4861; ms-office; MSOffice 16)", 
    accept-encoding: "gzip, deflate", 
    host: "localhost:8080", 
    connection: "Keep-Alive"
]

我还想,如果使用https,身份验证可能会出现一些问题,并且在这种情况下,Outlook 可能会发送不同的标头。此处 Microsoft 支持中心有一些问题报告:https://support.microsoft.com/en-us/help/4025591/you-can-t-add-an-internet-calendar-in-outlook,但指向“新现代身份验证”页面的链接断开:)。希望这可以帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    相关资源
    最近更新 更多