Java文件复制(本地与网络资源)

趁休息的时间,写了个文件复制的小程序,以避免了从网上下载的繁琐过程和本地域内机器间文件复制带来一点方便。具体实现如下:

-----------------------------------------------------------------------------------------------------------------------------------

 

package org.sunnysolong.io.file;

 

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

 

/**

* Title: JavaTech<br>

* Description: File Handler Sample<br>

* Copyright: Copyright (c) 2011 <br>

* Create DateTime: Jul 14, 2011 9:30:28 AM <br>

* @author wangmeng

*/

public class FileCopy {

 

private FileCopy(){}

 

/**

* Execute specified file.

* TMD,this can occur the error sometime like this: 

* @param file

*/

private static void open(String file){

try {

Runtime.getRuntime().exec("explorer " + file +"");

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* Local copying.

* @param source

* @param destination

* @param open

*/

public static void localCopy(String source, String directory, boolean open){

InputStream is = null;

File src = null;

String path = null;

try {

src = new File(source);

is = new FileInputStream(src);

System.out.println("file size: " + (Double.parseDouble(String.valueOf(src.length())) / 1024 / 1024) + "MB");

path = directory + "\\" + source.substring(source.lastIndexOf('\\') + 1);

writeBytesIntoFile(convertToBytes(is), path);

if(open){

System.out.println("Opening the file " + path + " ...");

open(path);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

/**

* Remote copying.

* @param source

* @param destination

* @param open

*/

private static void remoteCopy(String source, String directory, boolean open){

URL url = null;

URLConnection urlConn = null;

String path = null;

int contentLength = 0;

try {

url = new URL(source);

urlConn = url.openConnection();

urlConn.setDoInput(true);

urlConn.connect();

contentLength = urlConn.getContentLength();

System.out.println("file size: " + (Double.parseDouble(String.valueOf(contentLength)) / 1024 / 1024) + "MB");

if(contentLength == -1){

throw new Exception("TMD,the current net is a little busy, please try again later.");

}

path = directory + "\\" + source.substring(source.lastIndexOf('/') + 1);

writeBytesIntoFile(convertToBytes(urlConn.getInputStream()), path);

if(open){

System.out.println("Opening the file " + path + " ...");

open(path);

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

 

/**

* Copy key entrance.

* @param source

* @param destination

* @param open

*/

public static void copy(String source, String directory, boolean open){

if(source.startsWith("http://") || source.startsWith("https://")){

System.out.println("Copying the remote source " + source + " to the directory " + directory + " ...");

remoteCopy(source, directory, open);

System.out.println("Copying ended ...");

}else{

System.out.println("Copying the local source " + source + " to the directory " + directory + " ...");

localCopy(source, directory, open);

System.out.println("Copying ended ...");

}

 

}

 

/**

* Read input stream into a byte array and as a return value.

* @param is

* @return byte[]

*/

public static byte[] convertToBytes(InputStream is){

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] b = new byte[1024];

int len = 0;

try {

while((len = is.read(b)) != -1){

out.write(b, 0, len);

}

out.close();

} catch (IOException e) {

e.printStackTrace();

}

return out.toByteArray();

}

/**

* Write bytes into specified file.

* @param b

* @param path

* @return File: the current file.

*/

public static File writeBytesIntoFile(byte[] b, String path){

File file = null;

FileOutputStream fos = null;

try {

file = new File(path);

if(file.exists())

file.delete();

file.createNewFile();

fos = new FileOutputStream(file);

fos.write(b);

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

return file;

}

/**

* Copy image.

* @param source

* @param directory

*/

public static void imageCopy(String source, String directory, boolean open){

URL url = null;

HttpURLConnection conn = null;

String path = null;

try {

url = new URL(source);

conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setReadTimeout(2000);

System.out.println("image file length: " + conn.getContentLength());

path = directory + "\\" + source.substring(source.lastIndexOf('/') + 1);

writeBytesIntoFile(convertToBytes(conn.getInputStream()), path);

if(open){

open(path);

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args){

String source = "http://www.rubyist.net/~slagell/ruby/eval.txt";

FileCopy.copy(source, System.getProperty("user.home"), true);

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值