方法一:使用jsp实现:1 A* W7 O* y& i
- <span style="font-family:FangSong_GB2312;font-size:18px;"><%@ page contentType="text/html;charset=gb2312" %> / i: `0 z) J, k! t* p2 @- Q
- <%@page import="com.jspsmart.upload.SmartUpload"%>
2 _' w t% L( t8 V5 h! x - <%@page import="com.jspsmart.upload.File"%> 1 B( t( t c( ~8 ]
- <% - Q9 S' B6 ], d
- //新建一个SmartUpload对象 * `5 i S/ ?. p0 n( U' \1 B
- SmartUpload su = new SmartUpload(); . S4 q( u0 I \. e' b
- //初始化 / m# N( I2 A3 p4 u* I+ A
- su.initialize(pageContext);
9 d8 z2 {" N+ L - //设定contentDisposition为null以禁止浏览器自动打开文件,保证单击链接后是下载文件。若不设定,则下载的文件扩展名为doc时,浏览器将自动用word打开它。扩展名为pdf时,浏览器将用acrobat打开。
1 _' q6 i' |5 L* B - su.setContentDisposition(null); 4 U( K: t! P1 v. _, c8 G
- //下载文件
% R! ]. U$ I& k! ^- T }: ^ - su.downloadFile("WEB-INF/upload/11.rar"); L, V8 b9 t8 H Q2 Q
- //处理输出流问题
5 a, y; j0 i+ P0 [- g - out.clear();
% ^ s3 l: W$ W/ a3 W# c - out = pageContext.pushBody(); 5 ~ K1 \' r8 h5 t
- %> 5 {2 g, E# D; K$ M/ Q9 r2 g4 t
- </span>
复制代码 方法二:使用servlet 实现 :
" e7 ]. ~7 K+ `- package downloadFile; / k0 d4 ?: {) K
- 1 ]0 \ z6 j6 w1 Z7 W, A9 i9 D
- import java.io.BufferedInputStream;
: m" ?) R. f% c% _: C" B - import java.io.BufferedOutputStream;
& g( K6 W, _ A, f" I/ V - import java.io.File; " W$ W/ G! f" l
- import java.io.FileInputStream; * B& r2 ]. {8 Y) D7 n; Q
- import java.io.IOException;
4 [" I. V5 B5 V/ v' z - import javax.servlet.ServletException; & i" Y8 S! n. P8 w/ c
- import javax.servlet.ServletOutputStream;
! t/ S+ m2 b6 k+ Y3 g9 B7 I4 p2 s - import javax.servlet.http.HttpServlet; 0 j+ N9 D! O& k# g* ^1 b* H( g g
- import javax.servlet.http.HttpServletRequest;
3 U9 G4 M* L }( I; ?; R# t/ A - import javax.servlet.http.HttpServletResponse;
" c9 `, J3 M3 T% H+ p: k' f' m5 ]+ F -
& j% `) c" K% i3 a8 `9 Q; o* A - 8 f# n$ e, W y$ f# y; e( T
- % y7 T3 a% ?" U
- public class DownloadFile extends HttpServlet { * J" { s5 l9 E6 U( ^; R: n% D
- protected void service(HttpServletRequest req, HttpServletResponse res)
6 O- f% l" v% M4 } c% C5 w - throws ServletException, IOException { # S$ {% t2 q) O( [* f0 n% ?9 E
- $ a$ W9 U) @) X. B. ]2 a' |
- // 服务器相对路径 ; j; s4 q" o" g
- String path = req.getParameter("path"); 3 J4 H) r& ]- e
- // 服务器绝对路径
! m# o# N' K* c# E. X; t$ o3 @ - path = getServletContext().getRealPath("/") + path;
0 c, N; P( Q E: ?3 t$ G+ ^3 M - 3 p+ O0 K M1 X
- // 检查文件是否存在 ( G# c+ }0 x6 m) [6 u6 C8 m' H
- File obj = new File(path);
! N9 d( _" K; \ }; S: F+ l7 j - if (!obj.exists()) { 4 I1 _) y$ w% C J' A8 q
- res.setContentType("text/html;charset=GBK"); ; N# }) c) s# g0 D7 H* h. Y
- res.getWriter().print("指定文件不存在!"); $ m" B: t! O4 f: g
- return;
# Z! Y/ P c( C6 H2 v$ \ - } 3 d. A6 ]6 d1 y S4 m/ e
- 7 f3 _9 l& a9 D9 d, s: J E
- // 读取文件名:用于设置客户端保存时指定默认文件名 1 e3 Y6 g9 {8 B) X
- int index = path.lastIndexOf("\"); // 前提:传入的path字符串以“\”表示目录分隔符 7 T* q9 y6 E& n; H( `& n8 S V
- String fileName = path.substring(index + 1); 0 `' n5 s$ A8 e4 F8 |5 U
-
+ R0 h9 c$ y# k9 k* I+ } - // 写流文件到前端浏览器 3 ]; [! _9 I7 e0 N) d
- ServletOutputStream out = res.getOutputStream(); 9 W4 I. O+ b( e
- res.setHeader("Content-disposition", "attachment;filename=" + fileName);
+ n$ [' z% j# N4 G - BufferedInputStream bis = null; $ M9 w% V7 |& d
- BufferedOutputStream bos = null; $ v' U) L# u: X2 f0 X
- try {
. h* n/ ^$ M3 H2 E# \1 y& r - bis = new BufferedInputStream(new FileInputStream(path));
3 u/ L5 ~9 l3 _7 g& T: ^, ], @ d - bos = new BufferedOutputStream(out);
& s5 J- ~0 \% r$ k - byte[] buff = new byte[2048];
3 @+ Q2 A2 E/ C - int bytesRead; ! _, l3 L {" W- \" ^7 e6 g
- while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
. c' m: c; s7 q' a8 p. q* _ - bos.write(buff, 0, bytesRead); 2 s" o, v9 D9 P0 U! R' q1 Q2 e
- }
. {& v8 H' c) c8 a - } catch (IOException e) {
9 l5 {4 q6 m" L' Y, m0 L/ i- R9 n - throw e;
; @8 p$ Z, S( I! A4 ~' O& K, Z8 { - } finally { 2 y# ^: i6 ^" g: B R
- if (bis != null) * _" i& P: }5 I
- bis.close();
& x$ ?* V$ V0 ?- C2 C - if (bos != null) 2 x& p- L3 L5 m# h" q
- bos.close();
1 u2 B* r1 x5 D. ? - }
* N5 k1 T, p4 v - }
7 h( B; Z6 w" ?: h -
3 |- ?0 t6 e+ J7 ~/ u9 b5 c - }
复制代码 web.xml 设置: . \% y9 ^7 b; h8 [( N
- <servlet> l2 f( s9 l0 f) }8 m
- <servlet-name>DownloadFile</servlet-name>
: \( e2 y! M! Z; z! X$ O V! W - <servlet-class>downloadFile.DownloadFile</servlet-class>
6 l. V& {$ }5 Q1 g9 X8 W; I - </servlet>
8 k$ E* N7 O' c) r/ ] - <servlet-mapping> 8 _! U: d& o6 K1 a1 A7 W! C
- <servlet-name>DownloadFile</servlet-name> % D1 |+ b; d+ L# W( K5 P
- <url-pattern>/DownloadFile</url-pattern> & ?7 H" I9 o8 I0 S$ L8 ]
- </servlet-mapping> % [& x5 h( d$ J) W
- </web-app>
复制代码 6 a A+ }1 j/ V, [; y G* }7 H7 R) J
如果此Servlet命名为downloadFile,请求的URL为:http://localhost:8888/upload/DownloadFile?path=web.txt % ]3 { r( M, \0 j+ v. \ T
总结: 第一种方法我使用的jsp文件处理,并且使用第三方控件,简单的几行代码就搞定了,第二种方法是使用java语言写的,而且下载的文件是中文命名的话,还会出现乱码的问题.从此可以看出,站在巨人的肩膀上的重要性.
( T6 M }8 `: I. i
* E. L% @/ e* X |