如何实现文件下载|Java文件下载怎么实现的

|

㈠ 用C#怎么实现文件下载功能

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 代码如下: */ Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); string filename = Server.MapPath("DownLoad/z.zip"); Response.TransmitFile(filename); } //WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) { /* using System.IO; */ string fileName ="asd.txt";//客户端保存的文件名 string filePath=Server.MapPath("DownLoad/aaa.txt");//路径 FileInfo fileInfo = new FileInfo(filePath); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(fileInfo.FullName); Response.Flush(); Response.End(); } //WriteFile分块下载 protected void Button3_Click(object sender, EventArgs e) { string fileName = "aaa.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true) { const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 byte[] buffer = new byte[ChunkSize]; Response.Clear(); System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); long dataLengthToRead = iStream.Length;//获取下载的文件总大小 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); while (dataLengthToRead > 0 && Response.IsClientConnected) { int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 Response.OutputStream.Write(buffer, 0, lengthRead); Response.Flush(); dataLengthToRead = dataLengthToRead – lengthRead; } Response.Close(); } } //流方式下载 protected void Button4_Click(object sender, EventArgs e) { string fileName = "aaa.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }}这里提供4种常用下载方式 以供参考

㈡ html怎么实现网页中文件下载功能

共两种方法:

一、使用<a>标签来完成

㈢ VB.NET如何实现文件的下载

给你一个遍历所有盘符下的文件夹的例子加一个遍历文件的就可以了。TreeNode node = new TreeNode("我的电脑"); treeView.Nodes.Add(node); //加入一个我的电脑节点 string[] drivesName = System.IO.Directory.GetLogicalDrives() //取得驱动器列表的集合 foreach(string name in drivesName) //用foreach遍历集合 { TreeNode drivesNode = new TreeNode(name); node.Nodes.Add(drivesNode); //加到我的电脑节点下 }

㈣ java文件下载怎么实现的

下载就很简单了把你要下载的文件做成超级链接,可以不用任何组件比如说下载一个word文档<a href="名称.doc">名称.doc</a>路径你自己写import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.ProtocolException;import java.net.URI;import java.net.URL;import java.util.Random;/** * * 实现了下载的功能*/ public class SimpleTh {public static void main(String[] args){ // TODO Auto-generated method stub //String path = "http://www.7cd.cn/QingTengPics/倩女幽魂.mp3";//MP3下载的地址 String path ="http://img.99luna.com/music/%CF%EB%C4%E3%BE%CD%D0%B4%D0%C5.mp3"; try { new SimpleTh().download(path, 3); //对象调用下载的方法 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getFilename(String path){//获得文件的名字 return path.substring(path.lastIndexOf('/')+1); }public void download(String path,int threadsize) throws Exception//下载的方法 {//参数 下载地址,线程数量 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection();//获取HttpURLConnection对象 conn.setRequestMethod("GET");//设置请求格式,这里是GET格式 conn.setReadTimeout(5*1000);// int filelength = conn.getContentLength();//获取要下载文件的长度 String filename = getFilename(path); File saveFile = new File(filename); RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd"); accessFile.setLength(filelength); accessFile.close(); int block = filelength%threadsize ==0?filelength/threadsize:filelength/threadsize+1; for(int threadid = 0;threadid<=threadsize;threadid++){ new DownloadThread(url,saveFile,block,threadid).start(); } } private final class DownloadThread extends Thread{ private URL url; private File saveFile; private int block;//每条线程下载的长度 private int threadid;//线程id public DownloadThread(URL url,File saveFile,int block,int threadid){ this.url = url; this.saveFile= saveFile; this.block = block; this.threadid = threadid; } @Override public void run() { //计算开始位置的公式:线程id*每条线程下载的数据长度=? //计算结束位置的公式:(线程id+1)*每条线程下载数据长度-1=? int startposition = threadid*block; int endposition = (threadid+1)*block-1; try { try { RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd"); accessFile.seek(startposition);//设置从什么位置写入数据 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(5*1000); conn.setRequestProperty("Range","bytes= "+startposition+"-"+endposition); InputStream inStream = conn.getInputStream(); byte[]buffer = new byte[1024]; int len = 0; while((len = inStream.read(buffer))!=-1){ accessFile.write(buffer, 0, len); } inStream.close(); accessFile.close(); System.out.println("线程id:"+threadid+"下载完成"); } catch (FileNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } }}

㈤ python如何实现文件的下载,请尽量详细,高分!!!

import os,urllib.request,reos.chdir(r'd:')data = urllib.request.urlopen(url).read()with open(filename, 'wb') as f: f.write(data)url就是你要下载的文件链接,filename就是下载后保存的文件名。这段代码是把文件下载在d盘根目录下,你可以自己修改。不过是单线程的,想要多线程下载,比较复杂,我没有试过,这个下载小文件还是没有问题的。

㈥ 如何用JavaScript实现文件下载

参考如下:<head runat="server"> <title>文件下载</title></head><script type="text/javascript" > // 使用JS实现下载.jpg、.doc、.txt、.rar、.zip等文件的方法(参数 imgOrURL 为需要下载的图片的URL地址) // 使用该方法实现下载压缩文件时会有网页错误信息提示 // .doc、.rar、.zip 文件可以直接通过文件地址下载, // 如:<a href="../Images/test.doc" >点击下载文件</a> <a href="../Images/test.zip" >点击下载文件</a> function saveImageAs(imgOrURL) { if (typeof imgOrURL == 'object') imgOrURL = imgOrURL.src; window.win = open (imgOrURL); setTimeout('win.document.execCommand("SaveAs")', 500); } // 使用JS实现下载.txt、.doc、.txt、.rar、.zip等文件的方法(参数 fileURL 为需要下载的图片的URL地址) // 使用该方法实现下载压缩文件时不会有网页错误信息,但是不能使用该方法下载.jpg图片文件 // .doc、.rar、.zip 文件可以直接通过文件地址下载, // 如:<a href="../Images/test.doc" >点击下载文件</a> <a href="../Images/test.zip" >点击下载文件</a> function savetxt(fileURL){ var fileURL=window.open (fileURL,"_blank","height=0,width=0,toolbar=no,menubar=no,scrollbars=no,resizable=on,location=no,status=no"); fileURL.document.execCommand("SaveAs"); fileURL.window.close(); fileURL.close(); } // 功能类似savetxt方法,但是下载时初始文件名为code.txt,而不是跟目标文件名相同 function svcode(obj) { var winname = window.open('', '_blank', 'height=1,width=1,top=200,left=300'); winname.document.open('text/html', 'replace'); winname.document.writeln(obj.value); winname.document.execCommand('saveas','','code.txt'); winname.close(); }</script><body> <div> <br /> <a href="javascript: void 0" onclick="savetxt('../Images/test.txt'); return false">点击下载文件</a> <br /><br /> <a href="javascript:savetxt('../Images/test.txt')" >点击下载文件</a> <br /><br /> <!– .doc、.rar、.zip 文件可以直接通过文件所在地址下载 –> <a href="../Images/test.doc" >点击下载文件</a> <br /><br /> <a href="../Images/test.zip" >点击下载文件</a> </div></body>

㈦ ASP 如何实现文件下载

你把要下载的文件名传到下载页面,用request("fileNameField")获取文件名下面这地方改一下iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _";Data Source=" & server.mappath(request("fileNameField"))点击回下载答的地方用<a href='下载页面路径?fileNameField=要下载的文件名'>下载文件</a>这个

㈧ 从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){ } }

这个是服务器端文件下载工具类 题主可以试试,望采纳

㈨ 如何用ASP实现文件下载

调用response.Write("<a href=down.asp?filename="&UpLoadPath&ls_array(i+1)&">"&ls_array(i)&"</td></tr>")down.asp文件内容如下:<%Const FilePath = "UploadFile/" '文件存放路径From_url = Cstr(Request.ServerVariables("HTTP_REFERER"))Serv_url = Cstr(Request.ServerVariables("SERVER_NAME"))Function GetFileName(longname)'/folder1/folder2/file.asp=>file.asp while instr(longname,"/") longname = right(longname,len(longname)-1) wend GetFileName = longnameEnd FunctionDim StreamDim ContentsDim FileNameDim TrueFileNameDim FileExtConst adTypeBinary = 1FileName = Request.QueryString("FileName")if FileName = "" Then Response.Write "无效文件名!" Response.EndEnd ifFileExt = Mid(FileName, InStrRev(FileName, ".") + 1)Response.Clearif lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then Response.ContentType = "image/*" '对图像文件不出现下载对话框else Response.ContentType = "application/ms-download"end ifResponse.AddHeader "content-disposition", "attachment; filename=" & GetFileName(Request.QueryString("FileName"))Set Stream = server.CreateObject("ADODB.Stream")Stream.Type = adTypeBinaryStream.OpenTrueFileName= FilePath &FileNameResponse.Write TrueFileName Response.End Stream.LoadFromFile Server.MapPath(TrueFileName)While Not Stream.EOS Response.BinaryWrite Stream.Read(1024 * 64)WendStream.CloseSet Stream = NothingResponse.FlushResponse.End%>


赞 (0)