首页 > 代码库 > (5)基于hadoop的简单网盘应用实现1
(5)基于hadoop的简单网盘应用实现1
hadoop网盘的最终效果见下面,可以实现简单的文件上传、删除、下载功能,不同用户可以登录到自己的页面进行管理。
一、准备的安装包资源
(1)hadoop1.1.2安装包
(2)bootmetro一个CSS开元框架,用来提高web前端的开发效率
(3)mysql的Jdbc驱动包
(4)上传组件
(5)mysql安装包(我的电脑是x64,x86系统的请下载对应版本即可)
二、搭建hadoop集群环境
教程见(2)虚拟机下hadoop1.1.2集群环境搭建
三、mysql5.6安装和eclipse上的配置
(1)安装教程网上很多,这里就不详细列举,大家到网上搜索即可。
A、安装好后,把mysql安装文件夹下的bin目录的路径添加到PATH环境变量里。
B、然后打开cmd,输入mysqld,开启mysql服务。
C、创建hadoop数据库
打开cmd,输入:mysql -uroot -hlocalhost -p
然后会提示输入密码:密码默认为空,所以直接回车就可以进入Mysql命令行。
接下来输入:create database hadoop;就创建成功
我们输入:show databases; 来查看
(2)在eclipse上配置mysql
A、首先打开eclipse,创建web工程。
B、把mysql-connector-java-commercial-5.1.25.jar包复制到在WEB-INF/lib下。
C、链接hadoop数据库
在Window菜单栏下打开Open Perspertive,选择 Database Development。
然后再Database Connections文件夹下邮件选择New ..;
然后再URL上填上刚刚创建的hadoop数据库;
点击 Test Connection,测试链接成功。
四、fileupload控件实现文件的上传
(1)首先将commons-fileupload-1.3.1.jar和commons-io-2.4.jar复制到WEB-INF/lib目录下。
(2)在WebContent/下创建inedx.jsp文件用于上传文件。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form class="form-inline" method="POST" enctype="MULTIPART/FORM-DATA" action="UploadServlet" > <div style="line-height:50px;float:left;"> <input type="submit" name="submit" value=http://www.mamicode.com/"上传文件" />>然后再创建一个UploadServlet处理上传的文件。
package com.controller; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.PageContext; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class UploadServlet */ public class UploadServlet extends HttpServlet { /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); File file ; int maxFileSize = 50 * 1024 *1024; //50M int maxMemSize = 50 * 1024 *1024; //50M ServletContext context = getServletContext(); String filePath = context.getInitParameter("file-upload"); System.out.println("source file path:"+filePath+""); // 验证上传内容了类型 String contentType = request.getContentType(); if ((contentType.indexOf("multipart/form-data") >= 0)) { DiskFileItemFactory factory = new DiskFileItemFactory(); // 设置内存中存储文件的最大值 factory.setSizeThreshold(maxMemSize); // 本地存储的数据大于 maxMemSize. factory.setRepository(new File("c:\\temp")); // 创建一个新的文件上传处理程序 ServletFileUpload upload = new ServletFileUpload(factory); // 设置最大上传的文件大小 upload.setSizeMax( maxFileSize ); try{ // 解析获取的文件 List fileItems = upload.parseRequest(request); // 处理上传的文件 Iterator i = fileItems.iterator(); System.out.println("begin to upload file to tomcat server</p>"); 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("<br>"+fn+"<br>"); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // 写入文件 if( fileName.lastIndexOf("\\") >= 0 ){ file = new File( filePath , fileName.substring( fileName.lastIndexOf("\\"))) ; //out.println("filename"+fileName.substring( fileName.lastIndexOf("\\"))+"||||||"); }else{ file = new File( filePath , fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write( file ) ; System.out.println("upload file to tomcat server success!"); request.getRequestDispatcher("index.jsp").forward(request, response); } } }catch(Exception ex) { System.out.println(ex); } }else{ System.out.println("<p>No file uploaded</p>"); } } }
然后再web.xml下设置上传的路径:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>TestHadoop</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>UploadServlet</display-name> <servlet-name>UploadServlet</servlet-name> <servlet-class>com.controller.UploadServlet</servlet-class> </servlet> <context-param> <description>Location to store uploaded file</description> <param-name>file-upload</param-name> <param-value> D:\apache-tomcat-6.0.41\webapps\data </param-value> </context-param> <servlet-mapping> <servlet-name>UploadServlet</servlet-name> <url-pattern>/UploadServlet</url-pattern> </servlet-mapping> </web-app>我们测试一下是否可以上传,我现在将上传(1)Centos6.5下hadoop1.1.2环境搭建(单机版).docx文件;
我们到
D:\apache-tomcat-6.0.41\webapps\data可以看到文件已经上传成功。