⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 下载.txt

📁 写在txt里的描述用java实现下载的原码
💻 TXT
📖 第 1 页 / 共 2 页
字号:


在D:\Tomcat5.0\conf下的web.xml下加入   
    <mime-mapping>   
                  <extension>rar</extension>     
                  <mime-type>application/rar</mime-type>     
          </mime-mapping>


request.addHeader(name,value)
contentType列表
public partial class Administr_test : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
string dddds = Request["DownId"]; 
if (Request["DownId"] != null) 
{ 
string intDownId = Request["DownId"].Trim(); 
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuoMaWDataConnectionString"].ConnectionString); 
conn.Open(); 
String path = Server.MapPath("~/UploadedFile/"); 
string Url = string.Empty; 

string strCmd = "select DownRename from SoftDown where DownId =" + intDownId; 
SqlCommand Cmd = new SqlCommand(strCmd, conn); 
//File.Delete(Url.Trim());//删除该ID的对应的文件附件 
SqlDataReader MdiA = Cmd.ExecuteReader(); 


if (MdiA.Read()) 
{ 
Url = path + MdiA["DownRename"].ToString().Trim(); 
} 
string downstr = "../UploadedFile//"+ MdiA["DownRename"].ToString().Trim(); 

DownFile(downstr); 
} 
} 
public void DownFile(string strDownFile) 
{ 
string str = Server.MapPath(strDownFile); 
if (System.IO.File.Exists(str)) 
{ 
System.IO.FileInfo fi = new System.IO.FileInfo(str); 
System.Web.HttpContext.Current.Response.Clear(); 
System.Web.HttpContext.Current.Response.ClearHeaders(); 
System.Web.HttpContext.Current.Response.Buffer = false; 
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; 
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fi.FullName, System.Text.Encoding.UTF8)); 
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString()); 
System.Web.HttpContext.Current.Response.WriteFile(fi.FullName); 
System.Web.HttpContext.Current.Response.Flush(); 
System.Web.HttpContext.Current.Response.End(); 
} 
else 
{ 
MessageBox.Text="文件未找到!"; 
} 
} 


} 
只允许讯雷下载

<script>OnDownloadClick('thunder://QUFodHRwOi8vMTAyeC54ZG93bnMuY29tL0JhYnlsb252NnIzMi5yYXJaWg==','',location.href,'00754',2,'')</script>

文件上传在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件上传功能。

common-fileupload组件是apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/fileupload/下载。 

用该组件可实现一次上传一个或多个文件,并可限制文件大小。

下载后解压zip包,将commons-fileupload-1.0.jar复制到tomcat的webapps你的webappWEB-INFlib下,目录不存在请自建目录。

新建一个servlet: Upload.java用于文件上传:

import java.io.*; 

import java.util.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import org.apache.commons.fileupload.*; 

public class Upload extends HttpServlet {

private String uploadPath = "C:upload"; // 上传文件的目录

private String tempPath = "C:uploadtmp"; // 临时文件目录

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

}

}

在doPost()方法中,当servlet收到浏览器发出的Post请求后,实现文件上传。以下是示例代码:

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

try {

DiskFileUpload fu = new DiskFileUpload(); 

// 设置最大文件尺寸,这里是4MB

fu.setSizeMax(4194304); 

// 设置缓冲区大小,这里是4kb

fu.setSizeThreshold(4096); 

// 设置临时目录:

fu.setRepositoryPath(tempPath); 

// 得到所有的文件:

List fileItems = fu.parseRequest(request); 

Iterator i = fileItems.iterator(); 

// 依次处理每一个文件:

while(i.hasNext()) {

FileItem fi = (FileItem)i.next(); 

// 获得文件名,这个文件名包括路径:

String fileName = fi.getName(); 

// 在这里可以记录用户和文件信息

// ...

// 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:

fi.write(new File(uploadPath + "a.txt")); 

}

}

catch(Exception e) {

// 可以跳转出错页面

}

}

如果要在配置文件中读取指定的上传文件夹,可以在init()方法中执行:

public void init() throws ServletException {

uploadPath = ....

tempPath = ....

// 文件夹不存在就自动创建:

if(!new File(uploadPath).isDirectory())

new File(uploadPath).mkdirs(); 

if(!new File(tempPath).isDirectory())

new File(tempPath).mkdirs(); 

}

编译该servlet,注意要指定classpath,确保包含commons-upload-1.0.jar和tomcatcommonlibservlet-api.jar。

配置servlet,用记事本打开tomcatwebapps你的webappWEB-INFweb.xml,没有的话新建一个。

典型配置如下:

〈?xml version="1.0" encoding="ISO-8859-1"?〉

〈!DOCTYPE web-app

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd"〉

〈web-app〉

〈servlet〉

〈servlet-name〉Upload〈/servlet-name〉

〈servlet-class〉Upload〈/servlet-class〉

〈/servlet〉

〈servlet-mapping〉

〈servlet-name〉Upload〈/servlet-name〉

〈url-pattern〉/fileupload〈/url-pattern〉

〈/servlet-mapping〉

〈/web-app〉

配置好servlet后,启动tomcat,写一个简单的html测试:

〈form action="fileupload" method="post"

enctype="multipart/form-data" name="form1"〉

〈input type="file" name="file"〉

〈input type="submit" name="Submit" value="upload"〉

〈/form〉

注意action="fileupload"其中fileupload是配置servlet时指定的url-pattern。 

下面是某个大虾的代码:

这个Upload比smartUpload好用多了.完全是我一个个byte调试出来的,不象smartUpload的bug具多.

调用方法:

Upload up = new Upload(); 

up.init(request); 

/**

此处可以调用setSaveDir(String saveDir); 设置保存路径

调用setMaxFileSize(long size)设置上传文件的最大字节.

调用setTagFileName(String)设置上传后文件的名字(只对第一个文件有效)

*/

up. uploadFile(); 

然后String[] names = up.getFileName(); 得到上传的文件名,文件绝对路径应该是

保存的目录saveDir+"/"+names[i]; 

可以通过up.getParameter("field"); 得到上传的文本或up.getParameterValues("filed")

得到同名字段如多个checkBox的值.

其它的自己试试.

源码:____________________________________________________________

package com.inmsg.beans; 

import java.io.*; 

import java.util.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

public class Upload {

private String saveDir = "."; //要保存文件的路径

private String contentType = ""; //文档类型

private String charset = ""; //字符集

private ArrayList tmpFileName = new ArrayList(); //临时存放文件名的数据结构

private Hashtable parameter = new Hashtable(); //存放参数名和值的数据结构

private ServletContext context; //程序上下文,用于初始化

private HttpServletRequest request; //用于传入请求对象的实例

private String boundary = ""; //内存数据的分隔符

private int len = 0; //每次从内在中实际读到的字节长度

private String queryString; 

private int count; //上载的文件总数

private String[] fileName; //上载的文件名数组

private long maxFileSize = 1024 * 1024 * 10; //最大文件上载字节; 

private String tagFileName = ""; 

public final void init(HttpServletRequest request) throws ServletException {

this.request = request; 

boundary = request.getContentType().substring(30); //得到内存中数据分界符

queryString = request.getQueryString(); 

}

public String getParameter(String s) { //用于得到指定字段的参数值,重写request.getParameter(String s)

if (parameter.isEmpty()) {

return null; 

}

return (String) parameter.get(s); 

}

public String[] getParameterValues(String s) { //用于得到指定同名字段的参数数组,重写request.getParameterValues(String s)

ArrayList al = new ArrayList(); 

if (parameter.isEmpty()) {

return null; 

}

Enumeration e = parameter.keys(); 

while (e.hasMoreElements()) {

String key = (String) e.nextElement(); 

if ( -1 != key.indexOf(s + "||||||||||") || key.equals(s)) {

al.add(parameter.get(key)); 

}

}

if (al.size() == 0) {

return null; 

}

String[] value = new String[al.size()]; 

for (int i = 0; i 〈 value.length; i++) {

value[i] = (String) al.get(i); 

}

return value; 

}

public String getQueryString() {

return queryString; 

}

public int getCount() {

return count; 

}

public String[] getFileName() {

return fileName; 

}

public void setMaxFileSize(long size) {

maxFileSize = size; 

}

public void setTagFileName(String filename) {

tagFileName = filename; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -