㈠ 怎麼在網頁上設置下載文件
比如說你要通過網頁下載一個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>標簽來完成