TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Hadoop是一个能够对大量数据进行分布式处理的软件框架。Hadoop 是可靠的,因为它假设计算元素和存储会失败,因此它维护多个工作数据副本,确保能够针对失败的节点重新分布处理。Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上1 L9 _& @% f1 V8 e! @
上传:7 r# W0 M! P! K0 }. W/ w1 j' s
- /** , z2 H- l$ ^' W5 k* m. N' C% e
- * * c; E* e1 L5 F' Y. L% j! _
- * 上传控制类,上传页面跳转到这里
" |7 g: Y. @4 `- L! n, ^ - *
8 [. S$ m- v; Q% X# g - * @author 科帮网
1 l' ?% Z- Y* M2 D - * @version [版本号, 2012-08-02] 9 t* f1 f* i5 p4 ?0 [
- * @see [相关类/方法] 9 Y) |4 S& S0 ~; d8 g
- * @since version 1.0
: z7 x$ o0 t; C3 V" z! d - */: A/ I# [; V- O8 B
- public class UploadHDFSServlet extends HttpServlet {
( p% S4 E" i/ y0 S: f
1 x% q1 f0 I. p$ \% T3 H! [6 u- /** *0 Z: H2 \! c9 I* G n, Y& M" R
- * * 该方法负责上传控制处理页面 *
5 {) C. H0 e* u2 J - * @param request 客户端到服务器的请求消息
0 G" x+ v/ R) p- Q - * @param response 服务器返回给页面的消息7 {/ Z" M7 L8 V) O( F* o% D
- * @throws ServletException
" u) q) |3 W" F, `* Y0 a8 Q - * @throws IOException 6 Z! S3 p, W. l& J" g
- */. M/ z# D v3 y* k2 D- ~
- public void doPost(HttpServletRequest request, HttpServletResponse response)
7 |' N/ V5 ^8 J9 u6 [ - throws ServletException, IOException {9 ?" o) A( Z3 K+ j1 B1 m2 j: C+ b& d$ H
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);: r6 t! A- E. M7 s4 P4 I
- if(!isMultipart){
, q G' V& v, z9 \+ Y+ L7 m - }else{ + X2 C4 W1 r9 b0 a5 x
- FileItemFactory factory = new DiskFileItemFactory();
0 M* [& z1 C/ R4 ] - ServletFileUpload upload = new ServletFileUpload(factory);
5 o3 B! I K* T% f - try {4 Q i6 i# |5 o$ ^9 @
- List items = upload.parseRequest(request); ; i1 y% O3 i# f( r& @
- Iterator iter = items.iterator();
. A1 b5 N8 J: U# k - while(iter.hasNext()){
1 f7 O9 x* _2 R" P- }1 H0 E. ?- W1 C - FileItem item = (FileItem)iter.next();
0 A) H6 S. l! F; `+ C) h - if(item.isFormField()){
; P! _* R$ D6 E" ^( y8 O - String name = item.getFieldName();5 b9 K/ j w4 b3 i5 R4 u6 P, Z
- System.out.println(name);
2 \, G1 g* p+ W6 \6 s - }else{
6 j& `/ r# _3 N+ `. G8 [3 W - //表单名字
+ d( {2 \8 }; j - String fileName = item.getName();5 ]6 L% {+ m" X, l
- InputStream uploadedStream = item.getInputStream();
5 g9 D# @0 ~7 A+ X5 S' |8 g - HDFSOperation hdfsOperation = new HDFSOperation();) C: ^1 B V+ t/ @7 a7 \
- + P8 G0 _# j( k. g
- String hdfsPath = dfs.HDFSConfig.getHDFSPath() + fileName;
# Q# c1 E% N3 V- m5 X3 O - boolean flag = hdfsOperation.upLoad(uploadedStream, hdfsPath); 3 o! s5 s: S# I1 g
- * _ n7 y; o9 N& G0 G
- if(flag){
/ z/ {0 o) Q4 v) ? - response.sendRedirect("success.jsp");4 o. M9 K: W- b8 s. l
- }else{
8 _/ }/ r" e! s& `7 f! t7 ] - response.sendRedirect("failure.jsp");
* ^- x/ G/ |! J( S) k7 s - }
: X& h i' s3 u9 J8 I; A% F d - }
4 m' ~' r& ^3 f( x/ N - }
+ l& m) M! A& w2 j) m* W6 T' a. N - * O) x, E4 l5 H6 w
- } catch (Exception e) {
) n) j' ~5 f& b, K2 J% m - e.printStackTrace();
! _* ]" ^, Q) y# w& x. |+ {# Q2 d5 ` - }; }5 k# n6 N+ h
- }
6 S* O7 K" F# F/ }( {4 r7 @/ t -
) z- u i1 o B+ g5 o. B - } G, `0 ^5 D! t3 j
0 K# ^3 }2 ]" J# a' O$ g- }
复制代码 配置:' D, q q' F% |% m0 ]
- <configuration>7 |1 N3 B R& H$ s
- <property>" X4 K3 j# o8 N b8 S
- <name>fs.default.name </name>
2 ^) Y& w3 R" b6 ]" l& r - <value>hdfs://10.6.12.101:8888</value>$ m; `5 I! ^2 a7 x! M6 {
- </property>: }5 g5 G; a) s! d8 j6 ^- x
- <property>6 S! u& T8 o% j+ g
- <name>hadoop.tmp.dir</name>
q' C, s" v4 l3 [, `4 @5 E* c7 H2 c - <value>/home/hadoopuser/tmp</value>
$ ~# L9 }& m! i. ^+ J6 C# I! m - </property>. y2 W, Q' K$ V; N5 W
- </configuration>
复制代码 源码下载地址:点击下载+ V0 s* a- z& i* l# }
) }# q2 K: [0 J) t' n8 s1 P# u+ G* @1 u# k
' m$ g( Y G" m* Q; z
|
|