-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1231 lines (585 loc) · 53.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link href="/cato.github.io/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">
<link href="/cato.github.io/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">
<link href="/cato.github.io/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">
<link rel="apple-touch-icon" sizes="180x180" href="/cato.github.io/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/cato.github.io/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/cato.github.io/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/cato.github.io/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT">
<meta property="og:type" content="website">
<meta property="og:title" content="当然我在瞎扯">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="当然我在瞎扯">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="当然我在瞎扯">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/cato.github.io/',
scheme: 'Muse',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/">
<title>当然我在瞎扯</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/cato.github.io/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">当然我在瞎扯</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/cato.github.io/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br>
首页
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/cato.github.io/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
标签
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/cato.github.io/categories/" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br>
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/cato.github.io/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
归档
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/cato.github.io/2018/06/05/文件下载笔记/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Cato">
<meta itemprop="description" content>
<meta itemprop="image" content="/cato.github.io/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="当然我在瞎扯">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/cato.github.io/2018/06/05/文件下载笔记/" itemprop="url">服务器实现文件下载</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-05T17:00:00+08:00">
2018-06-05
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/cato.github.io/2018/06/05/文件下载笔记/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2018/06/05/文件下载笔记/" itemprop="commentCount"></span>
</a>
</span>
<span id="/cato.github.io/2018/06/05/文件下载笔记/" class="leancloud_visitors" data-flag-title="服务器实现文件下载">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="服务器文件下载"><a href="#服务器文件下载" class="headerlink" title="服务器文件下载"></a>服务器文件下载</h1><h2 id="需要的-jar-包"><a href="#需要的-jar-包" class="headerlink" title="需要的 jar 包"></a>需要的 jar 包</h2><ul>
<li>commons-fileupload</li>
<li>commons-io</li>
</ul>
<h2 id="文件下载-API"><a href="#文件下载-API" class="headerlink" title="文件下载 API"></a>文件下载 API</h2><h3 id="使用超链接的方式实现文件的下载"><a href="#使用超链接的方式实现文件的下载" class="headerlink" title="使用超链接的方式实现文件的下载"></a>使用超链接的方式实现文件的下载</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"文件的路径"</span>></span>文件名<span class="tag"></<span class="name">a</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="手动编码实现文件的下载"><a href="#手动编码实现文件的下载" class="headerlink" title="手动编码实现文件的下载"></a>手动编码实现文件的下载</h3><ul>
<li>设置两个头和一个流<ul>
<li>Content-Type: 文件的 MIME 类型</li>
<li>Content-Disposition:浏览器支持该格式的文件,提示下载</li>
<li>设置代表该文件的输入流:输入流是固定 <code>response.getOutputStream()</code></li>
</ul>
</li>
</ul>
<h2 id="附"><a href="#附" class="headerlink" title="附"></a>附</h2><p>遍历一个目录内的所有内容</p>
<p>使用树的遍历:广度优先遍历</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 1. 创建队列</span></span><br><span class="line"><span class="comment"> * 2. 创建根目录</span></span><br><span class="line"><span class="comment"> * 3. 添加根目录进队列</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> * while 循环遍历目录过程:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> * 循环判断条件:判断队列是否为空</span></span><br><span class="line"><span class="comment"> * 循环体:</span></span><br><span class="line"><span class="comment"> * 1. 队列出列一个元素</span></span><br><span class="line"><span class="comment"> * 2. 获取它里面的所有 File 对象(listFiles()),循环遍历 File[]</span></span><br><span class="line"><span class="comment"> * 3. 判断该元素是否叶子节点(使用 isFile() 方法)</span></span><br><span class="line"><span class="comment"> * 4. 返回 true 则为叶子节点,使用 HTML 标签显示该文件链接提供下载</span></span><br><span class="line"><span class="comment"> * 5. 返回 false 则为目录,添加进队列</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line">Queue<File> queue = <span class="keyword">new</span> LinkedList<File>();</span><br><span class="line">File root = <span class="keyword">new</span> File(<span class="string">"C:\\Users\\cato\\Desktop\\Resource"</span>);</span><br><span class="line">queue.offer(root);</span><br><span class="line"><span class="keyword">while</span> (!queue.isEmpty()) {</span><br><span class="line"> File file = queue.poll();</span><br><span class="line"> File[] files = file.listFiles();</span><br><span class="line"> <span class="keyword">for</span> (File son : files) {</span><br><span class="line"> <span class="keyword">if</span> (son.isFile()) {</span><br><span class="line"> %></span><br><span class="line"> <h4><a href="${ pageContext.request.contextPath }/DownloadServlet?filename=<%= son.getCanonicalPath() %>"><%= son.getName() %></a> </h4></span><br><span class="line"> <%</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> queue.offer(son);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/cato.github.io/2018/06/05/文件上传笔记/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Cato">
<meta itemprop="description" content>
<meta itemprop="image" content="/cato.github.io/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="当然我在瞎扯">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/cato.github.io/2018/06/05/文件上传笔记/" itemprop="url">服务器实现文件上传</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-05T15:00:00+08:00">
2018-06-05
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/cato.github.io/2018/06/05/文件上传笔记/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2018/06/05/文件上传笔记/" itemprop="commentCount"></span>
</a>
</span>
<span id="/cato.github.io/2018/06/05/文件上传笔记/" class="leancloud_visitors" data-flag-title="服务器实现文件上传">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="服务器文件上传"><a href="#服务器文件上传" class="headerlink" title="服务器文件上传"></a>服务器文件上传</h1><h2 id="需要的-jar-包"><a href="#需要的-jar-包" class="headerlink" title="需要的 jar 包"></a>需要的 jar 包</h2><ul>
<li>commons-fileupload</li>
<li>commons-io</li>
</ul>
<h2 id="文件上传-API"><a href="#文件上传-API" class="headerlink" title="文件上传 API"></a>文件上传 API</h2><h3 id="DiskFileItemFactor-磁盘文件项工厂类"><a href="#DiskFileItemFactor-磁盘文件项工厂类" class="headerlink" title="DiskFileItemFactor: 磁盘文件项工厂类"></a>DiskFileItemFactor: 磁盘文件项工厂类</h3><p><em>Initial</em></p>
<ul>
<li><code>DiskFileItemFactory()</code></li>
<li><code>DiskFileItemFactory(int sizeThreshold,File repostory)</code><ul>
<li>sizeThreshold: 设置文件上传的缓冲区大小,默认值未 10 KB</li>
<li>respository: 设置文件上传过程中产生临时文件存放的路径</li>
</ul>
</li>
</ul>
<p><em>Method</em></p>
<ul>
<li><code>setSizeThreshold()</code>: 设置缓冲区大小</li>
<li><code>setRepository</code>: 设置临时文件存放的路径</li>
</ul>
<p><em>Usage</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1.创建磁盘文件项工厂</span></span><br><span class="line">DiskFileItemFactory disFileItemFactory = <span class="keyword">new</span> DiskFileItemFactory();</span><br><span class="line"><span class="comment">// 1.1 设置缓冲区的大小</span></span><br><span class="line">diskFileItemFactory.setSizeThreshold(<span class="number">3</span> * <span class="number">1024</span> * <span class="number">1024</span>); <span class="comment">// 设置缓冲区的大小未 3M</span></span><br><span class="line"><span class="comment">// 1.2 设置临时文件存放的路径</span></span><br><span class="line"><span class="comment">// 创建新的临时文件存放的路径的 String 对象</span></span><br><span class="line">String tempPath = getServletContext().getRealPath(<span class="string">"/temp"</span>);</span><br><span class="line">diskFileItemFactory.setRepository(<span class="keyword">new</span> File(tempPath));</span><br></pre></td></tr></table></figure>
<h3 id="ServletFileUpload-核心解析类"><a href="#ServletFileUpload-核心解析类" class="headerlink" title="ServletFileUpload: 核心解析类"></a>ServletFileUpload: 核心解析类</h3><p><em>Initial</em></p>
<ul>
<li><code>ServletFileUpload</code></li>
<li><code>ServletFileUpload(FileItemFactory fileItemFactory)</code></li>
</ul>
<p><em>Method</em></p>
<ul>
<li><p><code>(static boolean) isMultipartContent(HttpServletRequest request)</code>: 判断表单的 enctype 属性是否正确</p>
</li>
<li><p><code>(List) parseRequest(HttpServletRequest request)</code>: 解析 Request 对象,返回一个 List 集合(每个部分的对象 FileItem)</p>
</li>
<li><p><code>setFileSizeMax(long fileSizeMax)</code>: 设置单个文件的大小</p>
</li>
<li><p><code>setSizeMax(long sizeMax)</code>: 设置上传的文件的总大小</p>
</li>
<li><p><code>setHeaderEncoding(String encoding)</code>: 中文文件名上传的乱码的问题</p>
</li>
<li><p><code>setProgressListener(ProgressListener pListener)</code>: 设置监听文件上传的进度</p>
</li>
</ul>
<h3 id="FileItem-文件项"><a href="#FileItem-文件项" class="headerlink" title="FileItem 文件项"></a>FileItem 文件项</h3><p><em>Usage</em></p>
<ul>
<li><p>普通项 </p>
<ul>
<li><p><code>(boolean) isFormField()</code>: 判断表单项是普通项还是文件上传项。如果为 true 代表是普通项</p>
</li>
<li><p><code>(String) getFieldName()</code>: 获得普通项的名称</p>
</li>
<li><p><code>(String) getString()</code></p>
</li>
<li><p><code>(String) getString(String encoding)</code>: 获得普通项的值</p>
</li>
</ul>
</li>
<li><p>文件上传项</p>
<ul>
<li><p><code>(String) getName()</code>: 获取上传的文件的文件名</p>
</li>
<li><p><code>(InputStream) getInputStream()</code>: 获取文件上传的输入流</p>
</li>
<li><p><code>(long) getSize()</code>: 获取上传的文件大小</p>
</li>
<li><p><code>delete()</code>: 删除文件上传过程中的临时文件</p>
</li>
</ul>
</li>
</ul>
<p>代码示例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span> <span class="keyword">throws</span> ServletException, IOException </span>{</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 判断表单的enctype属性:</span></span><br><span class="line"> <span class="keyword">boolean</span> flag = ServletFileUpload.isMultipartContent(request);</span><br><span class="line"> <span class="keyword">if</span> (!flag) {</span><br><span class="line"> <span class="comment">// enctype属性不是multipart/form-data</span></span><br><span class="line"> request.setAttribute(<span class="string">"msg"</span>, <span class="string">"表单的格式不正确!"</span>);</span><br><span class="line"> request.getRequestDispatcher(<span class="string">"/jsp/upload.jsp"</span>).forward(request, response);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 1.创建磁盘文件项工厂</span></span><br><span class="line"> DiskFileItemFactory diskFileItemFactory = <span class="keyword">new</span> DiskFileItemFactory();</span><br><span class="line"> <span class="comment">// 下列按需设置</span></span><br><span class="line"> <span class="comment">// 1.1设置缓冲区的大小:</span></span><br><span class="line"> diskFileItemFactory.setSizeThreshold(<span class="number">3</span> * <span class="number">1024</span> * <span class="number">1024</span>);<span class="comment">// 设置缓冲区的大小为3M</span></span><br><span class="line"> <span class="comment">// 1.2设置临时文件存放的路径:</span></span><br><span class="line"> <span class="comment">// 获得临时文件存放的路径:</span></span><br><span class="line"> String tempPath = getServletContext().getRealPath(<span class="string">"/temp"</span>);</span><br><span class="line"> diskFileItemFactory.setRepository(<span class="keyword">new</span> File(tempPath));</span><br><span class="line"> <span class="comment">// 2.创建一个核心的解析类</span></span><br><span class="line"> ServletFileUpload fileUpload = <span class="keyword">new</span> ServletFileUpload(diskFileItemFactory);</span><br><span class="line"> <span class="comment">// 设置文件上传的文件的大小:</span></span><br><span class="line"> <span class="comment">//fileUpload.setSizeMax(5*1024*1024);</span></span><br><span class="line"> <span class="comment">// 3.利用核心类解析Request,解析后会得到多个部分。返回一个List集合。List集合装的是每个部分的内容(FileItem文件项)。</span></span><br><span class="line"> List<FileItem> list = fileUpload.parseRequest(request);</span><br><span class="line"> <span class="comment">// 4.遍历List集合,会得到代表每个部分的文件项的对象。根据文件项判断是否是文件上传项。</span></span><br><span class="line"> <span class="keyword">for</span> (FileItem fileItem : list) {</span><br><span class="line"> <span class="comment">// 判断这个文件项是否是普通项还是文件上传项。</span></span><br><span class="line"> <span class="keyword">if</span> (fileItem.isFormField()) {</span><br><span class="line"> <span class="comment">// 普通项:</span></span><br><span class="line"> <span class="comment">// 接收普通项的值:(接收值不能再使用request.getParameter())</span></span><br><span class="line"> String name = fileItem.getFieldName();<span class="comment">// 获得普通项的名称</span></span><br><span class="line"> <span class="comment">// 获得普通项的值</span></span><br><span class="line"> String value = fileItem.getString(<span class="string">"UTF-8"</span>);</span><br><span class="line"> System.out.println(name+<span class="string">" "</span>+value);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">// 文件上传项:</span></span><br><span class="line"> <span class="comment">// 获得文件上传项的文件的名称:</span></span><br><span class="line"> String filename = fileItem.getName();</span><br><span class="line"> <span class="comment">// System.out.println("文件名:"+filename);</span></span><br><span class="line"> <span class="keyword">int</span> idx = filename.lastIndexOf(<span class="string">"\\"</span>);</span><br><span class="line"> <span class="keyword">if</span> (idx != -<span class="number">1</span>) {</span><br><span class="line"> <span class="comment">// 使用老版本浏览器:</span></span><br><span class="line"> filename = filename.substring(idx+<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 得到唯一文件名:</span></span><br><span class="line"> String uuidFilename = UploadUtils.getUuidFilename(filename);</span><br><span class="line"> <span class="comment">// 获得文件上传的文件的数据:</span></span><br><span class="line"> InputStream is = fileItem.getInputStream();</span><br><span class="line"> <span class="comment">// 获得文件上传的路径:磁盘绝对路径。</span></span><br><span class="line"> String realPath = getServletContext().getRealPath(<span class="string">"/upload"</span>);</span><br><span class="line"> <span class="comment">// 进行目录分离:</span></span><br><span class="line"> String path = UploadUtils.getRealPath(uuidFilename);</span><br><span class="line"> String newPath = realPath + path;</span><br><span class="line"> File file = <span class="keyword">new</span> File(newPath);</span><br><span class="line"> <span class="keyword">if</span> (!file.exists()) {</span><br><span class="line"> file.mkdirs();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 创建一个输出流,写入到设置的路径中。</span></span><br><span class="line"> OutputStream os = <span class="keyword">new</span> FileOutputStream(newPath+<span class="string">"/"</span>+uuidFilename);</span><br><span class="line"> <span class="comment">// 两个流对接:</span></span><br><span class="line"> <span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">byte</span>[] b = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line"> <span class="keyword">while</span> ((len = is.read(b)) != -<span class="number">1</span>) {</span><br><span class="line"> os.write(b, <span class="number">0</span>, len);</span><br><span class="line"> }</span><br><span class="line"> is.close();</span><br><span class="line"> os.close();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 删除临时文件</span></span><br><span class="line"> fileItem.delete();</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="附"><a href="#附" class="headerlink" title="附"></a>附</h2><h3 id="文件上传兼容浏览器问题"><a href="#文件上传兼容浏览器问题" class="headerlink" title="文件上传兼容浏览器问题"></a>文件上传兼容浏览器问题</h3><p>如果使用 IE 老版本的浏览器会出现一个文件名称获取时带有路径的情况:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//正确的文件名:aa.txt</span></span><br><span class="line"><span class="comment">//获取到错误的文件名:C:\Users\cato\Desktop\aa.txt</span></span><br></pre></td></tr></table></figure>
<p>解决:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> idx = fileName.lastIndexOf(<span class="string">"\\"</span>);</span><br><span class="line"><span class="keyword">if</span> (idx != -<span class="number">1</span>) {</span><br><span class="line"> <span class="comment">//使用老版本 IE 浏览器:</span></span><br><span class="line"> fileName = fileName.substring(idx + <span class="number">1</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="文件上传同一个目录下文件同名问题与同一目录下存放文件过多的问题"><a href="#文件上传同一个目录下文件同名问题与同一目录下存放文件过多的问题" class="headerlink" title="文件上传同一个目录下文件同名问题与同一目录下存放文件过多的问题"></a>文件上传同一个目录下文件同名问题与同一目录下存放文件过多的问题</h3><ul>
<li>在同一个目录下同名文件会出现后上传的覆盖掉先上传的文件,使用唯一文件名解决:</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过 UUID 获取随机的唯一标识</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getUniqueFileName</span><span class="params">(String fileName)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> idx = fileName.lastIndexOf(<span class="string">"."</span>);</span><br><span class="line"> String extetions = fileName.substring(idx);</span><br><span class="line"> <span class="keyword">return</span> UUID.randomUUID().toString().replace(<span class="string">"-"</span>, <span class="string">""</span>) + extetions;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li><p>同一目录下存放文件过多,打开时卡顿且不便管理<br> <em>解决方法:</em></p>
<ul>
<li>按时间分离:月、周、天、毫秒等</li>
<li>按用户分离:user id</li>
<li>按个数分离:一个目录放3000个文件</li>
<li>按目录分离算法:以某种特定算法分离</li>
</ul>
<p> <em>目录分离算法简述:</em></p>
<ul>
<li>创建一个唯一文件名</li>
<li>获取上面文件名的 hashCode 值:int 类型(32 位)</li>
<li>让 hashCode 的值 & 0xf:得出来的值作为一级目录的名字</li>
<li>让hashCode 右移 4 位( >>> 4 )</li>
<li>让 hashCode 的值 & 0xf:得出来的值作为二级目录的名字</li>
<li>…(以此类推到想要的层级数)</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getPath</span><span class="params">(String uniqueFileName, <span class="keyword">int</span> count)</span> </span>{</span><br><span class="line"> StringBuilder stringBuilder = <span class="keyword">new</span> StringBuilder();</span><br><span class="line"> <span class="keyword">int</span> code = uniqueFileName.hashCode();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < count; i++) {</span><br><span class="line"> <span class="keyword">int</span> directoryName = code & <span class="number">0xf</span>;</span><br><span class="line"> stringBuilder += (<span class="string">"/"</span> + directoryName);</span><br><span class="line"> code = code >>> <span class="number">4</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/cato.github.io/2018/03/21/ImagePickerHelper/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Cato">
<meta itemprop="description" content>
<meta itemprop="image" content="/cato.github.io/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="当然我在瞎扯">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/cato.github.io/2018/03/21/ImagePickerHelper/" itemprop="url">调用 iOS 系统相机/相册的流程组件:ImagePickerHelper</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-21T19:52:04+08:00">
2018-03-21
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/cato.github.io/2018/03/21/ImagePickerHelper/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2018/03/21/ImagePickerHelper/" itemprop="commentCount"></span>
</a>
</span>
<span id="/cato.github.io/2018/03/21/ImagePickerHelper/" class="leancloud_visitors" data-flag-title="调用 iOS 系统相机/相册的流程组件:ImagePickerHelper">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="ImagePickerHelper"><a href="#ImagePickerHelper" class="headerlink" title="ImagePickerHelper"></a>ImagePickerHelper</h2><p>一个封装了获取 iOS 系统相机/相册的流程组件,内置权限验证,避免未验证权限导致 Crash,刚好也是学习如何制作自己的 Cocoapods 库,附上传成功后的截图😋:</p>
<p><img src="https://github.com/Cato-chuk/cato.github.io/blob/master/images/searchicon.png" alt="screenshot01"></p>
<h3 id="项目地址"><a href="#项目地址" class="headerlink" title="项目地址"></a>项目地址</h3><p><a href="https://github.com/Cato-chuk/ImagePickerHelper" target="_blank" rel="noopener">https://github.com/Cato-chuk/ImagePickerHelper</a></p>
<h3 id="Usage"><a href="#Usage" class="headerlink" title="Usage"></a>Usage</h3><ul>
<li>在项目的 Profile 文件添加 <code>pod ImagePickerHelper</code></li>
<li>Terminal.app 进入项目根路径执行 <code>pod install</code></li>
<li>在需要调用系统相册/相机的 Controller 里 <code>import ImagePickerHelper</code></li>
<li>使用 <code>ImagePickerHelper.sharedInstance</code> 单例对象来调用方法</li>
</ul>
<h3 id="Method"><a href="#Method" class="headerlink" title="Method"></a>Method</h3><p>定义 Closure 别名</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">typealias</span> <span class="type">VoidBlock</span> = () -> <span class="type">Void</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">typealias</span> <span class="type">ImageBlock</span> = (<span class="number">_</span> image: <span class="type">UIImage</span>) -> <span class="type">Void</span></span><br></pre></td></tr></table></figure>
<ul>
<li>使用 ActionSheet(从底部升上) 显示 相机/照片 选项</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/// - Parameters:</span></span><br><span class="line"><span class="comment">/// - title: ActionSheet 显示的标题</span></span><br><span class="line"><span class="comment">/// - message: ActionSheet 显示的信息</span></span><br><span class="line"><span class="comment">/// - controller: 当前需要显示 ActionSheet 的 Controller</span></span><br><span class="line"><span class="comment">/// - completion: 选择照片完成后执行的 Closure</span></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">func</span> <span class="title">showObtainPhotoOptionSheet</span><span class="params">(withTipsTitle title: String, message: String, controller: UIViewController, completion: @escaping ImageBlock)</span></span></span><br></pre></td></tr></table></figure>
<ul>
<li>直接打开系统的相册选择器</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/// - Parameters:</span></span><br><span class="line"><span class="comment">/// - controller: 负责 present UIImagePickerController(.photoLibrary) 的 Controller</span></span><br><span class="line"><span class="comment">/// - completion: 选择照片完成后执行的 Closure</span></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">func</span> <span class="title">showPhotoLibrary</span><span class="params">(byController controller: UIViewController, completion: @escaping ImageBlock)</span></span></span><br></pre></td></tr></table></figure>
<ul>
<li>直接打开相机</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/// - Parameters:</span></span><br><span class="line"><span class="comment">/// - controller: 负责 present UIImagePickerController(.camera) 的 Controller</span></span><br><span class="line"><span class="comment">/// - completion: 选择照片完成后执行的 Closure</span></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">func</span> <span class="title">showCamera</span><span class="params">(byController controller: UIViewController, completion: @escaping ImageBlock)</span></span></span><br></pre></td></tr></table></figure>
<ul>
<li><p>系统相册权限验证</p>
<ul>
<li>参数 <code>callSystemImagePicker</code> 默认为 true(调用系统相册选择器),若设为 false 则需要在 authorizedBlock 里提供自定义相册选择器的嗲用</li>
<li>验证结果:notDetermined:将会请求相册权限</li>
<li>验证结果:authorized:直接打开相册</li>
<li>验证结果:其他(即未获得权限):未默认行为,但会执行 deniedBlock,可通过传入 deniedBlock 做想要的操作</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/// - Parameters:</span></span><br><span class="line"><span class="comment">/// - callSystemImagePicker: 是否使用系统的相册图库来选择图片</span></span><br><span class="line"><span class="comment">/// - authorizedBlock: 权限验证成功后执行的 Closure</span></span><br><span class="line"><span class="comment">/// - deniedBlock: 权限被拒绝后执行的 Closure</span></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">func</span> <span class="title">photoAlbumPermissions</span><span class="params">(callSystemImagePicker: Bool = <span class="literal">true</span>, authorizedBlock: @escaping VoidBlock = {}, deniedBlock: @escaping VoidBlock = {})</span></span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
</section>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
</div>
</div>
<aside id="sidebar" class="sidebar">
<div class="sidebar-inner">
<section class="site-overview-wrap sidebar-panel sidebar-panel-active">
<div class="site-overview">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" src="/cato.github.io/images/avatar.jpg" alt="Cato">
<p class="site-author-name" itemprop="name">Cato</p>
<p class="site-description motion-element" itemprop="description"></p>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/cato.github.io/archives/">
<span class="site-state-item-count">3</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<a href="/cato.github.io/tags/index.html">
<span class="site-state-item-count">5</span>
<span class="site-state-item-name">标签</span>
</a>
</div>
</nav>
<div class="links-of-author motion-element">
<span class="links-of-author-item">
<a href="https://github.com/Cato-chuk" target="_blank" title="GitHub">
<i class="fa fa-fw fa-github"></i>GitHub</a>
</span>
<span class="links-of-author-item">
<a href="mailto:cato.chuk@gmail.com" target="_blank" title="E-Mail">
<i class="fa fa-fw fa-envelope"></i>E-Mail</a>
</span>
</div>
</div>
</section>
</div>
</aside>
</div>
</main>
<footer id="footer" class="footer">
<div class="footer-inner">
<div class="copyright">© <span itemprop="copyrightYear">2019</span>
<span class="with-love">
<i class="fa fa-user"></i>
</span>
<span class="author" itemprop="copyrightHolder">Cato</span>
</div>
<div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>
<span class="post-meta-divider">|</span>
<div class="theme-info">主题 — <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>
</div>
</footer>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span id="scrollpercent"><span>0</span>%</span>
</div>
</div>
<script type="text/javascript">
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
</script>