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();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
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;
}
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();
}
}
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
// 下载网络文件
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//支持在线打开文件的一种方式
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(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
分享到:
相关推荐
NULL 博文链接:https://lspgdut.iteye.com/blog/1902216
java下载
java远程图片下载,需要先获得远程图片的路径,再通过输入输出流进行读写
用ajax实现的java文件打包下载。可以输入扩展名选择文件下载。
In addition to excellent content, this book includes licenses to two Java web components from BrainySoftware.com. You receive a full license of the Programmable File Download component for commercial ...
3.Drag-and-drop your desired class file on the DJ Java Decompiler window. DJ Java Decompiler supports drag-and-drop functions for OLE. 4.In Windows Explorer Right mouse-button pop-up menu available...
Java Language Conversion Assistant is a tool that automatically converts existing Java-language code into Visual C#® Quick Info File Name:VS7.1-KB819018-X86.exe Download Size:10092 KB Date Published:...
String destFilePath = path + "\\download\\" + filename; Map beanParams = new HashMap(); beanParams.put("results", datas); XLSTransformer transfer = new XLSTransformer(); transfer....
23、java流(Stream)、文件(File)和IO 24、java异常处理 25、java数据结构 26、java集合框架 27、java泛型 28、java序列化 29、java网络编程 30、java多线程 31、java8新特性 附加: linux命令 数据结构与算法分析 SQL...
websocket java 实现例子 tomcat 8 jdk 1.8
使用jsp工具,tomcat部署服务,完成一个文件上传和下载的B/S系统
前提条件: 定义src / main / resources / application.properties中提到的所有属性在src / main / resources / log4j.properties中为属性“ log4j.appender.file.File”定义日志文件路径该应用程序将在优化的空间...
Demo Netty Java界面客户端实践实现 首先了解一下客户端主要使用Netty 服务端主要使用ServerSocket java 客户端界面
Need to download all. covered: The Java programming language for people with no Java experience, and even people with no programming experience at all. Key concepts read like a list of Java features:...
Need to download all. covered: The Java programming language for people with no Java experience, and even people with no programming experience at all. Key concepts read like a list of Java features:...
今天小编就为大家分享一篇关于IntelliJ IDEA引入第三方jar包或查看Java源码的时候报decompiled.class file bytecode version:52.0(java 8)错误的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考...
Need to download all. covered: The Java programming language for people with no Java experience, and even people with no programming experience at all. Key concepts read like a list of Java features:...
解决Could not load font file: C:\Windows\FONTS\mstmc.ttf 对应问题贴https://blog.csdn.net/guozhangjie1992/article/details/103679640
Need to download all. covered: The Java programming language for people with no Java experience, and even people with no programming experience at all. Key concepts read like a list of Java features:...
library protobuf-java, which you can download manually from the official Maven repository and add it to the CLASSPATH, or use Maven's automatic dependency resolution features by adding a dependency...