TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上
/ U, k5 Y- O$ l4 U上传:, o$ o3 m% B! Y, ~7 c0 H
- /**
; u* |, N. J3 |) T _5 c0 n - * 1 N; d# c2 o+ x
- * 上传控制类,上传页面跳转到这里
9 ^) b; F% r' @ P" Z - * 9 i* U$ V! D: H# N8 X3 l
- * @author 科帮网
# y5 a, E v3 U. O& Q - * @version [版本号, 2012-08-02] ) v. D2 L* @* H# T
- * @see [相关类/方法] & f, q2 j* A+ q* s5 P
- * @since version 1.0) U3 p/ U1 D$ |8 A& H6 m
- */
& u3 C3 ^3 T3 c" q4 k - public class UploadHDFSServlet extends HttpServlet { k* F" y2 M3 n. v5 A5 T
- 7 f- @6 U5 J2 i. @0 {! @2 C6 p! F
- /** *
6 s! S0 g& X: x - * * 该方法负责上传控制处理页面 *
, G- N, ~$ j& O3 i" Y0 G a - * @param request 客户端到服务器的请求消息$ b W0 N6 X8 g b
- * @param response 服务器返回给页面的消息
0 Q9 i# C& K8 V( R' T - * @throws ServletException ) N9 i+ {. p# U% X: N o
- * @throws IOException ! W9 ]; X- j3 g/ D6 g9 F
- */
0 _9 t- Y# R* ]: r, i - public void doPost(HttpServletRequest request, HttpServletResponse response)
. u4 e, ]! \4 J4 n8 Q2 m! v - throws ServletException, IOException {& V2 T: S% S: D$ T% l: v
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);& p0 L5 ]( A& X$ Y, D
- if(!isMultipart){# `, l# d+ g! t( m8 y3 t
- }else{
. L* L: j& m) t J; D8 h' x7 a6 u - FileItemFactory factory = new DiskFileItemFactory();
2 r, h0 v7 c6 q$ ^ E - ServletFileUpload upload = new ServletFileUpload(factory);
; k! o, h5 d) _1 @ - try {3 Z; E3 x% i& v: H/ s
- List items = upload.parseRequest(request);
2 u5 m9 g: X1 c$ R) o+ C - Iterator iter = items.iterator();
) O' J% T7 w- y - while(iter.hasNext()){$ c" y: Y+ b$ t: H$ N& z) x
- FileItem item = (FileItem)iter.next();
0 K. Q2 n9 ^6 w& L+ `1 L - if(item.isFormField()){
- Q4 e1 H/ G1 A" @& L& v - String name = item.getFieldName();* J+ n) P+ S: t- i0 e$ \
- System.out.println(name);; ?, H3 c; ~9 Q' \! s
- }else{" d* `6 {. x" r+ u6 m
- //表单名字
6 A9 I2 m6 t7 Z! ^, k - String fileName = item.getName();
2 c/ Y$ l& j8 ?# [ - InputStream uploadedStream = item.getInputStream();
2 i/ U- b, s% ] - HDFSOperation hdfsOperation = new HDFSOperation();
% R. R: e2 Z3 t: U( f - 3 s' L8 \+ x/ @. w z0 j
- String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;
: d) O6 A- G: p! p7 K' g - boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath); 9 F1 Q" c9 ~" N2 u- O+ w0 r
-
- }! o9 Y$ D2 o1 E: B" Y - if(flag){
2 D* e" v) B& T; I; H - response.sendRedirect("success.jsp");3 h; b' }, \6 @- U7 }/ l
- }else{
2 ?, ~- k2 p& f% i) r5 |# H; i - response.sendRedirect("failure.jsp");$ v k4 z) z! j2 k2 z0 G2 e9 y/ R
- }* Y( n) z0 J& f% Z) L" J) e
- }) n% u) @2 H6 p2 A+ w
- }: s+ ]5 X# A0 U% s' S1 d
- % j" F; G. @) A3 s
- } catch (Exception e) {
+ M1 {' K8 z. ?) g a1 @ - e.printStackTrace();
% }# K; g5 T2 Z - }
( i- B' l4 k) P9 O5 s4 H) R - } ) r/ }2 c n& A) R+ ^: ?/ r/ Y4 [
- * ^ t2 f' ?( ?8 C- d5 J
- }6 h( A4 z4 i- ]! b
; L% d, y+ e, s5 z& h( T- G- }
复制代码 配置:8 ^* [7 {# Z7 X8 L
- <configuration>
8 B0 X* D* d7 T9 F+ z8 X3 v% w P - <property>
, r0 @" }/ t, J+ I5 G, P - <name>fs.default.name </name>
% {) J3 Q8 F1 R. ]' G. i - <value>hdfs://10.6.12.101:8888</value>
% [1 c9 ` P! g d - </property>
1 ^% ^6 e, b( y- L - <property>
5 I3 _5 t7 ^6 k1 P- N# X - <name>hadoop.tmp.dir</name>
; t' s- e% d# P0 q1 T- Y$ ?9 [ - <value>/home/hadoopuser/tmp</value>
5 p/ O! T0 W1 \ - </property># N L- O8 B* z
- </configuration>
复制代码 源码下载地址:点击下载; ]; B/ B7 c( k/ L2 Z) T1 b& C
# @9 L6 |; R+ @
3 a2 O' ]3 s* v! A" P9 f/ L1 t0 j4 h% x! {: R$ Q
|
|