1.以流的方式下载.
public HttpServletResponse download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径。 File file = new File(path); // 取得文件名。 String filename = file.getName(); // 取得文件的后缀名。 String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();</span><span style="color: #008000;">//</span><span style="color: #008000;"> 以流的形式下载文件。</span> InputStream fis = <span style="color: #0000ff;">new</span> BufferedInputStream(<span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(path)); </span><span style="color: #0000ff;">byte</span>[] buffer = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span><span style="color: #000000;">[fis.available()]; fis.read(buffer); fis.close(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 清空response</span>
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
2.下载本地文件
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException { // 下载本地文件 String fileName = "Operator.doc".toString(); // 文件的默认保存名 // 读到流中 InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径 // 设置输出的格式 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len); inStream.close(); } catch (IOException e) { e.printStackTrace(); } }
3.下载网络文件
public void downloadNet(HttpServletResponse response) throws MalformedURLException { // 下载网络文件 int bytesum = 0; int byteread = 0;URL url </span>= <span style="color: #0000ff;">new</span> URL("windine.blogdriver.com/logo.gif"<span style="color: #000000;">); </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { URLConnection conn </span>=<span style="color: #000000;"> url.openConnection(); InputStream inStream </span>=<span style="color: #000000;"> conn.getInputStream(); FileOutputStream fs </span>= <span style="color: #0000ff;">new</span> FileOutputStream("c:/abc.gif"<span style="color: #000000;">); </span><span style="color: #0000ff;">byte</span>[] buffer = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1204<span style="color: #000000;">]; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> length; </span><span style="color: #0000ff;">while</span> ((byteread = inStream.read(buffer)) != -1<span style="color: #000000;">) { bytesum </span>+=<span style="color: #000000;"> byteread; System.out.println(bytesum); fs.write(buffer, </span>0<span style="color: #000000;">, byteread); } } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (FileNotFoundException e) { e.printStackTrace(); } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) { e.printStackTrace(); } }</span></pre>
4.支持在线打开的方式
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception { File f = new File(filePath); if (!f.exists()) { response.sendError(404, "File not found!"); return; } BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); byte[] buf = new byte[1024]; int len = 0;response.reset(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 非常重要</span> <span style="color: #0000ff;">if</span> (isOnLine) { <span style="color: #008000;">//</span><span style="color: #008000;"> 在线打开方式</span> URL u = <span style="color: #0000ff;">new</span> URL("file:///" +<span style="color: #000000;"> filePath); response.setContentType(u.openConnection().getContentType()); response.setHeader(</span>"Content-Disposition", "inline; filename=" +<span style="color: #000000;"> f.getName()); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 文件名应该编码成UTF-8</span> } <span style="color: #0000ff;">else</span> { <span style="color: #008000;">//</span><span style="color: #008000;"> 纯下载方式</span> response.setContentType("application/x-msdownload"<span style="color: #000000;">); response.setHeader(</span>"Content-Disposition", "attachment; filename=" +<span style="color: #000000;"> f.getName()); } OutputStream out </span>=<span style="color: #000000;"> response.getOutputStream(); </span><span style="color: #0000ff;">while</span> ((len = br.read(buf)) > 0<span style="color: #000000;">) out.write(buf, </span>0<span style="color: #000000;">, len); br.close(); out.close(); }</span></pre>