TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上
; p5 a, S5 N# ^+ e, L2 o上传:; M" w' r8 B. [
- /**
6 _+ O. ]9 A) j) M - * & G- A- J& {$ |4 k2 |8 _, M
- * 上传控制类,上传页面跳转到这里3 F: ^9 s0 d9 B& B
- * 9 `5 V% O8 k7 ~' x
- * @author 科帮网8 i; c, O5 r) k3 L6 ?2 ]
- * @version [版本号, 2012-08-02] # Q3 S# i Z; E' J
- * @see [相关类/方法]
0 y0 X! h9 K. U# P - * @since version 1.0
( K" J. ^; f9 v$ C3 r - */. Z9 d' a- Q* o
- public class UploadHDFSServlet extends HttpServlet {7 Z6 o7 a* z/ x; L: P: I0 _
- . Z8 q" P! [3 [! u& z" K. T
- /** *6 O! E, `5 R, E8 T* N2 t& Q, k4 f2 `
- * * 该方法负责上传控制处理页面 * , x7 y$ n' M% k0 ]& p
- * @param request 客户端到服务器的请求消息
/ {9 o" X% Y4 K9 w6 K - * @param response 服务器返回给页面的消息
8 t/ n- N& D( W! @) j9 V# k+ R - * @throws ServletException 3 K0 A( i" g p9 E5 H% f
- * @throws IOException . x+ Z' Q3 L* d8 n- ~& }
- */+ H' R# [' D: g5 f: Y
- public void doPost(HttpServletRequest request, HttpServletResponse response)
/ U* b2 m. q% r3 f% S/ z& X - throws ServletException, IOException {" {+ k1 l% V e4 g7 t4 j! i
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);
! {* w7 ~- _( Z7 \1 V s" z - if(!isMultipart){
# n+ ] R% |5 E6 d - }else{
" r o% y8 g' z y - FileItemFactory factory = new DiskFileItemFactory();
/ b$ `7 C+ T8 w1 c1 | - ServletFileUpload upload = new ServletFileUpload(factory);& k# O: y: k1 n: x+ D7 b
- try {. k6 X9 s \# x9 Z, X6 R
- List items = upload.parseRequest(request);
7 z3 ~5 [% Y/ |& A5 z' J0 n - Iterator iter = items.iterator();
8 b, V9 P9 A$ D2 p9 _ - while(iter.hasNext()){; F& J' Q$ `3 w5 \% o* z
- FileItem item = (FileItem)iter.next();1 n# d: b7 s5 _( |- e1 Q" n
- if(item.isFormField()){2 r1 v& z3 o& x" ]4 ~
- String name = item.getFieldName();
9 [0 Z2 f+ G8 L9 M5 t* z' P - System.out.println(name);
4 ?* k8 q$ Q# D5 k: O - }else{
4 `5 d) o+ P+ n- X - //表单名字
: M! y5 d! g; |& ^% K8 a$ K - String fileName = item.getName();
$ W/ I5 [: ^' d4 N3 p0 B; V3 S' S - InputStream uploadedStream = item.getInputStream();
" K9 W: E, S$ O - HDFSOperation hdfsOperation = new HDFSOperation();8 C: L8 B7 J; o& L( w
-
6 v( n$ p' @2 [3 k. _* S - String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;8 T6 k% S3 T1 P4 X, o9 D
- boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath); + D* |4 z9 O9 K. r6 D
-
: \9 A% Y5 t/ @0 N! L - if(flag){5 m$ Y7 W: b9 I; m
- response.sendRedirect("success.jsp");% M q) t& d7 [* m. L" }) V! n
- }else{
6 O T3 \3 D% |. A8 Z2 d: o1 |; O" e - response.sendRedirect("failure.jsp");
- _6 D7 F/ h5 x9 o - }
4 O: \ s( ]' o7 U - }. H& X- h8 K$ L2 r) S+ ^
- }; a. D; T3 X' K' f* W4 E( e
-
6 h: h, O/ ?% t" v% K2 B- ?' x# g I& Y7 G - } catch (Exception e) { 2 Y5 K6 C! h- M7 l ~
- e.printStackTrace();
& t. ?) r, F" s. u P - }
' m. B8 X+ u9 b% U) m - }
" e6 b& C% X- `* |+ i1 M8 W4 r4 @ - : _! U6 X/ |; Q( x; R. S" g
- }6 t2 H+ ?$ U% |$ g: `
- 0 c5 e8 [2 |, ?3 V
- }
复制代码 配置:" E1 b$ [2 T* u% L) j) x3 y% a- D
- <configuration>
, s+ W# v1 |- }- ?' ] - <property>. k7 X4 L6 E6 }
- <name>fs.default.name </name>6 T' P% I; R9 O5 s# K' C/ b- f
- <value>hdfs://10.6.12.101:8888</value>, a) B( i" f3 _9 r2 L
- </property>! E) |- X. v0 w5 Q: ]2 v* c
- <property>) a) R3 _: Z: G+ ~" a/ R4 T! t
- <name>hadoop.tmp.dir</name>
; Y8 T7 i6 P3 Z3 V. G. { - <value>/home/hadoopuser/tmp</value>
, h9 ]. g c6 K& z. V# O - </property>) f! `% u& ^2 \6 @: c
- </configuration>
复制代码 源码下载地址:点击下载& W& B6 z. v9 c0 K U" B
6 F' U F2 S# n# q
7 d3 E+ M" ~. [
9 o! k: M5 k* f; R( O: A8 X0 d
|
|