文件流问题: file 写到另一个 file
/**
* 把服务器上源文件写到本项目临时文件夹中;
* 目的为了在jsp页面在线转换PDF时能获取到url
* this.getServletConfig().getServletContext().getRealPath("/") 获取项目运行路径
* @param fjpath 附件相对路径
* */
private void writeToProject(String fjpath) {
String name = null;
String fileFold = null;
InputStream is = null;
FileOutputStream fos = null;
String lswjj = null;
try {
String[] names = fjpath.split("//"); //以斜杠分割,因为保存在附件表中的路径为:/JYGL_ZYJSFJ//2016届毕业简介_1449544857570.pdf
fileFold = names[0]; //文件夹:/JYGL_ZYJSFJ
name = names[1]; //文件名:2016届毕业简介_1449544857570.pdf
lswjj = this.getServletConfig().getServletContext().getRealPath("/")+fileFold; //临时文件夹(自定义路径)
QyDao qydao = new QyDao(); //企业Dao操作类
String xtglPath = qydao.findFilePath(); //xtgl_xtsz(系统设置表)中保存的文件夹路径
File file = new File(xtglPath+fjpath); //源文件 D:\xgxt\files\JYGL_ZYJSFJ\2016届毕业简介_1449544857570.pdf
is = new FileInputStream(file);
File newFold = new File(lswjj); //新的临时文件夹
boolean fold = newFold.exists();
if (fold == false) {
newFold.mkdirs(); //创建文件夹
}
File uploadFile = new File(lswjj + File.separator + name);
//uploadFile:D:\JDevRuntime1213\system12.1.3.0.41.140521.1008\o.j2ee\drs\JywApp\ViewControllerWebApp.war\JYGL_ZYJSFJ\2016届毕业简介_1449544857570.pdf
System.out.println("uploadFile:"+uploadFile);
fos = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024];
int length = buffer.length;
int size = 0;
while ((size = is.read(buffer, 0, length)) != -1) {
fos.write(buffer, 0, size);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.caac;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
public class Doc {
/**
* 负责将path1路径下的文件同步到path2下
*
* @param path1
* OA系统中读取附件的URL
* @param path2
* 档案系统中存放附件的路径
* @throws MalformedURLException
* @throws UnsupportedEncodingException
*/
public void wirteDoc(String path1, String path2)
throws MalformedURLException, UnsupportedEncodingException {
URL file = new URL(path1);
OutputStream outputStream = null;
InputStream inputStream = null;
FileOutputStream out = null;
File temp = null;
try {
inputStream = file.openStream();
byte[] buffer = new byte[1024];// 设置一个大小为1K的数据缓冲空间
int len = 0;
temp = new File(path2.substring(0, path2.lastIndexOf("\\")));
if (!temp.exists()) {
temp.mkdirs();
}
out = new FileOutputStream(path2);
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);// 将缓冲空间的值写入path2对应的文件夹下
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 把服务器上源文件写到本地文件夹中;
*
* @param sourcePath
* 附件源路径
* @param aimFold
* 目标路径文件夹
* @param aimPath
* 目标附件路径
* */
public static void writeToProject(String sourcePath, String aimFold,
String aimPath) throws MalformedURLException,
UnsupportedEncodingException {
InputStream is = null;
FileOutputStream fos = null;
System.out.println("fold:"
+ aimPath.substring(0, aimPath.lastIndexOf("\\")));
try {
File file = new File(sourcePath); // 源文件
is = new FileInputStream(file);
File newFold = new File(aimFold); // 新的文件夹
boolean fold = newFold.exists();
if (fold == false) {
newFold.mkdirs(); // 创建文件夹
}
File uploadFile = new File(aimPath);
fos = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024];
int length = buffer.length;
int size = 0;
while ((size = is.read(buffer, 0, length)) != -1) {
fos.write(buffer, 0, size);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 把服务器上源文件写到本地文件夹中;
*
* @param sourcePath
* 附件文件夹源路径
* @param aimFold
* 目标路径文件夹
* @throws UnsupportedEncodingException
* @throws MalformedURLException
* */
public void readfile(String sourcePath, String aimFold)
throws MalformedURLException, UnsupportedEncodingException {
String sourceFile = "";
String aimPath = "";
File file = new File(sourcePath);
if (!file.isDirectory()) {
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(sourcePath + "\\" + filelist[i]);
sourceFile = sourcePath + "\\" + filelist[i];
if (!readfile.isDirectory()) { // 非文件夹
aimPath = aimFold + (i + 1) + "_" + filelist[i];
System.out.println("aimPath:" + aimPath);
writeToProject(sourceFile, aimFold, aimPath);
} else if (readfile.isDirectory()) {
readfile(sourcePath + "\\" + filelist[i], aimFold);
}
}
}
}
public static void main(String[] args) {
// String str =
// "d:\\asfroot\\userfile\\dag\\dba\\oatemp\\attach\\oatemp_24_4_2.doc";
// System.out.println(str.substring(0, str.lastIndexOf("\\")));
// System.out.println("aaa,bbb,ccc".substring(0,"aaa,bbb,ccc".indexOf(",")));
// Doc doc = new Doc();
// try {
// doc.writeToProject("E:\\test\\勤工助学测试要点.doc", "F:\\aaa\\",
// "F:\\aaa\\test.doc");
// } catch (MalformedURLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
Doc doc = new Doc();
try {
doc.readfile("E:\\test", "F:\\aaa\\");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("finish.");
}
}