服务器端实现文件下载功能代码
2023-09-14 09:00:23 时间
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
* Servlet implementation class Download
@WebServlet("/Download")
public class Download extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Download() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String name = "aaa.*";// 文件名
String uploadPath = UploadFileHelper.getRepositoryPath() + "//";// 文件来源
String filePath = name;
String fileName = name;
//处理文件名包含中文,浏览器乱码
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") 0) {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// firefox浏览器
} else {
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") 0) {
fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
}
}
response.setContentType("text/plain");
response.setHeader("Location", fileName);
response.reset();
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
filePath = uploadPath + filePath;
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.close();
bis.close();
fos.close();
fis.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
只需百来行代码,为你的Web页面增加本地文件操作能力,确定不试试吗? 笔者开源了一个`Web`思维导图[mind-map](https://github.com/wanglin2/mind-map),数据默认是存储在`localstorage`里,如果想保存到本地文件,需要使用导出功能,下次打开再使用导入功能,编辑完如果又想保存到文件,那么又需要从重新导出覆盖原来的文件,不得不说,可以但不优雅,所以最近增加了直接编辑本地文件的能力,体验了一下,还是不错的,并且就是调调`API`的事情,很简单,何乐而不为。
我学会了,写一个前端下载文件功能 过去有很多次文件下载的功能,但是都没有记录下来,这次有空就把文件下载的功能从0写一遍,于是就有了这篇文章。 我会从简到难的方式去实现下载功能。从直接下载字符串到简单请求下载文件,最终通过后端返回的文件名来实现动态下载文件。
使用Fiddle对浏览器从服务器端接收到的SAP UI5库文件源代码进行修改 Sometimes for trouble shooting or research purpose, you would like to make small changes on framework js file, and test how your application would react to those changes. For example I would like to add a new line for debugging purpose before line 70.
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1694479
只需百来行代码,为你的Web页面增加本地文件操作能力,确定不试试吗? 笔者开源了一个`Web`思维导图[mind-map](https://github.com/wanglin2/mind-map),数据默认是存储在`localstorage`里,如果想保存到本地文件,需要使用导出功能,下次打开再使用导入功能,编辑完如果又想保存到文件,那么又需要从重新导出覆盖原来的文件,不得不说,可以但不优雅,所以最近增加了直接编辑本地文件的能力,体验了一下,还是不错的,并且就是调调`API`的事情,很简单,何乐而不为。
我学会了,写一个前端下载文件功能 过去有很多次文件下载的功能,但是都没有记录下来,这次有空就把文件下载的功能从0写一遍,于是就有了这篇文章。 我会从简到难的方式去实现下载功能。从直接下载字符串到简单请求下载文件,最终通过后端返回的文件名来实现动态下载文件。
使用Fiddle对浏览器从服务器端接收到的SAP UI5库文件源代码进行修改 Sometimes for trouble shooting or research purpose, you would like to make small changes on framework js file, and test how your application would react to those changes. For example I would like to add a new line for debugging purpose before line 70.
相关文章
- JAVA代码实现FTP文件下载
- java下载文件_java下载文件到本地[通俗易懂]
- php实现文件下载的方法
- javaWEB文件上传与下载详解编程语言
- 断点下载文件Java代码详解编程语言
- Java从网上下载文件的代码详解编程语言
- 如何从TFTP服务器下载文件?
- ASP.NET 大文件下载的实现思路及代码
- 下载MySQL文件遭遇损坏问题如何解决(mysql下载文件被损坏)
- 如何选择适合自己的 MySQL 下载文件(mysql 下载哪个文件)
- IEphp关于强制下载文件的代码
- 关于php支持分块与断点续传文件下载功能代码
- href下载文件根据id取url并下载
- iis不能下载包含中文文件名的rar文件
- 上传文件返回的json数据会被提示下载问题解决方案