String dest = "d:/download/" + name;

            Path path = Paths.get(dest).getParent().toAbsolutePath().normalize();

            if(!Files.exists(path))
{
try {
Files.createDirectories(path);
} catch (IOException e) {
e.printStackTrace();
}
} try (FileChannel fc = new FileOutputStream(dest).getChannel()){ ByteBuffer buffer = getResponseAttachment(url);
fc.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class Main {
public static void main(String[] argv) throws Exception {
ByteBuffer bbuf = ByteBuffer.allocate(100);
File file = new File("filename"); boolean append = false; FileChannel wChannel = new FileOutputStream(file, append).getChannel(); wChannel.write(bbuf); wChannel.close();
}
}
String dest = "d:/download/" + name;
try (FileChannel fc = FileChannel.open(Paths.get(dest), StandardOpenOption.WRITE)){
ByteBuffer buffer = getResponseAttachment(url);
fc.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}

将ByteBuffer保存成文件的更多相关文章

  1. Ubuntu10.04中利用V4L2读取摄像头数据并保存成文件【转】

    转自:http://blog.chinaunix.net/uid-29339876-id-4042245.html 利用V4L2读取UVC摄像头数据并保存成视频文件,主要参考http://linuxt ...

  2. 把打印的内容保存成文件(PDF)

    有时候网页的内容和打印的内容会有一些差异,需要把打印的内容倒出来.是有办法的. 1.以谷歌为内核的浏览器示例,按Ctrl+p快捷键打开打印对话框,如图: 2.点击更改按钮,更改打印机,会出现选择目标打 ...

  3. 提取WORD中的所有InlineShape图片并保存成文件

    InlineShape表示的类型其实不只是图片,还包括OLE, ACTIVE X等. 下面是MSDN中的定义:Represents an object in the text layer of a d ...

  4. oracle学习之数据库数据保存成文件

    常常需要将数据库中的数据生成文档,由于比较喜欢脚本的方式,所以就需要使用spool的时候进行格式设置,以下简单整理了一下oracle中进行格式设置的一些东西,一共十八条,其实常用的也就那么几个,稍后会 ...

  5. C# byte[]保存成文件

    string path = Server.MapPath(@"\a.pdf"); FileStream fs = new FileStream(path, FileMode.Cre ...

  6. lumisoft会将eml后缀格式的附件给解析成文本,这里保存成文件

    MIME_Entity[] attachments = mime.Attachments; foreach (MIME_Entity entity in attachments) { string f ...

  7. php把数组保存成文件格式

    php把数组保存为文件格式的函数实例,或许有的还没听说过可以把数组保存成文件,其实这样做也是另有它用的,两种方法各有千秋,有兴趣的PHP爱好者敬请参阅: $file="./cache/fil ...

  8. tcpdump抓包并保存成cap文件

    首选介绍一下tcpdump的常用参数 tcpdump采用命令行方式,它的命令格式为: tcpdump [ -adeflnNOpqStvx ] [ -c 数量 ] [ -F 文件名 ] [ -i 网络接 ...

  9. 将Chrome调试器里的JavaScript变量保存成本地JSON文件

    我写了一个系列的文章,主要用来搜集一些供程序员使用的小工具,小技巧,帮助大家提高工作效率. 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diag ...

随机推荐

  1. PAT Advanced 1022 Digital Library (30 分)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  2. Ubuntu系统---中英文问题小记

    Ubuntu系统---中英文问题小记 Ubuntu系统安装的时候,选择English版本,这样进入tty模式,中文提示不会显示乱码,终端下也是提示英文版的信息. Ubuntu 系统中文显示乱码的问题解 ...

  3. 大数据之路week02--day03 Map集合、Collections工具类的用法

    1.Map(掌握) (1)将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. (2)Map和Collection的区别? A: Map 存储的是键值对形式的元素,键唯一,值可以 ...

  4. Please, commit your changes or stash them before you can merge. Aborting

    1.stash 通常遇到这个问题,你可以直接commit你的修改:但我这次不想这样. 看看git stash是如何做的. git stash    git pull    git stash pop ...

  5. tp5.1下redis配置和使用

    //1.config目录下新建redis.php <?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/12/1 ...

  6. github下载慢的问题

    1. 修改HOSTS文件:在“C:\Windows\System32\drivers\etc” 下的HOSTS文件,添加以下地址:  151.101.44.249 github.global.ssl. ...

  7. 2019牛客暑期多校训练营(第五场)F maximum clique 1 二分图求最大独立集

    https://ac.nowcoder.com/acm/contest/885/F #include <bits/stdc++.h> //CLOCKS_PER_SEC #define se ...

  8. 【算法进阶-康托展开】-C++

    目录 引入 这位老爷子就是康托 基本概念 康托展开是一个全排列到一个自然数的双射,常用于构建hash表时的空间压缩.设有n个数(1,2,3,4,-,n),可以有组成不同(n!种)的排列组合,康托展开表 ...

  9. pyecharts v1 版本 学习笔记 饼图,玫瑰图

    饼图: 普通案例 from example.commons import Faker from pyecharts import options as opts from pyecharts.char ...

  10. cursor: hand和cursor:pointer的区别

    cursor:hand 与 cursor:pointer 的效果是一样的,都像光标指向链接一样,光标变成手行. cursor:hand :IE完全支持.但是在firefox是不支持的,没有效果. cu ...