1.模拟QQ相册上传图片(上传指定文件夹下所有的图片)
多线程上传(每个图片开辟一个子线程)
package com.hp.demo1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo01 extends Thread {
public static void main(String[] args) {
Tp t = new Tp();
t.start();
}
static class Tp extends Thread{
@Override
public void run() {
System.out.println("图片开始上传");
for (int i = 1; i <=4; i++) {
file1();
System.out.println(i*25 + "%");
try {
Tp.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("图片上传完毕");
}
private void file1() {
File file = new File("D:/tupian");
File file2 = new File("D:/tupian2");
FileInputStream fi = null;
FileOutputStream fo = null;
File[] f1 = file.listFiles();
for (int j = 0; j < f1.length; j++) {
try {
fi = new FileInputStream(f1[j]);
File out = new File(file2, f1[j].getName());
fo = new FileOutputStream(out);
int a = 0;
while ((a = fi.read()) != -1) {
fo.write(a);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fo.close();
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}