我不知道你的实现是什么样的,但是当我尝试使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试添加 或 ,这两种方法在 Outlook 2016 及更高版本中都可以正常http://localhost:8080/calendar.ics工作http://localhost:8080/downloadCalendar.ics。我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"
]
Run Code Online (Sandbox Code Playgroud)
我还认为,如果https使用身份验证,则可能会出现一些问题,并且在这种情况下,Outlook 可能会发送不同的标头。Microsoft 支持中心有一些问题报告:https://support.microsoft.com/en-us/help/4025591/you-can-t-add-an-internet-calendar-in-outlook,但带有“新现代身份验证”页面的链接已损坏:)。希望这可以帮助。