/**
* 將上傳的文件由本地硬盤考到本項目的服務(wù)部署的位置
*
* @param preparePath
*? ? ? ? ? ? String 原文件路徑 如:c:/fqf.txt
* @param newPath
*? ? ? ? ? ? String 復(fù)制后路徑 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String storeFileName, String preparePath) {
try {
int bytesum = 0;
int byteread = 0;
// 獲取系統(tǒng)根目錄
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("最終目標(biāo)文件:" + 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; // 字節(jié)數(shù) 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
// }
} catch (Exception e) {
System.out.println("復(fù)制單個文件操作出錯");
e.printStackTrace();
}
}