多线程多文件上传

</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();
		
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值