Hadoop網盤具體實現(二)
阿新 • • 發佈:2019-02-10
Hadoop 環境搭好之後,我嘗試了實現一個 Hadoop 網盤的小專案,使用 Java 的 api,你也可以選擇其他的。經過修改,網盤頁面具體的效果如下。
實現了檔案的上傳,下載,刪除,使用者登入到自己的資料夾進行簡易的檔案管理。
一、這裡我的前期準備:
1)Hadoop2.7.3
2)mysql 的 Jdbc 驅動包
3) 上傳元件
4) 這裡電腦還需要安裝 mysql
二、Hadoop2.7.3 的安裝配置詳見 Hadoop 網盤具體實現(一)
三、MySQL 的安裝網上的教程很多,不再贅述。這裡著重介紹如何在 eclipse 上快速配置 MySQL。
1)開啟 eclipse,建立 dynamic web project
2)將 mysql-connector-java-5.1.7-bin.jar 複製到 WebContent/WEB-INF/lib / 目錄下
3)連結資料庫
因為我用的是 mac 下的 eclipse,順序可能不太一樣,大概如下,開啟 window–>Perspective–>Open Perspective–>Other
選擇 Database Development,OK
然後再 Database Connections 資料夾下右鍵選擇 New
接著選擇 MySQL,next
接著如下,選擇匹配自己的 MySQL 配置填寫,填完之後可以 test connection,如果顯示 Ping Successfully,則資料庫配置好了
這裡資料庫建表如下
4)、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.lsy.yunpan.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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.mapred.JobConf;
import com.lsy.yunpan.model.HDFSDao;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/UploadServlet")
public final class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final Log log = LogFactory.getLog(UploadServlet.class);
private final int MAX_FILE_SIZE = 50 * 1024 * 1024; // 50M
private final int MAX_MEM_SIZE = 50 * 1024 * 1024; // 50M
private String fileUploadPath;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
}
@Override
public void init() throws ServletException {
super.init();
log.debug("init UploadServlet");
ServletContext context = getServletContext();
this.fileUploadPath = context.getInitParameter("file.upload.path");
log.debug("source file path:" + fileUploadPath + "");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("UTF-8");
File file;
// 驗證上傳內容了型別
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 設定記憶體中儲存檔案的最大值
factory.setSizeThreshold(MAX_MEM_SIZE);
// 本地儲存的資料大於 maxMemSize.
factory.setRepository(new File("/tmp"));
// 建立一個新的檔案上傳處理程式
ServletFileUpload upload = new ServletFileUpload(factory);
// 設定最大上傳的檔案大小
upload.setSizeMax(MAX_FILE_SIZE);
try {
// 解析獲取的檔案
List<FileItem> fileList = upload.parseRequest(request);
// 處理上傳的檔案
Iterator<FileItem> iterator = fileList.iterator();
log.debug("begin to upload file to tomcat server</p>");
while (iterator.hasNext()) {
FileItem item = iterator.next();
if (!item.isFormField()) {
// 獲取上傳檔案的引數
String fileName = item.getName();
String fn = fileName.substring(fileName.lastIndexOf("\\") + 1);
log.debug("<br>" + fn + "<br>");
// boolean isInMemory = item.isInMemory();
// long sizeInBytes = item.getSize();
// 寫入檔案
if (fileName.lastIndexOf("\\") >= 0) {
file = new File(fileUploadPath, fileName.substring(fileName.lastIndexOf("\\")));
} else {
file = new File(fileUploadPath, fileName.substring(fileName.lastIndexOf("\\") + 1));
}
item.write(file);
JobConf conf = HDFSDao.getConfig();
HDFSDao hdfs = new HDFSDao(conf);
hdfs.copyFile(fileUploadPath+File.separator+fn, "/swan/"+fn);
}
}
log.debug("upload file to tomcat server success!");
request.setAttribute("flag", "success");
request.setAttribute("msg", "upload file to tomcat server success!<br/>upload file to hadoop hdfs success!s");
request.getRequestDispatcher("upload.jsp").forward(request, response);
} catch (Exception ex) {
log.error(ex.getMessage());
request.setAttribute("flag", "danger");
request.setAttribute("msg", ex.getMessage());
request.getRequestDispatcher("upload.jsp").forward(request, response);
}
} else {
log.warn("<p>No file uploaded</p>");
request.setAttribute("flag", "warning");
request.setAttribute("msg", "<p>No file uploaded</p>");
request.getRequestDispatcher("upload.jsp").forward(request, response);
}
}
}
然後在 web.xml 下設定上傳的路徑:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>hadoopYupan</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>
<context-param>
<description>Location to store uploaded file</description>
<param-name>file.upload.path</param-name>
<param-value>/Users/apple/tmp</param-value>
</context-param>
</web-app>
我們測試一下是否可以上傳,我現在將上傳(4).docx
可以看到,上傳成功