hadoop网盘环境搭建,代码全部过程

更新时间:2024-01-28 05:18:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

题目:Hadoop完全分布式环境下的网盘项目 姓名:王宏磊 学号:201500800546专业:软件工程 班级:15-01班 手机:17863087272 姓名:苗纯源学号:201500800524专业:软件工程 班级:15-01班 手机:17863085895 项目 起评分 流畅性 美观性 回答问题 安装步骤 编程风格 要求 达到题目基本要求 程序运行过程完整流畅,能清楚说明问题 程序界面美观舒适 能正确回答老师就代码和程序设计的问题 清晰明了 程序书写正规,程序注释完整规范 满分 50 10 10 10 10 10 总评 实际得分 Part1、安装说明

一、虚拟机和linux系统的安装

我们选择的虚拟机是VMware Workstation Pro14.0,linux系统的版本是CentOS-6.6-x86_64-bin-DVD1.iso

(1) 安装虚拟机,运行VMware Workstation Pro14.0,选择合适的安装路径,从网

上找到该版本虚拟机的注册码,输入后一直点下一步直到安装完成即可。

1 / 53

(2) 安装linux系统,打开虚拟机,点击文件->新建虚拟机,选择自定义,下一步,

为了防止版本问题,这里选择硬件兼容性为10.x,选择linux系统镜像的路径,设置linux系统的用户名和密码然后点击下一步,处理机数量选择1个即可,内存暂时设置为1g,若以后内存不够可以再修改,网络类型选择NAT,剩下的均为默认设置,一直下一步直到完成。

完成之后打开该新建的虚拟机,虚拟机会自动安装linux操作系统,等待其安装。安装完成之后输入用户名和密码登录到linux系统中。

(3) 关闭防火墙和selinux

学习环境可以直接把防火墙关闭掉

(1) 用root用户登录后,执行查看防火墙状态。

shell命令为: service iptables status

2 / 53

(2) 用service iptables stop关闭防火墙,这个是临时关闭防火墙。

(3) 如果要永久关闭防火墙用。

chkconfig iptables off

selinux是Linux一个子安全机制,学习环境可以将它禁用。

用命令vim /etc/sysconfig/selinux打开文件设置SELINUX为disabled。

二、Hadoop开发环境的搭建

1、3台主机的hostname修改和Ip地址绑定(三台主机均以root用户登录) (1)3台主机的基本网络环境

CentOS6,64位,在虚拟机下实现。

在虚拟机下安装了一

个centos6系统,然后克隆该虚拟机为3个虚拟机,其Ip地址和主机名分别如下: 192.198.162.133hadoop 192.168.162.131hadoop0 192.168.162.132hadoop1

3 / 53

(2)IP地址与hostname绑定

在hadoop主机下,在控制台中输入vi /etc/sysconfig/network,将hostname修改

为hadoop,在hadoop0主机下操作类似,将HOSTNAME修改为hadoop0;在hadoop1主机下操作类似,将HOSTNAME修改为hadoop1。

在hadoop主机下,输入:vi /etc/hosts,在hosts下添加如下内容

192.168.162.133 hadoop

4 / 53

192.168.162.131 hadoop0 192.168.162.132 hadoop1

然后通过scp命令,将修改好的hosts复制到hadoop0和hadoop1的/etc/hosts文件夹下覆盖:

scp /etc/hosts root@hadoop0:/etc/hosts scp /etc/hosts root@hadoop1:/etc/hosts

(3)测试3太主机之间是否能Ping通 在3台主机下分别输入: ping hadoop ping hadoop0 ping hadoop1

如果3太主机都能ping通,那说明前面的配置成功

2、SSH免密码登录

在hadoop主机下:

(1) 查看当前用户下(root)是否有.ssh的隐藏文件,如果没有就创建一个

5 / 53

命令为mkdir .ssh

(2)生成公钥

输入ssh-keygen -t rsa -P '' (注:最后是二个单引号,表示不设置密码) 然后分发公钥到目标机器

ssh-copy-id -i ~/.ssh/id_rsa.pub root@hadoop1

ssh-copy-id -i ~/.ssh/id_rsa.pub root@hadoop0 (注意不要忘记了参数-i)

(3)验证SSH是否安装成功,以及是否可以免密码登陆本机

ssh localhost ssh hadoop0 ssh hadoop1

6 / 53

在第一次登陆是需要密码,以后则不要。到此,ssh的免密码登陆完成 附:如果失败,有可能是以下原因(在配置过程中我们遇到的是第一种情况,权限不足): 1、权限问题

.ssh目录,以及/home/当前用户 需要700权限,参考以下操作调整 sudo chmod 700 ~/.ssh

sudo chmod 700 /home/当前用户

.ssh目录下的authorized_keys文件需要600或644权限,参考以下操作调整

sudo chmod 600 ~/.ssh/authorized_keys 2、StrictModes问题 编辑

sudo vi /etc/ssh/sshd_config 找到

7 / 53

#StrictModes yes 改成

StrictModes no

如果还不行,可以用ssh -vvv 目标机器ip 查看详情,根据输出内容具体问题具体分析了

参考网址:https://www.cnblogs.com/yjmyzz/p/4481720.html

3、安装和配置hadoop

(1)解压安装hadoop2.9.0

在hadoop主机下:

下载并将hadoop-2.9.0.tar.gz解压到当前用户目录下(/usr/local) tar -zxvf hadoop-2.9.0.tar.gz

然后将hadoop文件夹重命名为hadoop。

(2)配置hadoop

在hadoop主机下:

进入hadoop文件夹下的etc文件夹下的hadoop文件夹,修改配置文件。 1.指定jdk安装位置: Hadoop-env.sh:

export JAVA_HOME=/root/jdk1.8/jdk1.8.0_171 2.hadoop核心配置文件,配置HDFS地址和段口号。 core-site.xml

8 / 53

fs.default.name hdfs://hadoop:9000

hadoop.tmp.dir /usr/local/hadoop/tmp

3.hdfs-site.xml,默认的配置方式是3,这里表示副本数是3,由于我们搭建的子节点只有2个,所以输入1或2 hdfs-site.xml

dfs.replication 2

dfs.permissions false

3.1修改core-default.xml文件,不然在之后的eclipse编程时会出现

9 / 53

filesysnotexist “hdfs”的错误,该文件在hadoop安装目录下的/share/doc/Hadoop/hadoop-project-dist/hadoop-common/下

fs.hdfs.impl

org.apache.hadoop.hdfs.DistributedFileSystem The FileSystem for hdfs: uris.

4.配置MapReduce文件,配置JobTracker的地址和端口 mapred-site.xml

mapred.job.tracker hadoop:9001

5‘修改masters文件,如果没有就创一个空白文件改名为masters。 在其中写入hadoop 6.修改slaves文件 写入hadoop0

hadoop1

7.然后将hadoop文件夹复制到另外2台主机。

scp -R /uer/local/hadoop root@hadoop0:/uer/local /hadoop

10 / 53

scp -R /uer/local/hadoop root@hadoop1:/uer/local /Hadoop 8. 修改环境变量 vi /etc/profile

export JAVA_HOME=/root/jdk1.8/jdk1.8.0_171 export HADOOP_HOME=/usr/local/hadoop

export PATH=.:$HADOOP_HOME/bin:$JAVA_HOME/bin:$PATH 同样,将profile复制到另外两台主机上 scp /etc/profile root@hadoop0:/etc/ scp /etc/profile root@hadoop1:/etc/ 注意请先使用source /etc/profile更新命令

(3)启动hadoop

在hadoop下: 输入:

hadoop namenode -format

然后通过start-all.sh启动所有进程:

.进入到$HADOOP_HOME/sbin目录下启动start-all.sh

可以通过jps命令来查看运行的进程, 如果成功运行,那么将会有如下显示

11 / 53

在hadoop0和hadoop1的shell中输入jps也会有3个运行,其中datanode一定要检查是否存在,如果不存在说明配置存在问题。

附:在配置中,我们遇到datanode无法启动的问题,经查阅后了解到是由于我们使用的是64位linux系统,需要下载对应的64位编译版本替换原来的native文件,下载对应文件后,将准备好的64位的lib包解压到已经安装好的hadoop安装目录的lib/native 和 lib目录下:然后增加环境变量:vi /etc/profile增加下面的内容:

export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native export HADOOP_OPTS=\让环境变量生效source /etc/profile

自检hadoop checknative –a 指令检查hadoop checknative –a 参考地址:https://blog.csdn.net/jack85986370/article/details/51902871

https://www.cnblogs.com/fanfanfantasy/p/4123412.html

至此,hadoop启动成功,要想看集群状态,可以打开下面两个网址:

localhost:8088(mapreduce的web页面,注意hadoop2.0以前的版本端口号为50030,我使用的是2.9.0版本,所有端口号为8088) localhost:50070(HDFS的web页面)

12 / 53

三、搭建并测试开发环境(JAVA)

1、JAVAjdk的安装

(1)查看是否已经安装了java JDK。

Shell下输入java –version

13 / 53

注意:Hadoop机器上的JDK,最好是Oracle的Java JDK,不然会有一些问题,比如可能没有JPS命令。 如果安装了其他版本的JDK,卸载掉。 (2) 安装java JDK

去下载Oracle版本Java JDK:我下载的是1.8版本的

jdk-8u171-linux-x64.tar.gz,然后将jdk-8u171-linux-x64.tar.gz解压到你想安装的目录下,这里我选的目录是/root/jdk1.8

Shell命令为tar -zxvf jdk-8u171-linux-x64.tar.gz -C /root/jdk1.8,也可以直接在文件系统里右键复制粘贴到对应目录

接下来添加环境变量,设置JDK的环境变量 JAVA_HOME。需要修改配置文件/etc/profile,追加

export JAVA_HOME=/root/jdk1.8/jdk1.8.0_171 export

CLASSPATH=.:/root/jdk1.8/jdk1.8.0_171/jre/lib/rt.jar:/root/jdk1.8/jdk1.8.0_171/lib/dt.jar:/root/jdk1.8/jdk1.8.0_171/lib/tools.jar export PATH=$JAVA_HOME/bin:$PATH 修改完毕后,执行 source /etc/profile

三台虚拟机上都要进行对应的操作,或者设置好以个虚拟机的之后,将相应的文件用scp命令复制到另外两台虚拟机上

安装后再次执行 java –version,可以看见已经安装完成。

14 / 53

2、Tomcat的安装

(1) 下载 tomcat Linux 版本

oracle 官网下载地址:http://tomcat.apache.org/download-80.cgi

我下载的是apache-tomcat-7.0.86.tar.gz,注意不要下载8.0及以上版本,否则之

后的编程会提示不兼容问题

(2) 在 usr 目录下新建 tomcat 目录,然后将 下载的 tomcat 用工具拷贝到这个

新建的 tomcat目录中

(3) 解压 tomcat 文件, 由于文件名有点长,我们需要给解压后的文件夹重命名,

以便后面引用方便,我改为了tomcat7.0

(4) 配置环境变量(注意:这里我们前提是已经安装并配置好了 JDK)

15 / 53

追加export CATALINA_HOME=/usr/tomcat/tomcat7.0

export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH

输入如下命令让配置生效:source /etc/profile

(5) 启动 tomcat

进入到 /usr/tomcat/tomcat7.0/bin 目录下,输入如下命令 ./startup.sh

(6) 查看 tomcat 是否启动成功

ps -efigrep java

(7) 关闭的方法为进入到 /usr/tomcat/tomcat8.5/bin 目录下,输入如下命令

./shutdown.sh (8) 验证是否启动成功

启动tomcat 之后,在浏览器输入 http:\\\\localhost:8080。如果出现 tomcat 的主页则启动成功

16 / 53

附:参考资源;http://www.jb51.net/article/116654.htm

3、mysql安装

(1) 下载mysql的linux版本,我这里下载的是mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz

(2) 将安装包解压到/usr/local下并改名为mysql

(3) 添加mysql用户组和mysql用户

先检查是否有mysql用户组和mysql用户,命令为groups mysql,若无,则添加;

groupadd mysql

useradd -r -g mysql mysql

若添加成功则再次groupsmysql时会如下图

(4) 进入mysql目录更改权限

cd /usr/local/mysql/ chown -R mysql:mysql ./ (5) 执行安装脚本

./scripts/mysql_install_db --user=mysql

安装完之后修改当前目录拥有者为root用户,修改data目录拥有者为mysql chown -R root:root ./ chown -R mysql:mysql data (6) 先启动mysql,然后更改mysql密码

./support-files/mysql.server start

17 / 53

如果MySQL启动报错,则可能是已经存在MySQL进程,杀掉即可 ps aux|grep mysql kill -9 上边的进程号

#或者下边一条命令即可杀掉所有MySQL进程 ps aux|grep mysql|awk '{print $2}'|xargs kill -9 MySQL启动之后再执行如下命令更改密码:

./bin/mysqladmin -u root -h localhost.localdomain password 'root' 密码更改后即可登录MySQL ./bin/mysql -h127.0.0.1 -uroot -proot

登录之后将其他用户的密码也可改为root

update mysql.user set password=password('root') where user='root'; flush privileges;

在数据库中创建一个hadoop数据库并在hadoop数据库中创建一个users表,为后面的开发做准备

(7) 增加远程登录权限

本地登陆MySQL后执行如下命令

18 / 53

grant all privileges on *.* to root@'%' identified by 'root'; flush privileges;

(8) 将MySQL加入Service系统服务

cp support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld restart service mysqld status

附:参考网址:https://blog.csdn.net/wwd0501/article/details/71171614,注意该网址中的

第九步不要修改my.cnf文件,否则会导致mysql启动不成功

4、eclipse安装和配置

(1)官网下载eclipse,我下载的是eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz (2)解压安装包到/root/下改名为eclipse

(3)下载hadoop-eclipse-plugin-2.7.3.jar并放到eclipse下的plugins目录下 然后启动eclipse,打开eclipseWindow -> preferences找到hadoop, 设置hadoop

路径

(4)配置eclipse开发tomcat

新建项目, 选择Web project 设置项目名称 选择new Runtime

在弹出对话框配置tomcat版本以及tomcat位置 然后下一步, 下一步

19 / 53

勾选web.xml,并完成

(5) 链接数据库

在Window菜单栏下打开Open Perspertive,选择 Database Development。 然后再Database Connections文件夹下邮件选择New ..; 然后再URL上填上刚刚创建的hadoop数据库; 点击 Test Connection,测试链接成功。

20 / 53

至此,有关hadoop网盘开发的所有配置已经配置完毕,接下来就可以开始代码部分了

PART2代码部分

一、 UI界面

登录界面:

输入账号密码正确后会登录到该用户的网盘,若该用户不存在可以点击注册进行账号的注册,注册完毕后即可进行登录

21 / 53

相关代码: 登录界面数据提交:

<%@pagelanguage=\contentType=\ pageEncoding=\%>

我的网盘

登录

使用以下账号直接登录

QQ登录 微博登录 微信登录

没有账号?立即注册

sduwh.com     欢迎您使用本网盘

对登录界面提交的数据进行处理:

publicclassLoginServletextends HttpServlet {

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

this.doPost(request, response);

23 / 53

}

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException { }

}

UserBeanCl ubc = newUserBeanCl(); if(ubc.checkUser(username, password)){

//用户合法,跳转到界面

HttpSession session = request.getSession(); session.setAttribute(\, username);

JobConf conf = HdfsDAO.config(); FileStatus[] list = hdfs.ls(\+username); request.setAttribute(\,list);

request.getRequestDispatcher(\+username+\).forward(request,

String username = request.getParameter(\); String password = request.getParameter(\);

HdfsDAO hdfs = new HdfsDAO(conf);

response);

}else{ }

//用户不合法,调回登录界面,并提示错误信息 System.out.println(\);

request.getRequestDispatcher(\).forward(request, response);

注册界面:

用户输入用户名邮箱密码验证码之后点击注册即可注册成功,然后用户便可通过登录账

24 / 53

号登录到自己的网盘

相关代码 注册界面代码:

<%@pagelanguage=\contentType=\ pageEncoding=\%>

我的网盘

25 / 53

欢迎注册

使用以下账号直接注册

QQ注册 微博注册 微信注册

已有账号?立即登录

26 / 53

sduwh.com     欢迎您使用本网盘

对注册界面提交的数据进行处理: public class CreateServlet extends HttpServlet{

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws }

this.doPost(request, response);

ServletException, IOException {

//获取提交的数据

String id=request.getParameter(\

String username = request.getParameter(\String password = request.getParameter(\String emal = request.getParameter(\//创建新建用户的对象

UserCreate ubc = new UserCreate(); try {

if(ubc.createUser(id,username,password,emal)){

//用户创建成功,hdfs中创建文件夹 JobConf conf = HdfsDAO.config();

HdfsDAO hdfs = new HdfsDAO(conf); hdfs.mkdirs(\

//跳转界面

request.getRequestDispatcher(\

}else{ }

27 / 53

//用户创建失败,调回登录界面,并提示错误信息 System.out.println(\

request.getRequestDispatcher(\

values('\

}

}

} catch (SQLException e) { }

// TODO Auto-generated catch block System.out.println(\e.printStackTrace();

values('\

网盘主界面:

相关代码:

<%@pagelanguage=\contentType=\ pageEncoding=\%>

<%@pageimport=\%>

Main UI

Backgrounds

Bloc style

active\title=\style=\background:url(css/img/white-title.png);\>

Sidebar style

29 / 53

Hi, User |

退出

全部文件

用户信息

30 / 53

用户名 手机号 邮箱 密码

修改信息

修改邮箱 修改密码 更换绑定手机号 更多

网盘

<%

FileStatus[] list = (FileStatus[])request.getAttribute(\); %>

文件

31 / 53

文件名 属性 大小 删除 下载

<%

String path=null;

path=list[0].getPath().toString().substring(18,list[0].getPath().toString().length()-list[0].getPath().

if(list != null)

for (int i=0; i

%>

<% %>

<%= (list[i].isDir()?\目录\:\文件\) %> <%= list[i].getLen()/1024%>

32 / 53

getName().toString().length()).toString();

if(list[i].isDir()) {

out.print(\

href=\\\+list[i].getPath()+\+list[i].getPath().getName()+\);

}else{ }

out.print(\+list[i].getPath().getName()+\);

g(),\) %>&mypath=<%=list[i].getPath().toString().substring(18,list[i].getPath().toString().length()-list[i].getPath().getName().toString().length()).toString()%>\>

g(),\) %>&mypath=<%=list[i].getPath().toString().substring(18,list[i].getPath().toString().length()-list[i].getPath().getName().toString().length()).toString()%>\>下载

二、数据交互 数据库连接: publicclass ConnDB { }

检测用户登录是否合法: public class UserBeanCl {

private Connection ct = null; public Connection getConn(){ try { //加载驱动

Class.forName(\);

//得到连接 ct =

} catch (Exception e) {

// TODO Auto-generated catch block e.printStackTrace(); } returnct; }

DriverManager.getConnection(\);

private Statement sm = null; private Connection ct = null; private ResultSet rs = null; public void close(){

try {

if(sm != null){ }

if(ct != null){ }

if(rs != null){ }

34 / 53

sm.close(); sm = null;

ct.close(); ct = null;

rs.close(); rs = null;

}

}

}

catch (SQLException e) { }

// TODO Auto-generated catch block e.printStackTrace();

//检查登录用户是否合法

public boolean checkUser(String user, String password){ }

boolean b = false; try { //获得连接 } return b;

ct = new ConnDB().getConn(); //创建statement sm = ct.createStatement();

rs = sm.executeQuery(\if(rs.next()){ }

//说明用户存在

String pwd = rs.getString(3); if(password.equals(pwd)){ } b = false;

//说明密码正确 b = true; b = false;

}else{

}else{

} catch (SQLException e) {

e.printStackTrace(); this.close(); }finally{

用户注册部分与数据库的交互主要代码:

publicbooleancreateUser(String id,String user, String password,String emal) throws SQLException{

booleanb = false;

35 / 53

}

try { } returnb;

//获得连接

ct = new ConnDB().getConn(); //创建statement sm = ct.createStatement();

sm.execute(\

b = true;

values('\+id+\+user+\+emal+\+password+\);

}finally{

this.close();

数据访问层: HdfsDAO.java

publicclass HdfsDAO {

//HDFS访问地址

privatestaticfinal String HDFS = \;

publicHdfsDAO(Configuration conf) { this(HDFS, conf); }

publicHdfsDAO(String hdfs, Configuration conf) { this.hdfsPath = hdfs; this.conf = conf; }

//hdfs路径

private String hdfsPath; //Hadoop系统配置

private Configuration conf;

//启动函数

36 / 53

publicstaticvoid main(String[] args) throws IOException { JobConf conf = config();

HdfsDAO hdfs = new HdfsDAO(conf);

}

//加载Hadoop配置文件 publicstatic JobConf config(){

JobConf conf = newJobConf(HdfsDAO.class); conf.setJobName(\);

conf.addResource(\); conf.addResource(\); conf.addResource(\); returnconf; }

//在根目录下创建文件夹

publicvoidmkdirs(String folder) throws IOException { Path path = new Path(folder);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); if(!fs.exists(path)) { fs.mkdirs(path);

System.out.println(\ + folder); } fs.close(); }

//某个文件夹的文件列表

publicFileStatus[] ls(String folder) throws IOException { Path path = new Path(folder);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); FileStatus[] list = fs.listStatus(path); System.out.println(\ + folder);

System.out.println(\); if(list != null) for (FileStatus f :list) {

System.out.printf(\大小: %dK\\n\, f.getPath().getName(), (f.isDir()?\目

37 / 53

录\:\文件\), f.getLen()/1024); }

System.out.println(\); fs.close();

returnlist; }

publicvoidcopyFile(String local, String remote) throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); //remote---/用户/用户下的文件或文件夹

fs.copyFromLocalFile(new Path(local), new Path(remote));

System.out.println(\ + local + \ + remote); fs.close(); }

//删除文件或文件夹

publicvoidrmr(String folder) throws IOException { Path path = new Path(folder);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.deleteOnExit(path);

System.out.println(\ + folder); fs.close(); }

//下载文件到本地系统

publicvoiddownload(String remote, String local) throws IOException { Path path = new Path(remote);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.copyToLocalFile(path, new Path(local));

System.out.println(\ + remote + \ + local); fs.close(); } }

三、功能实现

38 / 53

文件上传功能: (1)选择要上传的文件

(2)上传完毕:

39 / 53

主要代码:

publicclassUploadServletextends HttpServlet { /**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding(\); String filePath0 = new

String(request.getParameter(\).getBytes(\),\); System.out.println(\+filePath0+\); File file ;

intmaxFileSize = 50 * 1024 *1024; //50M intmaxMemSize = 50 * 1024 *1024; //50M ServletContext context = getServletContext();

String filePath = context.getInitParameter(\); System.out.println(\+filePath+\); // 验证上传内容了类型

String contentType = request.getContentType(); if ((contentType.indexOf(\) >= 0)) {

DiskFileItemFactory factory = newDiskFileItemFactory(); // 设置内存中存储文件的最大值 factory.setSizeThreshold(maxMemSize); // 本地存储的数据大于 maxMemSize. factory.setRepository(new File(\));

40 / 53

// 创建一个新的文件上传处理程序

ServletFileUpload upload = new ServletFileUpload(factory); // 设置最大上传的文件大小 upload.setSizeMax( maxFileSize ); try{

// 解析获取的文件

ListfileItems = upload.parseRequest(request);

// 处理上传的文件

Iteratori = fileItems.iterator();

System.out.println(\); while( i.hasNext () ) {

FileItem fi = (FileItem)i.next(); if( !fi.isFormField () ) { // 获取上传文件的参数

String fieldName = fi.getFieldName(); String fileName = fi.getName();

String fn = fileName.substring( fileName.lastIndexOf(\)+1); System.out.println(\+fn+\); booleanisInMemory = fi.isInMemory(); longsizeInBytes = fi.getSize(); // 写入文件

if( fileName.lastIndexOf(\) >= 0 ){ file = newFile( filePath ,

fileName.substring( fileName.lastIndexOf(\))) ; }else{

file = newFile( filePath ,

fileName.substring(fileName.lastIndexOf(\)+1)) ; } fi.write( file ) ;

System.out.println(\);

System.out.println(\);

41 / 53

//将tomcat上的文件上传到hadoop上

hdfs.copyFile(filePath+\+fn, filePath0+fn);

System.out.println(\);

FileStatus[] list = hdfs.ls(filePath0); request.setAttribute(\,list);

} } }catch(Exception ex) {

System.out.println(ex); } }else{

System.out.println(\);

} } /**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }

42 / 53

JobConf conf = HdfsDAO.config(); HdfsDAO hdfs = new HdfsDAO(conf);

request.getRequestDispatcher(\).forward(request, response);

this.doGet(request, response);

文件删除功能:

主要代码:

publicclassDeleteFileServletextends HttpServlet {

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws

//FileStatus[] list = (FileStatus[])request.getAttribute(\

ServletException, IOException {

String filePath = new String filePath1 = new

43 / 53

String(request.getParameter(\).getBytes(\),\); String(request.getParameter(\).getBytes(\),\);

JobConf conf = HdfsDAO.config();

HdfsDAO hdfs = new HdfsDAO(conf); hdfs.rmr(filePath);

System.out.println(\+filePath+\); System.out.println(\+filePath1+\); FileStatus[] list = hdfs.ls(filePath1); request.setAttribute(\,list); }

文件下载功能:

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws }

this.doGet(request, response); }

request.getRequestDispatcher(\).forward(request,response);

ServletException, IOException {

44 / 53

主要代码:

publicclass DownloadServlet extends HttpServlet {

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws

String local = \; String filePath = new String filePath1 = new

45 / 53

privatestaticfinallongserialVersionUID = 1L;

ServletException, IOException {

String(request.getParameter(\).getBytes(\),\); String(request.getParameter(\).getBytes(\),\);

本文来源:https://www.bwwdw.com/article/s6nw.html

Top