(5)基於hadoop的簡單網盤應用實現1
hadoop網盤的最終效果見下面,可以實現簡單的檔案上傳、刪除、下載功能,不同使用者可以登入到自己的頁面進行管理。
一、準備的安裝包資源
(1)hadoop1.1.2安裝包
(2)bootmetro一個CSS開元框架,用來提高web前端的開發效率
(3)mysql的Jdbc驅動包
(4)上傳元件
(5)mysql安裝包(我的電腦是x64,x86系統的請下載對應版本即可)
二、搭建hadoop叢集環境
三、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="上傳檔案" />
</div>
<div style="line-height:50px;float:left;">
<input type="file" name="file1" size="30"/>
</div>
</form>
</body>
</html>
然後再建立一個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
可以看到檔案已經上傳成功。