1:安装NFS

(1)安装

yum install nfs-utils rpcbind

(2)启动rpcbind服务

systemctl restart rpcbind.service

查看服务状态  

systemctl status rpcbind.service

查看rpc

lsof -i :111

netstat -lntup|grep rpcbind

(3)启动NFS服务

systemctl start nfs.service

查看状态

systemctl status nfs.service

查看rpc注册的端口信息

rpcinfo -p localhost

(4)启动顺序一定是rpcbind->nfs,否则有可能出现错误

systemctl start rpcbind.service

systemctl enable rpcbind.service

systemctl start nfs.service

systemctl enable nfs.service

(5)配置端口

nfs除了主程序端口2049和rpcbind的端口111是固定以外,还会使用一些随机端口,以下配置将定义这些端口,以便配置防火墙。

MOUNTD_PORT=4001  

STATD_PORT=4002

LOCKD_TCPPORT=4003

LOCKD_UDPPORT=4003

RQUOTAD_PORT=4004

(6)配置

/home/wzh/nfs    192.168.0.0/24(rw,sync,insecure,no_root_squash)

/home/wzh/nfs    192.168.3.0/24(rw,sync,insecure,no_root_squash)

exportfs -r  #重载exports配置

exportfs -v  #查看共享参数

2:Windows10系统下面挂载测试

C:\Users\yan>mount \\192.168.0.XXX\home\wzh\nfs\ x:

x: 现已成功连接到 \\192.168.0.XXX\home\wzh\nfs\

命令已成功完成。

C:\Users\yan>

3:解决客户端无法写入的问题

[wzh@centos-oracle ~]$ chmod 777 nfs/

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;
} }

CentOS下安装配置NFS并通过Java进行文件上传下载的更多相关文章

  1. java实现文件上传下载

    喜欢的朋友可以关注下,粉丝也缺. 今天发现已经有很久没有给大家分享一篇技术文章了,于是想了一下给大家分享一篇java实现文件上传下载功能的文章,不喜欢的希望大家勿喷. 想必大家都知道文件的上传前端页面 ...

  2. java+大文件上传下载

    文件上传下载,与传统的方式不同,这里能够上传和下载10G以上的文件.而且支持断点续传. 通常情况下,我们在网站上面下载的时候都是单个文件下载,但是在实际的业务场景中,我们经常会遇到客户需要批量下载的场 ...

  3. java web 文件上传下载

    文件上传下载案例: 首先是此案例工程的目录结构:

  4. FasfDFS整合Java实现文件上传下载功能实例详解

    https://www.jb51.net/article/120675.htm 在上篇文章给大家介绍了FastDFS安装和配置整合Nginx-1.13.3的方法,大家可以点击查看下. 今天使用Java ...

  5. FasfDFS整合Java实现文件上传下载

    文章目录     一 : 添加配置文件     二 : 加载配置文件         1. 测试加载配置文件         2. 输出配置文件     三:功能实现         1.初始化连接信 ...

  6. Java web文件上传下载

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52048666 作者:朱培 ID:sdksdk0 邮 ...

  7. fastDFS与java整合文件上传下载

    准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugins是必需要的,是maven用来编译的插件,第二个是maven打源码包的,可以不要. ...

  8. java 实现文件上传下载以及查看

    项目的目录结构 代码  IOUtils.java package cn.edu.zyt.util; import java.io.IOException; import java.io.InputSt ...

  9. Jsch - java SFTP 文件上传下载

    使用Jsch上传.下载文件,核心步骤是:获取channel,然后使用get/put方法下载.上传文件 核心代码句: session = jSch.getSession(ftpUserName, ftp ...

随机推荐

  1. python 全栈开发,Day53(jQuery的介绍,jQuery的选择器,jQuery动画效果)

    js总结 js: 1.ECMAScript5 ES5语法 2.DOM CRUD 获取 3种方式 id tag className //面向对象 对象 : 属性和方法 某个对象中 function $( ...

  2. java / android int类型如何判空?

    /** TextUtils.isEmpty() 方法的实现 * Returns true if the string is null or 0-length. * @param str the str ...

  3. python算法双指针问题:两个有序数组的合并

    最近在看<你也能看得懂的python算法书>, 自己来实现一下里面的算法吧. 有书里的有所不同. 比如这个题目,我的实现如下: from django.test import TestCa ...

  4. Python yaml处理

    安装方式: pip install pyyaml 一.module.yaml为 name: Tom Smith age: 37 spouse: name: Jane Smith age: 25 chi ...

  5. 【noip模拟赛6】收入计划 最大值的最小值 二分答案

    描述 高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱.从今天起,Matrix67将连续工作N天(1<=N<=100 000).每一天末他可以领取当天及前面若干天里没有领取的工 ...

  6. 005 Spark快速入门的简单程序案例

    参考:官网的quick start http://spark.apache.org/docs/1.6.0/quick-start.html 这里只是在shell命令行中简单的书写一些命令,做一个简单的 ...

  7. Windows10下安装Maven以及Eclipse安装Maven插件 + 创建Maven项目

    在官网下载Maven      http://maven.apache.org/download.cgi 下载下来后加压缩,将apache-maven-3.5.4文件夹复制到想要存放它的位置,我放在了 ...

  8. 《Gradle权威指南》--Android Gradle插件

    No1: Android Gradle插件分类 App插件id:com.android.application Library插件id:com.android.library Test插件id:com ...

  9. Qt学习之信号与槽(一)

    Qt学习之信号与槽(一) 目录 QT的信号与槽机制 在窗口的UI设计中操作添加信号和槽     QT的信号与槽机制   QT的两种机制 在Qt和PyQt中有两种通信机制: 低级事件处理机制(low-l ...

  10. mysql-ubuntu卸载安装mysql

    安装MySQL sudo apt-get install mysql-server mysql-client 查看状态 是否是运行中 sudo service mysql status 启动MySQL ...