WEB-INF目录下文件复制的几种方式
2018年1月31日 10:42:55
工作完写点博客记录下。
需求:从web-inf下拷贝文件到指定目录。
目录结构

直接贴代码
第一种方式,字节流读取
try {
int index = 0;
System.out.println("开始读取");
File filef = new File("web/WEB-INF/apk/"+channel+".apk");
System.out.println(filef.getAbsolutePath());
InputStream inputStream = new FileInputStream(filef.getAbsolutePath());//获取文件所在路径并读入
if(inputStream!=null){
//读取文件(缓存字节流)
BufferedInputStream in = new BufferedInputStream(inputStream);
//写入相应的文件
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("web/apk/"+channel+".apk"));
//读取数据
//一次性取多少字节
byte[] bytes = new byte[2048];
//接受读取的内容
int n = -1;
//循环取出数据
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//转换成字符串
String str = new String(bytes,0,n,"GBK");
System.out.println(str);
//写入相关文件
out.write(bytes, 0, n);
}
//清除缓存
out.flush();
//关闭流
in.close();
out.close();
}else if(index<=1){
index++;
}
//}
/*}*/
} catch (Exception e) {
e.printStackTrace();
}
注意读取的文件路径要从web开始写!
第二种方式
使用apache的commons的FileUtils
jar:commons-io-2.4.jar
使用方式
@Test
public void test2(){
File file2 = new File("web/apk/22.apk");
File file1 = new File("web/WEB-INF/apk/22.apk");
System.out.println(file1.getAbsolutePath());
try {
FileUtils.copyFile(file1,file2);
} catch (IOException e) {
e.printStackTrace();
}
}
file1是要读取的路径,file2是要写入的路径
贴一下人家工具类的源码
/**
* Copies a file to a new location.
* <p>
* This method copies the contents of the specified source file
* to the specified destination file.
* The directory holding the destination file is created if it does not exist.
* If the destination file exists, then this method will overwrite it.
* <p>
* <strong>Note:</strong> Setting <code>preserveFileDate</code> to
* {@code true} tries to preserve the file's last modified
* date/times using {@link File#setLastModified(long)}, however it is
* not guaranteed that the operation will succeed.
* If the modification operation fails, no indication is provided.
*
* @param srcFile an existing file to copy, must not be {@code null}
* @param destFile the new file, must not be {@code null}
* @param preserveFileDate true if the file date of the copy
* should be the same as the original
*
* @throws NullPointerException if source or destination is {@code null}
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
* @throws IOException if the output file length is not the same as the input file length after the copy
* completes
* @see #copyFileToDirectory(File, File, boolean)
* @see #doCopyFile(File, File, boolean)
*/
public static void copyFile(final File srcFile, final File destFile,
final boolean preserveFileDate) throws IOException {
checkFileRequirements(srcFile, destFile);
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' exists but is a directory");
}
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
}
final File parentFile = destFile.getParentFile();
if (parentFile != null) {
if (!parentFile.mkdirs() && !parentFile.isDirectory()) {
throw new IOException("Destination '" + parentFile + "' directory cannot be created");
}
}
if (destFile.exists() && destFile.canWrite() == false) {
throw new IOException("Destination '" + destFile + "' exists but is read-only");
}
doCopyFile(srcFile, destFile, preserveFileDate);
}
/**
* Internal copy file method.
* This caches the original file length, and throws an IOException
* if the output file length is different from the current input file length.
* So it may fail if the file changes size.
* It may also fail with "IllegalArgumentException: Negative size" if the input file is truncated part way
* through copying the data and the new file size is less than the current position.
*
* @param srcFile the validated source file, must not be {@code null}
* @param destFile the validated destination file, must not be {@code null}
* @param preserveFileDate whether to preserve the file date
* @throws IOException if an error occurs
* @throws IOException if the output file length is not the same as the input file length after the
* copy completes
* @throws IllegalArgumentException "Negative size" if the file is truncated so that the size is less than the
* position
*/
private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate)
throws IOException {
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination '" + destFile + "' exists but is a directory");
} try (FileInputStream fis = new FileInputStream(srcFile);
25 FileChannel input = fis.getChannel();
26 FileOutputStream fos = new FileOutputStream(destFile);
27 FileChannel output = fos.getChannel()) {
final long size = input.size(); // TODO See IO-386
long pos = 0;
long count = 0;
while (pos < size) {
final long remain = size - pos;
count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain;
final long bytesCopied = output.transferFrom(input, pos, count);
if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size
break; // ensure we don't loop forever
}
pos += bytesCopied;
}
} final long srcLen = srcFile.length(); // TODO See IO-386
final long dstLen = destFile.length(); // TODO See IO-386
if (srcLen != dstLen) {
throw new IOException("Failed to copy full contents from '" +
srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen);
}
if (preserveFileDate) {
destFile.setLastModified(srcFile.lastModified());
}
}
重点看红色部分,底层还是字节流,没具体看,可能效率上会更高。
能自己写的就别用人家封装好的,即使用了,也要分析人家的实现方式!
WEB-INF目录下文件复制的几种方式的更多相关文章
- java中文件复制的4种方式
今天一个同事问我文件复制的问题,他一个100M的文件复制的指定目录下竟然成了1G多,吓我一跳,后来看了他的代码发现是自己通过字节流复制的,定义的字节数组很大,导致复制后目标文件非常大,其实就是空行等一 ...
- Java实现文件复制的四种方式
背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...
- shell脚本监控目录下文件被篡改时报警
思路: 目录下文件被篡改的几种可能: 1.被修改 2.被删除 3.新增文件 md5命令详解 参数: -b 以二进制模式读入文件内容 -t 以文本模式读入文件内容 -c 根据已生成的md5值,对现存文件 ...
- C# 获取目录下文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- linux 目录下文件批量植入和删除,按日期打包
linux目录下文件批量植入 [root@greymouster http2]# find /usr/local/http2/htdocs/ -type f|xargs sed -i " ...
- Linux中/proc目录下文件详解
转载于:http://blog.chinaunix.net/uid-10449864-id-2956854.html Linux中/proc目录下文件详解(一)/proc文件系统下的多种文件提供的系统 ...
- php遍历目录下文件,并读取内容
<?php echo "<h2>遍历目录下文件,并读取内容</h2><br>\n"; function listDir($dir) { i ...
- linux获得目录下文件个数
获得当前目录下文件个数赋值给变量panonum: panonum=$(ls -l |grep "^-" | wc -l) 获取指定目录下文件个数赋值给指定变量: panonum=$ ...
- 安装debian 9.1后,中文环境下将home目录下文件夹改为对应的英文
安装了debian 9.1后,中文环境下home目录下文件夹显示的是中文,相当不方便cd命令,改为对应的英文吧,需要用到的软件xdg-user-dirs-gtk #安装需要的软件 sudo apt i ...
随机推荐
- CentOS系统下Redis安装和自启动配置的步骤
相信大家都知道Redis是一个C实现的基于内存.可持久化的键值对数据库,在分布式服务中常作为缓存服务.所以这篇文章将详细介绍在CentOS系统下如何从零开始安装到配置启动服务.有需要的可以参考借鉴. ...
- 正则API
正则表达式:规定字符串中字符出现规律的公式 如果备选字符列表中个别字符之间是连续的,可用-省略中间的字符.比如: 匹配1位数字: [0-9]匹配1位小写字母 : [a-z] 匹配1位大写字母 : ...
- 前端自动化构建工具Gulp简单入门
昨天听同事分享了Gulp的一些简单使用,决定自己也试一试. 一.安装 gulp是基于nodejs的,所以要先下载安装node(直接搜node,在官网下载就好了) 1.全局安装gulp npm inst ...
- ios7对于NSString对象进行了的变更
1.instancetype替代id来做返回值的类型.
- 【笔记】npm 安装 vue-cli
最近完成了慕课网的 高仿饿了么webApp 课程,对于vue 的认识有了更深一步的认识,但是其脚手架 vue-cli 的安装流程还是有点懵,于是今天重新试了一遍加深认识 网上参考过一些有用的教程在这里 ...
- 前端之CSS介绍--选择器
一.CSS简介 介绍 css我们称呼层叠样式表(英文全称:Cascading Style Sheets).它是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等 ...
- linux_rsync定时备份
在linux系统中,需要注意空格使用,有着整体性原则,并且注意大小写问题 Rsync数据同步工具 开源.快速.多功能.可实现全量和增量的本地或远程 具有本地和远程两台主机之间数据快速同步镜像.远程备份 ...
- linux_目录结构
目录的作用是什么? 1. 归档和分类 2. 区分同名文件 什么是FHS? 目录层次标准,linux目录规范标准 linux系统目录有哪些特点? 1. 逻辑上所有目录都在 / 目录下,根目录是所有目录的 ...
- AI_深度学习为何兴起?
深度学习和神经网络,在此技术背后的理念,已经发展了好几十年了,为何现在流行起来了? 最直接因素: 将帮助你在自己的组织中,发现好机会,来应用这些东西 为什么深度学习这么厉害? x轴表示完成任务的数据数 ...
- The server's host key is not cached in the registry. You have no guarantee that the server……
使用putty中的pscp.exe ,可以通过脚本方式实现windows向linux上传文件,但pscp.exe第一次运行时必须手工输入确认信息,本文主要解决掉初次运行时的人工交互,彻底实现静默运行. ...