CentOS下安装配置NFS并通过Java进行文件上传下载

https://blog.csdn.net/weixin_33859844/article/details/86011253?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=1328740.38960.16169974866720673&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

4:通过Java进行文件上传下载

(1)工具包

commons-lang-2.6.jar
netty-3.2.8.Final.jar
nfs-client-1.0.3.jar
slf4j-api-1.7.25.jar

(2)NfsUtil.java

package com.test;

import com.emc.ecs.nfsclient.nfs.NfsCreateMode;
import com.emc.ecs.nfsclient.nfs.NfsSetAttributes;
import com.emc.ecs.nfsclient.nfs.io.Nfs3File;
import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream;
import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream;
import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;
import com.emc.ecs.nfsclient.rpc.CredentialUnix;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @作者 y
 * @版本 V1.0
 * @描述 NFS工具类
 */
public class NfsUtil {
    private static final String NFS_IP = "192.168.0.XXX";
    private static final String NFS_DIR = "/home/wzh/nfs";
    
    /**
     * 上传文件到NFS服务器
     * @param path   NFS 存储的相对路径
     * @param fileName 文件名称包括文件后缀
     * @param content   文件二进制内容
     * @return 
     */
    public static boolean upload(String path, String fileName, byte []content){
        NfsFileOutputStream outputStream = null;
        
        NfsSetAttributes nfsSetAttr = new NfsSetAttributes();
        nfsSetAttr.setMode((long) (0x00100 + 0x00080 + 0x00040 + 0x00020 + 0x00010 + 0x00008 + 0x00004 + 0x00002));
        
        try {
            Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(-2, -2, null), 3);
            String paths[] = path.substring(1).split("/");//去掉第一个/之后进行分割处理

            StringBuilder p = new StringBuilder();
            
            //首先判断目录是否存在,如果不存在则进行创建目录
            for(String s:paths){
                p.append("/").append(s);
                Nfs3File filePath = new Nfs3File(nfs3, p.toString());
                if (!filePath.exists()) {
                    filePath.mkdir(nfsSetAttr);
                }
            }
            
            
            //创建文件
            Nfs3File desFile = new Nfs3File(nfs3, path+"/"+fileName);
            desFile.create(NfsCreateMode.GUARDED, nfsSetAttr, null);
            
            outputStream = new NfsFileOutputStream(desFile);
            outputStream.write(content);
            
            return true;
        } catch (IOException ex) {
            Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            if(null!=outputStream){
                try {
                    outputStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return false;
    }
    
    /**
     * 文件下载
     * @param filePath NFS上面的文件路径信息
     * @return 
     */
    public static byte[] download(String filePath){
        ByteArrayOutputStream bos = null;
       
        NfsFileInputStream inputStream = null;
        BufferedInputStream bis = null;
        
        try {
            Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(-2, -2, null), 3);
            Nfs3File file = new Nfs3File(nfs3, filePath);
        
            inputStream = new NfsFileInputStream(file);
            
            bis = new BufferedInputStream(inputStream);
            bos = new ByteArrayOutputStream();
            
            int date = -1;
            while ((date = bis.read()) != -1) {
                bos.write(date);
            }
            
            return bos.toByteArray();
        } catch (IOException ex) {
            Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            if(null!=bos){
                try {
                    bos.close();
                } catch (IOException ex) {
                    Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(null!=bis){
                try {
                    bis.close();
                } catch (IOException ex) {
                    Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(null!=inputStream){
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return null;
    }
}3)测试类FileTest.java

package com.test;

import java.io.BufferedOutputStream;
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.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @作者 y
 * @版本 V1.0
 * @描述 
 */
public class FileTest {

    public static void main(String[] args) {
        String fileName = "wszm.pdf";
        int hashcode = fileName.hashCode();
        int dir1 = hashcode & 0xf;  //0--15
        int dir2 = (hashcode & 0xf0) >> 4;  //0-15
        String path = "/" + dir1 + "/" + dir2;
        
        byte []file = fileToBytes("G:\\tmp\\wszm.pdf");
        boolean flag = NfsUtil.upload(path, fileName, file);
        System.out.println("flag:"+flag);
        
        for(int i=0;i<3;i++){
            byte []buff = NfsUtil.download("/t01/t001/tt/ssptbin20180613.7z");
            bytesToFile(buff,"G:\\tmp\\ssptbin20180613"+i+".7z");
        }
        

    }
    
    public static void bytesToFile(byte[] buffer, final String filePath){

        File file = new File(filePath);

        OutputStream output = null;
        BufferedOutputStream bufferedOutput = null;

        try {
            output = new FileOutputStream(file);

            bufferedOutput = new BufferedOutputStream(output);

            bufferedOutput.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(null!=bufferedOutput){
                try {
                    bufferedOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != output){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static byte[] fileToBytes(String filePath) {
        byte[] buffer = null;
        File file = new File(filePath);
        
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;

        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();

            byte[] b = new byte[1024];

            int n;

            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            
            buffer = bos.toByteArray();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
            } finally{
                try {
                    if(null!=fis){
                        fis.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return buffer;
    }
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值