㈠ 怎么在网页上设置下载文件
比如说你要通过网页下载一个word文档,需要有这几步操作:
1、通过ftp把word文档上传到服务器,最好以英文命名;
2、在后台编辑文章的时候,给某个文字添加一个链接,这个链接地址就是word文档的地址;
3、保存之后,来到前台页面,点击文字,网页就会自动下载这个文档到你电脑本地。
㈡ 从web服务器上下载文件是如何实现的
/**
*根据文件输入流,和文件名称下载文件
*@paramresp HttpServletResponse
*@paramfile 供下载的文件
*@paramfile_name 所显示的下载文件名称
*/
publicvoidFileDownLoad(HttpServletResponseresp,Filefile,Stringfile_name){
try{
StringfileName=newString(file_name.getBytes("GBK"),"ISO8859_1");
resp.setContentType("application;charset=utf-8"); //指定文件的保存类型。
resp.setHeader("Content-disposition","attachment;filename="+fileName);
ServletOutputStreamoupstream=resp.getOutputStream();
FileInputStreamfrom=newFileInputStream(file);
byte[]buffer=newbyte[catchSize];
intbytes_read;
while((bytes_read=from.read(buffer))!=-1){
oupstream.write(buffer,0,bytes_read);
}
oupstream.flush();
}catch(Exceptione){
}
}
这个是服务器端文件下载工具类 题主可以试试,望采纳
㈢ JavaWeb下载文件,怎么获取文件下载完毕的状态
在Javaweb中,上传下载是经常用到的功能,对于文件上传,浏览器在上传的过程中是以流的过程将文件传给服务器,一般都是使用commons-fileupload这个包实现上传功能,因为commons-fileupload依赖于commons-io这个包,所以需要下载这两个包commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar。
1、搭建环境
创建Web项目,将包导入到项目lib下
3、实现文件下载
(第一种文件下载)
注意:该代码是直接访问Servlet类的
?
04142package com.load;import java.io.FileInputStream;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;//直接使用Http://localhost:8080/Test1/download进行下载,但是这个有缺陷,如果下载文件名中有中文,就会变成乱码现象!@WebServlet("/download")public class download extends HttpServlet {private static final long serialVersionUID = 1L;public download() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/plain;charset=utf-8");response.setCharacterEncoding("utf-8");response.setHeader("Location","中文.txt");response.setHeader("Content-Disposition", "attachment; filename=" + "账号.txt");OutputStream outputStream = response.getOutputStream();InputStream inputStream = new FileInputStream("E:/loads"+"/账号.txt");byte[] buffer = new byte[1024];int i = -1;while ((i = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, i);}outputStream.flush();outputStream.close();}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
(第二种下载方法)
新建jsp页面选择下载
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>下载</title></head><body><a href="../DoDownloadServlet?filename=呵呵.txt">点击下载</a></body></html>
创建Servlet类进行下载(注意:该下载如果文件名是中文的话,一样会出现乱码现象)
package com.load;
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.JspFactory;import javax.servlet.jsp.PageContext;import org.h
sqldb.lib.StringUtil;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;@WebServlet("/DoDownloadServlet")public class DoDownloadServlet extends HttpServlet {private static final long serialVersionUID = 1L;public DoDownloadServlet() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//得到下载文件的名称//String filename = request.getParameter("filename");//String filename = new String(FileName.getBytes("iso8859-1"),"UTF-8");//新建SmartUpload对象SmartUpload su = new SmartUpload();PageContext pagecontext = JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);//上传初始化su.initialize(pagecontext);//设置禁止打开该文件su.setContentDisposition(null);//下载文件try {su.downloadFile("/listener/"+filename);} catch (SmartUploadException e) {// TODO Auto-generated catch blocke.printStackTrace();}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
(第三种下载的方法)
同上的jsp页面代码,这里就不再重复了。
新建Serlvet类,实现下载功能(注意:这里文件名就算是中文名,也不会出现乱码问题了!)
package com.load;
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.JspFactory;import javax.servlet.jsp.PageContext;import org.hsqldb.lib.StringUtil;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;@WebServlet("/DoDownloadServlet")public class DoDownloadServlet extends HttpServlet {private static final long serialVersionUID = 1L;public DoDownloadServlet() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获得文件名称String path1 = request.getParameter("filename");//获得路径名称String path = request.getSession().getServletContext().getRealPath("/listener/"+path1);// path是根据日志路径和文件名拼接出来的File file = new File(path);String filename = file.getName();try {//判断是否是IE11Boolean flag= request.getHeader("User-Agent").indexOf("like Gecko")>0;//IE11 User-Agent字符串:Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko//IE6~IE10版本的User-Agent字符串:Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.0; Trident/6.0)if (request.getHeader("User-Agent").toLowerCase().indexOf("msie") >0||flag){filename = URLEncoder.encode(filename, "UTF-8");//IE浏览器}else {//先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,//这个文件名称用于浏览器的下载框中自动显示的文件名filename = new String(filename.replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1");//firefox浏览器//firefox浏览器User-Agent字符串://Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0} InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer;buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.addHeader("Content-Disposition", "attachment;filename=" +filename);response.addHeader("Content-Length", "" + file.length());OutputStream os = response.getOutputStream();response.setContentType("application/octet-stream");os.write(buffer);// 输出文件os.flush();os.close();} catch (IOException e) {e.printStackTrace();}System.out.println(filename);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
㈣ 网页中的下载文件是如何实现的
是上传到服务器上的链接文件!
㈤ 如何下载网页的所有文件
当你打开一个网页的时候,你可以点击导航工具里面的-页面-另存为-选择所有文件即可保持到本机上面。
㈥ Javaweb中的文件下载实现
需要在响应头部加上一些标示,告诉浏览器这个是文件下载。
如果你用了框架比如struts,需要加如下配置
<result name="success" type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">fileStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">2048</param>
</result>
如果没有用框架,就手动在返回对象添加这些contentType
㈦ 在InternetExplore中,如果按Web方式下载文件,那么只需要()。
果断选择D,这个web下载只需要<a href="yourfile">点击下种子啊</a>
㈧ 怎样用web迅雷下载文件
把你的MP4接到电脑上面,把你需要的东西直接复制进去就可以了
㈨ html怎么实现网页中文件下载功能
共两种方法:
一、使用<a>标签来完成