导出pdf之--下载到本地
2017-10-12 14:49
274 查看
需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片
两种实现方法:
在HTML网页中,通过超链接链接到要下载的文件的地址
[html] view
plain copy
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
</body>
</html>
其中day06/download是文档路径,本实例的程序结构如下:
程序运行后,可以通过单击需要下载文档实现下载
但是这里会出现一个问题,就是单击下载压缩包的时候会弹出下载页面,但是下载图片的时候浏览器就直接打开了图片,没有下载。
这是因为通过超链接下载文件时,如果浏览器可以识别该文件格式,浏览器就会直接打开。只有浏览器不能识别该文件格式的时候,才会实现下载。因此利用第二种方法实现下载功能。
通过Servlet下载文件的原理是通过servlet读取目标程序,将资源返回客户端。
[html] view
plain copy
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
<h1>通过servlet程序下载文件</h1>
<a href="/day06/ServletDownload?filename=cors.zip">压缩包</a>
<a href="/day06/ServletDownload?filename=1.png">图片</a>
</body>
</html>
其中,/day06/ServletDownload 是servlet程序的映射路径
然后新建一个servlet,名称为ServletDownload,URL映射为/ServletDownload
添加代码如下:
[java] view
plain copy
package com.lsgjzhuwei.servlet.response;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDownload
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class ServletDownload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletDownload() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获得请求文件名
String filename = request.getParameter("filename");
System.out.println(filename);
//设置文件MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + filename);
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
重启tomcat服务器,即可实现对压缩包和对图片的下载。
两种实现方法:
一:通过超链接实现下载
在HTML网页中,通过超链接链接到要下载的文件的地址[html] view
plain copy
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
</body>
</html>
其中day06/download是文档路径,本实例的程序结构如下:
程序运行后,可以通过单击需要下载文档实现下载
但是这里会出现一个问题,就是单击下载压缩包的时候会弹出下载页面,但是下载图片的时候浏览器就直接打开了图片,没有下载。
这是因为通过超链接下载文件时,如果浏览器可以识别该文件格式,浏览器就会直接打开。只有浏览器不能识别该文件格式的时候,才会实现下载。因此利用第二种方法实现下载功能。
二:通过Servlet程序实现下载
通过Servlet下载文件的原理是通过servlet读取目标程序,将资源返回客户端。[html] view
plain copy
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
<h1>通过servlet程序下载文件</h1>
<a href="/day06/ServletDownload?filename=cors.zip">压缩包</a>
<a href="/day06/ServletDownload?filename=1.png">图片</a>
</body>
</html>
其中,/day06/ServletDownload 是servlet程序的映射路径
然后新建一个servlet,名称为ServletDownload,URL映射为/ServletDownload
添加代码如下:
[java] view
plain copy
package com.lsgjzhuwei.servlet.response;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDownload
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class ServletDownload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletDownload() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获得请求文件名
String filename = request.getParameter("filename");
System.out.println(filename);
//设置文件MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + filename);
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
重启tomcat服务器,即可实现对压缩包和对图片的下载。
JAVA 实现通过URL下载文件到本地库
1 /** 2 * TODO 下载文件到本地 3 * @author nadim 4 * @date Sep 11, 2015 11:45:31 AM 5 * @param fileUrl 远程地址 6 * @param fileLocal 本地路径 7 * @throws Exception 8 */ 9 public void downloadFile(String fileUrl,String fileLocal) throws Exception { 10 URL url = new URL(fileUrl); 11 HttpURLConnection urlCon = (HttpURLConnection) url.openConnection(); 12 urlCon.setConnectTimeout(6000); 13 urlCon.setReadTimeout(6000); 14 int code = urlCon.getResponseCode(); 15 if (code != HttpURLConnection.HTTP_OK) { 16 throw new Exception("文件读取失败"); 17 } 18 19 //读文件流 20 DataInputStream in = new DataInputStream(urlCon.getInputStream()); 21 DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal)); 22 byte[] buffer = new byte[2048]; 23 int count = 0; 24 while ((count = in.read(buffer)) > 0) { 25 out.write(buffer, 0, count); 26 } 27 out.close(); 28 in.close(); 29 }
相关文章推荐
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之一——使用servlet
- PDF 导出地址 浏览器选择下载地址
- java iText 的jar导出pdf格式,禁止浏览器预览,直接下载功能
- 创建PDF模板,java添加内容、导出下载PDF
- JavaEE_POI导出Excel (网络下载) +(本地导出) 工具类的编写
- 创建PDF模板,java添加内容、导出下载PDF
- Java 批量文件压缩导出,并下载到本地
- iText导出pdf,poi导出excel并下载到客户端
- js实现html下载到本地并生成pdf文件
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之二——使用Action
- 导出excel表格到本地服务器, 远程下载 Java
- HttpWebRequest 请求PDF文件,并下载到本地
- 用apache做为代理下载本地pdf文件
- .net MVC 数据库数据导出到Excel表格并下载到本地
- 保存pdf文件至数据库,在线预览并下载到本地
- PHP使用外部命令导出数据库,备份到服务器并下载到本地
- Java实现数据库数据导出到Excel中并下载到浏览器本地
- 创建PDF模板,java添加内容、导出下载PDF
- Spring Boot 菜鸟教程 18 itext导出pdf下载
- 创建PDF模板,java添加内容、导出下载PDF