美文网首页
java后台copy文件的一个写法

java后台copy文件的一个写法

作者: nofantasy | 来源:发表于2017-09-14 16:13 被阅读0次

/**

* 将上传的文件由本地硬盘考到本项目的服务部署的位置

*

* @param preparePath

*            String 原文件路径 如:c:/fqf.txt

* @param newPath

*            String 复制后路径 如:f:/fqf.txt

* @return boolean

*/

public static void copyFile(String storeFileName, String preparePath) {

try {

int bytesum = 0;

int byteread = 0;

// 获取系统根目录

String rootPath = (new File("")).getAbsolutePath();

rootPath = rootPath.substring(0, rootPath.lastIndexOf("\\"));

File file1 = new File(rootPath + "/apache-tomcat/webapps/mobileoa/res");

if (!file1.exists()) {

file1.mkdir();

}

File file2 = new File(rootPath + "/apache-tomcat/webapps/mobileoa/res/data");

if (!file2.exists()) {

file2.mkdir();

}

File oldfile = new File(rootPath + "/apache-tomcat/webapps/mobileoa/res/data/kmAttachment");

if (!oldfile.exists()) {

oldfile.mkdir();

}

// if (oldfile.exists()) { //文件存在时

InputStream inStream = new FileInputStream(preparePath); // 读入原文件

System.out.println("读入文件:" + preparePath);

FileOutputStream fs = new FileOutputStream(rootPath + "/apache-tomcat/webapps/mobileoa/res/data/kmAttachment/" + storeFileName);

System.out.println("最终目标文件:" + rootPath + "/apache-tomcat/webapps/mobileoa/res/data/kmAttachment/" + storeFileName);

System.out.println(fs);

byte[] buffer = new byte[1444];

// int length;

while ((byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; // 字节数 文件大小

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

// }

} catch (Exception e) {

System.out.println("复制单个文件操作出错");

e.printStackTrace();

}

}

相关文章

  • java后台copy文件的一个写法

    /** * 将上传的文件由本地硬盘考到本项目的服务部署的位置 * * @param preparePath * ...

  • kotlin_实体类

    java写法 kotlin写法(使用data,自动生成 get set tostring copy hashcod...

  • 十二

    文件copy两种方式利用 java.io 类库,直接为源文件构建一个 FileInputStream 读取,然后再...

  • 基于JQuery的ajax前后端数据交互

    打开页面即发送请求 像后台发送内容(不包含文件) PS:上边两种ajax写法作用相同,第一种是简写 向后台发送文件...

  • 面试刷题12:zero copy是怎么回事?

    文件copy是java的io部分不可忽视的内容。 我是李福春,我在准备面试,今天的问题是: zero-copy...

  • php转换字符编码为utf-8

    有个需求,把文件夹下的所有HTML文件读取名字,并展示在后台页面 但是这种写法,在后台展示的都是乱码,考虑到读取出...

  • 查询接口名称和字段

    .java xml文件 选择器从后台读取数据:

  • POSTGRESQL 备份与恢复几种方法

    一、Copy COPY在 PostgreSQL表和标准文件系统文件之间 移动数据。COPY TO把一个表的内容复制...

  • shell之for循环

    语法 第一种写法:利用for循环创建50个文件夹 第二种java写法:利用for循环创建50个文件夹

  • golang append 数组切片

    直接append的话数组改变了会跟着改变,所以需要copy一份 写法一 写法二 写法三

网友评论

      本文标题:java后台copy文件的一个写法

      本文链接:https://www.haomeiwen.com/subject/zapdsxtx.html