</pre><pre name="code" class="java">文件上传类:
package com.huawei.it.thread;
import java.io.*;
public class UploadFile {
public File upload(String inPath, String outPath) {
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(getFile(inPath)));
output = new BufferedOutputStream(new FileOutputStream(getOutFile(outPath, inPath)));
int len = 0;
byte[] bt = new byte[1024];
while ((len = input.read(bt)) != -1) {
output.write(bt, 0, len);
}
output.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
public File getFile(String path) {
if (path == null || path.equals("")) {
return null;
}
File file = new File(path);
if (file.isDirectory()) {
return null;
}
return file;
}
public File getOutFile(String path, String inputPath) {
if (path == null || path.equals("")) {
return null;
}
File file = new File(path);
if (file.isDirectory()) {
return new File(file.getAbsolutePath() + "\\"
+ inputPath.substring(inputPath.lastIndexOf("/") + 1));
}
return file;
}
}
线程处理类:
package com.huawei.it.thread;
import java.io.File;
public class CopyFileThread implements Runnable {
private File file;
public CopyFileThread(File file) {
this.file = file;
}
public void run() {
copyFile(file);
}
public void copyFile(File file) {
System.out.println(">>>>>>>>>>>>>>>>>>>>文件开始上传");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("<<<<<<<<<<<<<<<<<<<<<<文件上传完毕");
}
}
测试类:
package com.huawei.it.thread;
import java.io.File;
public class FileUploadThreadTest {
public static final String FILE_PATH = "d:/test/Oracle性能优化.ppt";// 多文件路径
public static final String FILE_PATH2 = "d:/test/SQL性能优化PPT.ppt";// 多文件路径
public static final String FILE_DWNLOAD_PATH = "d:/test/download/";// 多文件路径
public static void main(String[] args) {
UploadFile up = new UploadFile();
File file1 = up.upload(FILE_PATH, FILE_DWNLOAD_PATH);
File file2 = up.upload(FILE_PATH2, FILE_DWNLOAD_PATH);
CopyFileThread cp1 = new CopyFileThread(file1);
CopyFileThread cp2 = new CopyFileThread(file2);
Thread th1 = new Thread(cp1);
Thread th2 = new Thread(cp2);
System.out.println();
th1.start();
th2.start();
System.out.println();
}
}