java 解析 ical,使用java编写.ics iCal文件

I am attempting to implement my own iCal creator using java and for some reason I can't get my .ics file to be recognized. I was wondering what I am doing wrong I can get output that looks exactly like the example from wikipedia. What is the difference between the .ics file and the once that my program has generated.

Their Example:

BEGIN:VCALENDAR

VERSION:2.0

PRODID:-//hacksw/handcal//NONSGML v1.0//EN

BEGIN:VEVENT

UID:uid1@example.com

DTSTAMP:19970714T170000Z

ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com

DTSTART:19970714T170000Z

DTEND:19970715T035959Z

SUMMARY:Bastille Day Party

END:VEVENT

END:VCALENDAR

My .ics file

BEGIN:VCALENDAR

VERSION:1.0

PRODID://Elara/lofy/tanare/delp/314sum2015//

BEGIN:VEVENT

UID:uid1@example.com

DTSTAMP:19970714T170000Z

ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com

DTSTART:19970714T170000Z

DTEND:19970715T035959Z

SUMMARY:Bastille Day Party

END:VEVENT

END:VCALENDAR

This is the code used to generate the .ics file.

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

public class iCal {

private String version = "VERSION:1.0 \n";

private String prodid = "PRODID://Elara/lofy/tanare/delp/314sum2015// \n";

private String calBegin = "BEGIN:VCALENDAR \n";

private String calEnd = "END:VCALENDAR \n";

private String eventBegin = "BEGIN:VEVENT \n";

private String eventEnd = "END:VEVENT \n";

public void iCal(){

}

public void write( String name ){

StringBuilder builder = new StringBuilder();

builder.append(name);

builder.append(".ics");

String testExample = "UID:uid1@example.com\nDTSTAMP:19970714T170000Z\nORGANIZER;

CN=John Doe:MAILTO:john.doe@example.com\nDTSTART:19970714T170000Z

\nDTEND:19970715T035959Z\nSUMMARY:Bastille Day Party\n";

try {

File file = new File(builder.toString());

// if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}

FileWriter fw = new FileWriter(file.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);

bw.write(calBegin);

bw.write(version);

bw.write(prodid);

bw.write(eventBegin);

bw.write(testExample);

bw.write(eventEnd);

bw.write(calEnd);

bw.close();

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

}

}

}

解决方案

Apparently not all the lines in a vCalendar are allowed to end with a space character.

BEGIN:VCALENDAR

...

BEGIN:VEVENT

...

END:VEVENT

END:VCALENDAR

If you remove those spaces, your format validates.

Edit: Also, from the Wikipedia entry on iCalendar:

Each line is terminated by CR+LF (in hexadecimal: 0D0A).

Try using \r\n instead of \n.

net.fortuna.ical4j.model.Calendar 是 ical4j 库中用于表示 iCalendar 格式的类,这个格式通常用于处理日历事件、待办事项等。在 Java 中,如果你需要将一个 .ics 文件(即 iCalendar 文件)上传到七牛云存储,你需要使用七牛提供的 Java SDK。 以下是一个简单的示例步骤,展示如何将一个 .ics 文件上传到七牛云存储: 1. 引入七牛 Java SDK 到你的项目中。 2. 初始化七牛的客户端,通常需要提供你的 Access Key 和 Secret Key。 3. 创建一个输入流来读取你的 .ics 文件。 4. 使用七牛 SDK 提供的方法将文件上传到指定的存储空间(Bucket)。 示例代码如下(注意,代码需要根据实际情况调整): ```java import com.qiniu.http.Response; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import java.io.FileInputStream; import java.io.InputStream; public class QiNiuUploadICS { public static void main(String[] args) { // 你的 Access Key 和 Secret Key String accessKey = "your_access_key"; String secretKey = "your_secret_key"; // 七牛存储空间的名称 String bucket = "your_bucket_name"; // 要上传的 .ics 文件的路径 String localFilePath = "path_to_your_ics_file.ics"; // 构造一个带指定Zone对象的配置类 Auth auth = Auth.create(accessKey, secretKey); UploadManager uploadManager = new UploadManager(); try (InputStream inputStream = new FileInputStream(localFilePath)) { String key = "your_ics_file_name.ics"; // 在七牛云存储中显示的文件名 // 调用 put 方法上传 Response response = uploadManager.put(inputStream, key, getuptoken(bucket)); // 解析上传成功的结果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); } catch (Exception e) { e.printStackTrace(); } } private static String uptoken(String bucketName) { // 生成上传凭证,然后准备上传 return Auth.create("your_access_key", "your_secret_key").uploadToken(bucketName); } } ``` 请注意,上述代码是一个简化的示例,实际使用时你需要根据七牛云存储的具体要求和你的项目需求来调整参数和错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值