如何将文件名添加到转换后的字节文件中

时间:2015-06-17 10:15:50

标签: java

我正在下载生成的ics日历,在下载ics日历时,它正在下载名称,如" ics"。我想添加像" test.ics"这样的名字,但我无法添加名称。请帮我动态添加名称到下载的ics文件。使用下面的代码将ics文件转换为字节。

private byte[] calendarAsByteArray(net.fortuna.ical4j.model.Calendar calendar) throws FileNotFoundException {
    byte[] bytes;
    try {
        ByteArrayOutputStream output =  new ByteArrayOutputStream();
        CalendarOutputter outputter = new CalendarOutputter();
        outputter.setValidating(false);
        outputter.output(calendar, output);
        bytes = output.toByteArray();

    } catch (Exception e) {
        throw new IllegalStateException("can't convert calendar to bytes: "+e);
    }
    return bytes;
}

1 个答案:

答案 0 :(得分:0)

您可以将您的bytes数组写入文件并指定所需的文件名:

    private byte[] calendarAsByteArray(net.fortuna.ical4j.model.Calendar calendar) throws FileNotFoundException {
        byte[] bytes;
        try {
            ByteArrayOutputStream output =  new ByteArrayOutputStream();
            CalendarOutputter outputter = new CalendarOutputter();
            outputter.setValidating(false);
            outputter.output(calendar, output);
            bytes = output.toByteArray();

            FileOutputStream fos = new FileOutputStream("test.ics");
            fos.write(bytes);
            fos.close()

    } catch (Exception e) {
        throw new IllegalStateException("can't convert calendar to bytes: "+e);
    }
    return bytes;
}