commons - vfs 도구 업로드 다운로드 실현

6463 단어 자바
package com.visystem.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.provider.ftp.FtpFileSystemConfigBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * vfs     
 * @author julong
 * @date 2019 3 29    3:06:22
 * @desc  
 * 
 */
public class VFSUtils {

	private static final Logger logger = LoggerFactory.getLogger(VFSUtils.class);

	private static FileSystemManager instance = null;

	/**
	 *        
     * jar:../lib/classes.jar!/META-INF/manifest.mf
     * zip:http://somehost/downloads/somefile.zip
     * jar:zip:outer.zip!/nested.jar!/somedir
     * jar:zip:outer.zip!/nested.jar!/some%21dir
     * tar:gz:http://anyhost/dir/mytar.tar.gz!/mytar.tar!/path/in/tar/README.txt
     * tgz:file://anyhost/dir/mytar.tgz!/somepath/somefile
     * gz:/my/gz/file.gz
     * hdfs://somehost:8080/downloads/some_dir
     * hdfs://somehost:8080/downloads/some_file.ext
	 * webdav://somehost:8080/dist
     * http://somehost:8080/downloads/somefile.jar
     * http://myusername@somehost/index.html
	 * ftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz
	 * ftps://myusername:mypassword@somehost/pub/downloads/somefile.tgz
	 * sftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz
	 * tmp://dir/somefile.txt
	 * res:path/in/classpath/image.png
	 * ram:///any/path/to/file.txt
	 * mime:file:///your/path/mail/anymail.mime!/
     * mime:file:///your/path/mail/anymail.mime!/filename.pdf
     * mime:file:///your/path/mail/anymail.mime!/_body_part_0
	 * @return
	 * @throws FileSystemException
	 * @author julong
	 * @date 2019 3 29    6:34:23
	 * @desc
	 */
	public static synchronized FileSystemManager getInstanceManager() throws FileSystemException{
		logger.debug("【vfs     】-     !");
		if (instance == null) {
			try {
				FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
				FileSystemOptions options = new FileSystemOptions();
				//      
				builder.setControlEncoding(options, "UTF-8");
//				builder.setServerLanguageCode(options, "zh");
				instance = VFS.getManager();
			} catch (FileSystemException e) {
				// TODO: handle exception
				logger.debug("【vfs     】-       !",e);
				throw new FileSystemException(e);
			}
		}
		return instance;
	}
	
	/**
	 *   ftpURL
	 * @param username
	 * @param password
	 * @param hostname
	 * @param port
	 * @param relativePath
	 * @return
	 * @author julong
	 * @date 2019 3 29    6:38:46
	 * @desc
	 */
	public static String concatFTPURL(String username,String password,String hostname,String port,String relativePath){
		StringBuffer sb = new StringBuffer();
		sb.append("ftp://");
		sb.append(username);
		sb.append(":");
		sb.append(password);
		sb.append("@");
		sb.append(hostname);
		sb.append(":");
		sb.append(port);
		sb.append(relativePath);
		return sb.toString();
	}
	
	/**
	 * vfs      
	 * @param downloadURL
	 * @return InputStream
	 * @throws IOException
	 * @author julong
	 * @date 2019 3 29    3:42:09
	 * @desc
	 */
	public static InputStream downloadFileToInputStream(String downloadURL) throws IOException{
		logger.debug("【vfs     】-downloadFileToInputStream    downloadURL:{}",downloadURL);
		FileObject ftpFile = instance.resolveFile(downloadURL);
		return ftpFile.getContent().getInputStream();
	}
	
	/**
	 * vfs      
	 * @param downloadURL
	 * @return byte[]
	 * @throws IOException
	 * @author julong
	 * @date 2019 3 29    3:43:24
	 * @desc
	 */
	public static byte[] downloadFileToByte(String downloadURL) throws IOException{
		logger.debug("【vfs     】-downloadFileToByte    downloadURL:{}",downloadURL);
		FileObject ftpFile = instance.resolveFile(downloadURL);
		return IOUtils.toByteArray(ftpFile.getContent().getInputStream());
	}
	
	/**
	 * vfs      
	 * @param filePath
	 * @param bytes
	 * @throws IOException
	 * @author julong
	 * @date 2019 3 29    3:43:35
	 * @desc
	 */
	public static void writeByteArrayToFile(String filePath,byte[] bytes) throws IOException{
		logger.debug("【vfs     】-    filePath:{}",filePath);
		FileUtils.writeByteArrayToFile(new File(filePath), bytes);
	}
	
	
	/**
	 *     
	 * @param uploadURL
	 * @param bytes
	 * @return
	 * @author julong
	 * @date 2019 3 29    5:44:30
	 * @desc
	 */
	public static boolean uploadFileToByte(String uploadURL,byte[] bytes){
		logger.debug("【vfs     】-    uploadURL:{}",uploadURL);
		OutputStream output  =  null;
		try {
			long byteLength = bytes.length;
			FileObject ftpFile = instance.resolveFile(uploadURL);
			if(null != ftpFile && ftpFile.exists() == false){
				ftpFile.createFile();
			}
			output = ftpFile.getContent().getOutputStream();
			IOUtils.write(bytes,output);
			IOUtils.closeQuietly(output);
			if(ftpFile.isFile()){
				long size = ftpFile.getContent().getSize();
				if(byteLength == size){
					logger.debug("【vfs     】-      size:{}",size);
					return true;
				}
			}else{
				logger.debug("【vfs     】-       getURL:{}",ftpFile.getURL());
				return true;
			}
			return false;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("【vfs     】-        :",e);
		}finally{
			if(output != null){
				try {
					output.close();
					output.flush();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		return false;
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			VFSUtils.getInstanceManager();
			String concatFTPURL = VFSUtils.concatFTPURL("vicp", "vicp", "192.168.10.132", "21", "/git.txt");
			System.out.println(concatFTPURL);
			
			byte[] bytes = VFSUtils.downloadFileToByte(concatFTPURL);
			System.out.println(bytes.length);
			boolean result = VFSUtils.uploadFileToByte("ftp://vicp:[email protected]/gggg/vvv/bbb/git1.txt", bytes);
			System.out.println(result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}

좋은 웹페이지 즐겨찾기