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

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;
} }
上一篇:【转】MySQL性能优化的最佳21条经验


下一篇:C#开发微信门户及应用(21)-微信企业号的消息和事件的接收处理及解密