博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO流复制文件
阅读量:4561 次
发布时间:2019-06-08

本文共 3854 字,大约阅读时间需要 12 分钟。

1. 经过测试可以复制 txt excel csv exe pdf文件其他格式的没测,估计也没问题

public class Hello {    private static final  String LINE_SEPARATOR = System.getProperty("line.separator");//换行    public static void main(String[] args) throws Exception {        FileInputStream reader = new FileInputStream("d:/毕向东Java基础课堂笔记.pdf");        FileOutputStream writer = new FileOutputStream("e:\\毕向东Java基础课堂笔记.pdf");        int len = 0;        byte[] buffer = new byte[1024*1024];//1MB缓存        while((len = reader.read(buffer))!=-1){ //len = reader.read(buffer) buffer千万不要忘了,否则复制的文件超大            writer.write(buffer ,0 , len);            writer.flush();        }        System.out.println("复制成功");        writer.close();        reader.close();    }}

 2.优化复制文件

public static void main(String[] args) throws Exception {        File src = new File("d:/Evernote_6.1.2.2292.exe");        File des = new File("e:\\NEW_Evernote_6.1.2.2292.exe");        copyFile(src,des);    }    private static void copyFile(File src, File des) throws  Exception{        FileInputStream reader = new FileInputStream(src);        FileOutputStream writer = new FileOutputStream(des);        int len = 0;        byte[] buffer = new byte[1024*1024];//1MB缓存        while((len = reader.read(buffer))!=-1){ //len = reader.read(buffer) buffer千万不要忘了,否则复制的文件超大,陷入死循环            writer.write(buffer ,0 , len);            writer.flush();        }        System.out.println("复制成功");        writer.close();        reader.close();    }

 3.下载文件三要素: contentType, contentLength, setHeader-"Content-Disposition"    

  

File f = new File("d:/photo_04.jpg");			response.setHeader("Content-Disposition", "attachment; filename="+f.getName());			response.setContentType("image/jpeg");				FileInputStream fis = new FileInputStream(f);			response.setContentLength(fis.available());			byte[] buf = new byte[1024];			int len = 0;			while((len = fis.read(buf))!=-1){				response.getOutputStream().write(buf, 0, len);			}

  4.优化: 在doGet方法中调用即可!

private static Map
imageContentType = new HashMap
(); static{ imageContentType.put("jpg", "image/jpeg"); imageContentType.put("jpeg", "image/jpeg"); imageContentType.put("png", "image/png"); imageContentType.put("tif", "image/tiff"); imageContentType.put("tiff", "image/tiff"); imageContentType.put("ico", "image/x-icon"); imageContentType.put("bmp", "image/bmp"); imageContentType.put("gif", "image/gif"); } public static void writeImgToResponse(File image, HttpServletResponse response) throws Exception{ FileInputStream fis = new FileInputStream(image); response.setContentLength(fis.available()); String filename = image.getName(); response.setHeader("Content-Disposition", "attachment; filename=" + filename); String type_key = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase(); response.setContentType(imageContentType.get(type_key)); byte[] buf = new byte[1024*1024]; int len = 0; while((len = fis.read(buf)) != -1){ response.getOutputStream().write(buf, 0, len); } }

  5.继续优化:当用到框架的时候,不懂为什么不设置下载头?

public static void writeImgToResponse(File image, HttpServletResponse response) throws Exception {        FileInputStream inputStream = new FileInputStream(image);        int length = inputStream.available();        byte data[] = new byte[length];        response.setContentLength(length);        String fileName = image.getName();        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();        response.setContentType(imageContentType.get(fileType));        inputStream.read(data);        OutputStream toClient = response.getOutputStream();        toClient.write(data);        toClient.flush();        IOUtils.closeQuietly(toClient);        IOUtils.closeQuietly(inputStream);    }

 

转载于:https://www.cnblogs.com/bravolove/p/5811307.html

你可能感兴趣的文章
promise
查看>>
Go 网络编程笔记
查看>>
[]Java面试题123道
查看>>
http 连接复用
查看>>
ASP.NET页面传值汇总
查看>>
观察者模式
查看>>
bundle update: env: ruby_executable_hooks: No such file or directory
查看>>
Linux重置mysql密码(转载)
查看>>
图片上传
查看>>
中间件与auth认证的那点儿所以然
查看>>
Scala
查看>>
Android 中LinearLayout控件属性
查看>>
面向对象之多态性
查看>>
树状数组
查看>>
【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)
查看>>
pyqt动画的使用
查看>>
pyqt 自定义信号
查看>>
多任务--进程 及 进程间通信
查看>>
多线程/多进程+QProgressBar实现进度条
查看>>
多任务(进程)案例----- 拷贝文件夹
查看>>