Microsoft Outlook 和其他日历客户端可以订阅“Internet 日历”。
对于 Outlook,它接受一个 URL(http: 或 webcal:)。正确配置后,Outlook 客户端中显示的“Internet 日历”会保持最新。
我想知道如何自己发布“Internet 日历”。我正在使用 Java。我已经在使用 iCal4j 创建“.ics 文件”事件图书馆。我模糊地假设我需要创建一个发送 ics 流的 servlet。事件。
任何帮助我入门的示例或引用文档都将不胜感激。
最佳答案
我不知道您的实现方式如何,但是当我尝试使用 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.ics
或 http://localhost:8080/,这两种方法在 Outlook 2016 及更高版本中都可以正常工作下载Calendar.ics
。
我返回的 Test.ics
只是从我的日历中以 ics 格式导出的约会。
另外作为旁注,这里是随 Outlook 请求一起发送的 header :
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 可能会发送不同的 header 。 Microsoft 支持中心有一些问题报告:https://support.microsoft.com/en-us/help/4025591/you-can-t-add-an-internet-calendar-in-outlook ,但与“新现代认证”页面的链接断开:)。希望这可以帮助。
关于java - 使用 Java 发布 iCalendar Feed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34341897/