問題導(dǎo)讀
1.本文安裝了哪些軟件? 2.使用了Java什么技術(shù)?
 hadoop作為網(wǎng)盤,不是最好的,但是作為練手還是可以的。同時(shí)hadoop作為網(wǎng)盤實(shí)現(xiàn)方式有多種,本例子,使用的是Java api,當(dāng)然使用rest api同樣是可以的。 詳細(xì)參考下面:
hadoop網(wǎng)盤的最終效果見下面,可以實(shí)現(xiàn)簡單的文件上傳、刪除、下載功能,不同用戶可以登錄到自己的頁面進(jìn)行管理。 一、準(zhǔn)備的安裝包資源
(1)hadoop1.1.2安裝包
(2)bootmetro一個(gè)CSS開元框架,用來提高web前端的開發(fā)效率
(3)mysql的Jdbc驅(qū)動(dòng)包
(4)上傳組件 (5)mysql安裝包(我的電腦是x64,x86系統(tǒng)的請(qǐng)下載對(duì)應(yīng)版本即可) 二、搭建hadoop集群環(huán)境教程見(2)虛擬機(jī)下hadoop1.1.2集群環(huán)境搭建 三、mysql5.6安裝和eclipse上的配置(1)安裝教程網(wǎng)上很多,這里就不詳細(xì)列舉,大家到網(wǎng)上搜索即可。 A、安裝好后,把mysql安裝文件夾下的bin目錄的路徑添加到PATH環(huán)境變量里。 B、然后打開cmd,輸入mysqld,開啟mysql服務(wù)。 C、創(chuàng)建hadoop數(shù)據(jù)庫 打開cmd,輸入:mysql -uroot -hlocalhost -p 然后會(huì)提示輸入密碼:密碼默認(rèn)為空,所以直接回車就可以進(jìn)入Mysql命令行。 接下來輸入:create database hadoop;就創(chuàng)建成功 我們輸入:show databases; 來查看 (2)在eclipse上配置mysql A、首先打開eclipse,創(chuàng)建web工程。 B、把mysql-connector-java-commercial-5.1.25.jar包復(fù)制到在WEB-INF/lib下。 C、鏈接hadoop數(shù)據(jù)庫 在Window菜單欄下打開Open Perspertive,選擇 Database Development。 然后再Database Connections文件夾下郵件選擇New ..; 然后再URL上填上剛剛創(chuàng)建的hadoop數(shù)據(jù)庫; 點(diǎn)擊 Test Connection,測試鏈接成功。 四、fileupload控件實(shí)現(xiàn)文件的上傳(1)首先將commons-fileupload-1.3.1.jar和commons-io-2.4.jar復(fù)制到WEB-INF/lib目錄下。 (2)在WebContent/下創(chuàng)建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>
復(fù)制代碼
然后再創(chuàng)建一個(gè)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.<a target="_blank" class="keylink">jsp</a>.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+"");
- // 驗(yàn)證上傳內(nèi)容了類型
- String contentType = request.getContentType();
- if ((contentType.indexOf("multipart/form-data") >= 0)) {
- DiskFileItemFactory factory = new DiskFileItemFactory();
- // 設(shè)置內(nèi)存中存儲(chǔ)文件的最大值
- factory.setSizeThreshold(maxMemSize);
- // 本地存儲(chǔ)的數(shù)據(jù)大于 maxMemSize.
- factory.setRepository(new File("c:\\temp"));
- // 創(chuàng)建一個(gè)新的文件上傳處理程序
- ServletFileUpload upload = new ServletFileUpload(factory);
- // 設(shè)置最大上傳的文件大小
- 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 () )
- {
- // 獲取上傳文件的參數(shù)
- 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.<a target="_blank" class="keylink">jsp</a>").forward(request, response);
-
- }
- }
- }catch(Exception ex) {
- System.out.println(ex);
- }
- }else{
- System.out.println("<p>No file uploaded</p>");
-
- }
-
-
-
- }
- }
復(fù)制代碼
然后再web.xml下設(shè)置上傳的路徑:
- <?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>
復(fù)制代碼
我們測試一下是否可以上傳,我現(xiàn)在將上傳(1)Centos6.5下hadoop1.1.2環(huán)境搭建(單機(jī)版).docx文件;我們到 - D:\apache-tomcat-6.0.41\webapps\data
復(fù)制代碼可以看到文件已經(jīng)上傳成功。
參考http://www.it165.net/admin/html/201408/3580.html |