从服务器下载文件 DownloadServlet()
.html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> <h1>
使用<a>标签的方式实现文件下载
</h1>
<a href="/WEB14/download/a.txt">a.txt</a>
<br>
<a href="/WEB14/download/a.jpg">a.jpg</a> <h1>使用服务器端编码的方式实现文件下载</h1>
<a href="/WEB14/downloadServlet?filename=a.txt">a.txt</a>
<br>
<a href="/WEB14/downloadServlet?filename=a.jpg">a.jpg</a>
<br>
<a href="/WEB14/downloadServlet2?filename=你的名字.txt">你的名字</a> </body>
</html>
1.DownloadServlet()代码
package com.hdh.content; import java.io.FileInputStream;
import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //1.获取下载文件的名称
String filename=request.getParameter("filename");
//*告诉客户端这个文件不是解析 而是以附件的形式下载
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//2.获取文件的绝对路径
String path=this.getServletContext().getRealPath("download/"+filename);
//3.获得该文件的输入流
FileInputStream in=new FileInputStream(path);
//获得输出流---通过response获得的输出流 用于向客户端写内容
ServletOutputStream out=response.getOutputStream();
//拷贝文件
int len=0;
byte [] buffer=new byte[1024]; while((len=in.read(buffer))>0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
2.在服务器上运行后出现图片无法下载而是直接解析:
需要不进行解析而是以附件的形式下载:告诉客户端这个文件不是解析 而是以附件的形式下载
加上:response.setHeader("Content-Disposition", "attachment;filename="+filename);
package com.hdh.content; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DownloadServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 解决下载 你的名字.txt时乱码的问题
*/
//1.获取下载文件的名称
String filename=request.getParameter("filename"); filename=new String(filename.getBytes("ISO8859-1"),"UTF-8");
//*告诉客户端这个文件不是解析 而是以附件的形式下载
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//2.获取文件的绝对路径
String path=this.getServletContext().getRealPath("download/"+filename);
//3.获得该文件的输入流
InputStream in=new FileInputStream(path);
//获得输出流---通过response获得的输出流 用于向客户端写内容
ServletOutputStream out=response.getOutputStream();
//拷贝文件
int len=0;
byte [] buffer=new byte[1024]; while((len=in.read(buffer))>0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
3.如果在获取中文文件名时,文件名时乱码问题
filename=new String(filename.getBytes("ISO8859-1"),"UTF-8");
从服务器下载文件 DownloadServlet()的更多相关文章
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- (4)FTP服务器下载文件
上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...
- Python 实现批量从不同的Linux服务器下载文件
基于Python实现批量从不同的Linux服务器下载文件 by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/file_for_downl ...
- 从Linux服务器下载文件到本地命令
从Linux服务器下载文件夹到本地1.使用scp命令 scp /home/work/source.txt work@192.168.0.10:/home/work/ #把本地的source.txt文件 ...
- 从Linux服务器下载文件夹到本地
从Linux服务器下载文件夹到本地 1.使用scp命令 scp /home/work/source.txt work@192.168.0.10:/home/work/ #把本地的source.txt文 ...
- Java Web实现使用浏览器从服务器下载文件(后台)
Java Web实现 使用浏览器从服务器下载文件. 下面实现两种情况的下载,需求如下: 需求(一):1.用户在页面填写表单. 2.填写完成后,选择下载,将表单内容发往后台. 3.后台根据内容生产一个文 ...
- Java 从服务器下载文件到本地(页面、后台、配置都有)
先来看实现效果: 有一个链接如下: 点击链接下载文件: 第一种方法:Servlet实现 一.HTML页面部分: 1.HTML页面中的一个链接 <a id="downloadTempl ...
- js从服务器下载文件
通常,将文件绝对路径url作为超链接<a>的链接地址href的值,点击<a>后,浏览器将会尝试请求文件资源,如果浏览器能够辨认文件类型,则将会以预设的打开方式直接打开下载的文件 ...
- C#从服务器下载文件到客户端源码
1.在window窗体加个button控件,双击进去
随机推荐
- Mysql表操作《一》表的增删改查
一.表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为一条记录 二.创建表 语法 ...
- Linux常用运维指令
cd data/apps./=========================================== ps -ef | grep tomcatps -ef | grep desktopX ...
- 2019建模美赛B题(派送无人机)M奖论文
昨天上午出了建模美赛的结果,我们小组获得的是M奖,感觉挺开心的.我一直觉得拿O奖那种是个概率事件,需要天时地利人和的各种因素都合适才行,所以看到自己是M奖,感觉自己的能力已经得到了认可就很满意了.今天 ...
- 使用 WebClient 來存取 GET,POST,PUT,DELETE,PATCH 網路資源
WebClient 基本資訊 提供通用方法使用 WebRequest 類別傳送及接收 URI (支援 http:, https:, ftp:,和 file: ) 的資源 Namespace:Syste ...
- Go语言特殊函数介绍
main 函数 Go语言程序的默认入口函数(主函数):func main()函数体用{}一对括号包裹.只能应用于package main func main(){ //函数体 } init 函数 go ...
- appium关于当前网络情况测试,实现打开关闭网络(python3.4版)
appium关于当前网络情况测试,实现打开关闭网络(不需要root测试机) # python from appium.webdriver.connectiontype import Connectio ...
- 如何让一个div居于页面正中间
如何让一个div居于页面正中间 如何让一个div居于页面中间,我今天说的是让一个div水平居中同时垂直居中,而不是简单的top:50%,left:50%.当然,我们就按一开始的思路写一下:top,le ...
- 推荐-Everything搜索工具
简介: windows操作系统下极其强大的文件搜索工具. 下载: https://www.voidtools.com/downloads/ 推荐理由: 速度之快难以想象,日常工作必备工具之一. 发现的 ...
- activeMQ入门+spring boot整合activeMQ
最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ.zeroMQ.rocketMQ.Kafka等后续再去学习 ...
- Python 实现flatten功能
from collections import Iterable def flatten(items): for x in items: if isinstance(x, Iterable) and ...