马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
IO
- @GetMapping("io")
- public void downloadZip() {
- long start = System.currentTimeMillis();
- EntityWrapper<AttachmentEntity> wrapper = new EntityWrapper<>();
- List<AttachmentEntity> attachments = attachmentMapper.selectList(wrapper);
- String zpiName = "D:/data/压缩文件io.zip";
- try (FileOutputStream fos = new FileOutputStream(
- zpiName); ZipOutputStream zipOutputStream = new ZipOutputStream(fos)) {
- int i = 0;
- for (AttachmentEntity attachment : attachments) {
- String uuid = UUID.randomUUID().toString();
- FileInputStream fileInputStream = new FileInputStream(attachment.getPath());
- zipOutputStream.putNextEntry(new ZipEntry(uuid + "/" + attachment.getName()));
- IOUtils.copy(fileInputStream, zipOutputStream);
- zipOutputStream.closeEntry();
- fileInputStream.close();
- i++;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- long end = System.currentTimeMillis();
- System.out.println("耗时:" + (end - start));
- }
复制代码 NIO
- @GetMapping("nio")
- public void downloadZipNio() throws FileNotFoundException {
- long start = System.currentTimeMillis();
- EntityWrapper<AttachmentEntity> wrapper = new EntityWrapper<>();
- List<AttachmentEntity> attachments = attachmentMapper.selectList(wrapper);
- String zpiName = "D:/data/压缩文件nio.zip";
- try (FileOutputStream fos = new FileOutputStream(zpiName);
- ZipOutputStream zos = new ZipOutputStream(fos);
- WritableByteChannel wbc = Channels.newChannel(zos)) {
- for (AttachmentEntity attachment : attachments) {
- FileInputStream fis = new FileInputStream(attachment.getPath());
- FileChannel channel = fis.getChannel();
- String uuid = UUID.randomUUID().toString();
- zos.putNextEntry(new ZipEntry(uuid + "/" + attachment.getName()));
- channel.transferTo(0, channel.size(), wbc);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- long end = System.currentTimeMillis();
- System.out.println("耗时:" + (end - start));
- }
复制代码 运行时间对比(ms)
ionio375349350321340330
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |