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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.