请教哥哥,文件下载代码怎样写?

19831218girl 2004-08-09 05:24:59
请教哥哥,文件下载代码怎样写?
我没分了哦,好抱歉
...全文
576 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
悠悠的爸爸 2004-08-16
  • 打赏
  • 举报
回复
续:

package com.ces.jyjl.util.download;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

/**
* <p>Title: ResponseContentType</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 唐明胜
* @version 1.0
*/

public class ResponseContentType {
public static final String TEXT_CSS = "text/css";
public static final String TEXT_HTML = "text/html";
public static final String TEXT_PLAIN = "text/plain";
public static final String TEXT_RICHTEXT = "text/richtext";
public static final String TEXT_RTF = "text/rtf";
public static final String TEXT_SGML = "text/sgml";
public static final String TEXT_XML = "text/xml";

public static final String IMAGE_CGM = "image/cgm";
public static final String IMAGE_GIF = "image/gif";
public static final String IMAGE_JPEG = "image/jpeg";
public static final String IMAGE_PNG = "image/png";
public static final String IMAGE_TIFF = "image/tiff";

public static final String VIDIO_MPEG = "vidio_mpeg";

public static final String MODEL_VRML = "model/vrml";
public static final String MODEL_IGES = "model/iges";
public static final String MODEL_MESH = "model/mesh";

public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
public static final String APPLICATION_JAVA = "application/java";
public static final String APPLICATION_POSTSCRIPT = "application/postscript";
public static final String APPLICATION_PDF = "application/pdf";
public static final String APPLICATION_ZIP = "application/zip";
public static final String APPLICATION_MSWORD = "application/msword";
public static final String APPLICATION_MSEXCEL = "application/vnd.ms-excel";
public static final String APPLICATION_XML = "application/xml";

private static List resContList = null;
/**
* 功能:对文件类型进行初始化
*/
static {
resContList = new ArrayList();
resContList.add(TEXT_CSS);
resContList.add(TEXT_HTML);
resContList.add(TEXT_PLAIN);
resContList.add(TEXT_RICHTEXT);
resContList.add(TEXT_RTF);
resContList.add(TEXT_SGML);
resContList.add(TEXT_XML);
resContList.add(IMAGE_CGM);
resContList.add(IMAGE_GIF);
resContList.add(IMAGE_JPEG);
resContList.add(IMAGE_PNG);
resContList.add(IMAGE_TIFF);
resContList.add(VIDIO_MPEG);
resContList.add(MODEL_VRML);
resContList.add(MODEL_IGES);
resContList.add(MODEL_MESH);
resContList.add(APPLICATION_OCTET_STREAM);
resContList.add(APPLICATION_JAVA);
resContList.add(APPLICATION_POSTSCRIPT);
resContList.add(APPLICATION_PDF);
resContList.add(APPLICATION_ZIP);
resContList.add(APPLICATION_MSWORD);
resContList.add(APPLICATION_MSEXCEL);
resContList.add(APPLICATION_XML);
}

static String getContentType(String fileName){
String contentType = "application/x-msdownload";
if(fileName.indexOf(".") > -1){
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
int size = resContList.size();
String contTypeName = "";
fileType = fileType.toLowerCase();
for(int i = 0; i < size; i++){
contTypeName = (String)resContList.get(i);
if(contTypeName.indexOf(fileType) > -1){
contentType = contTypeName;
break;
}
}
}
return contentType;
}
}
悠悠的爸爸 2004-08-16
  • 打赏
  • 举报
回复
我给你两个下载文件的类,可下载很多种文件。
package com.ces.jyjl.util.download;

import java.io.*;
import java.sql.*;
import java.net.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ces.jyjl.util.*;
import oracle.jdbc.driver.OracleResultSet;
import javax.servlet.ServletOutputStream;
import oracle.sql.BLOB;
import oracle.sql.CLOB;

/**
* <p>Title: WebDownload</p>
* <p>Description: </p>
* <p>Copyright: CES Copyright (c) 2004</p>
* <p>Company: </p>
* @author 唐明胜
* @version 1.0
*/

public class WebDownload {
private String contentType = "";
private String headerValue = "";
private String argFilePath = "";
private InputStream inStream = null;
private HttpServletResponse response = null;
private HttpServletRequest request = null;

/**
* 设置response的contentType类型。
* @param contentType 其值可参考ResponseContentType提供的值,
* 如果确切不知道如何设置,可以不用调用该方法,系统会自行辨认。
*/
public void setContentType(String contentType) {
this.contentType = (Tool.isEmpty(contentType)) ? "" : contentType;
}

/**
* 功能:设置头部信息Content-Disposition的值。
* @param headerValue 头部信息Content-Disposition
*/
public void setHeaderValue(String headerValue) {
this.headerValue = (Tool.isEmpty(headerValue)) ? "" : headerValue;
}

/**
* 功能:将数据库中的值显示出来,显示字段由sql语句确定,当有多个字段时,
* 只显示第一个字段的值。
* @param request 请求对象;
* @param response 反馈对象;
* @param sqlStr sql查询语句;
* @param fileName 下载时想要显示给客户端的文件名
* @throws SQLException
*/
public void downLoadFromField(HttpServletRequest request,
HttpServletResponse response, String sqlStr,
String fileName) throws SQLException {
this.request = request;
this.response = response;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
request.setCharacterEncoding("GBK");
conn = DAOUtil.getDBConnection();
st = conn.createStatement();
rs = st.executeQuery(sqlStr);
ResultSetMetaData rsmd = rs.getMetaData();
String fieldType = rsmd.getColumnTypeName(1).toUpperCase();
String fieldName = rsmd.getColumnName(1);
String checkType = "BLOB;CLOB;LONG;LONG_RAW;NBLOB;NCLOB";
if (rs.next()) {
if (checkType.indexOf(fieldType) > -1) {
if (fieldType.equalsIgnoreCase("BLOB")) {
BLOB blobValue = (BLOB) rs.getObject(1);
inStream = blobValue.getBinaryStream();
} else if (fieldType.equalsIgnoreCase("CLOB")) {
CLOB clobValue = (CLOB) rs.getObject(1);
inStream = clobValue.binaryStreamValue();
}
} else {
inStream = rs.getBinaryStream(1);
}
setResponse(fileName);
download();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DAOUtil.closeDBConnection(conn);
}
}

/**
* 功能:将服务器路径下的文件下载。
* @param request 页面请求对象
* @param response 页面回馈对象
* @param file 要下载的文件
*/
public void downloadFormFile(HttpServletRequest request, HttpServletResponse response, File file){
if(file == null || !file.exists() || file.isDirectory()){
return;
}
this.request = request;
this.response = response;
try {
request.setCharacterEncoding("GBK");
String fileName = file.getName();
inStream = new FileInputStream(file);
setResponse(fileName);
download();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

/**
* 功能:方法重载。
* <B>此方法为并不确切知道文件在服务器的确切路径,但知道其url字符串。此时可用该方法下载文件。
* 本方法为什么不用url直接产生输入流,原因在于中文名的文件不能正确识别,抛出
* java.io.FileNotFoundException的异常,不知如何解决,目前暂时以文件产生输入流。
* </B>
* @param request 页面请求对象
* @param response 页面回馈对象
* @param fileHttp 要下载的文件
* @throws MalformedURLException
*/
public void downloadFormFile(HttpServletRequest request, HttpServletResponse response, String fileHttp) throws MalformedURLException{
this.request = request;
this.response = response;
try {
String fileName = fileHttp.substring(fileHttp.lastIndexOf("/") + 1);
setResponse(fileName);
request.setCharacterEncoding("GBK");
URL url = new URL(fileHttp);
String protocal = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
String file = url.getFile();
String urlAddr = protocal + "://" + host + ":" + port;
if(port == -1){
urlAddr = protocal + "://" + host + file;
}
urlAddr += file.substring(0, file.lastIndexOf("/") + 1);

// urlAddr += java.net.URLEncoder.encode(fileName, "UTF-8");//JDK1.4提供该方法,且可解决中文名文件下载问题,
urlAddr += Tool.encode(fileName, "UTF-8");
// urlAddr += java.net.URLEncoder.encode(fileName);//JDK1.3提供该方法,但仍然找不到URL指向的中文名文件
//java.net.URLEncoder.
url = new URL(urlAddr);
inStream = url.openStream();
download();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}

/**
* 功能:内部调用。
* @param fileName 完成response的设置
*/
private void setResponse(String fileName) {
try{
fileName = new String(fileName.getBytes("GBK"), "8859_1");
contentType = ResponseContentType.getContentType(fileName);
headerValue = "attachment; filename=" + fileName;
response.setContentType(contentType);
response.setHeader("Content-Disposition", headerValue);
}catch(UnsupportedEncodingException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}

/**
* 内部调用:完成将希望显示的值输出到流中。
*
*/
private void download() {
try {
PrintWriter pwriter = response.getWriter();
int by = -1;
while ((by = inStream.read()) != -1) {
pwriter.write(by);
}
inStream.close();
pwriter.flush();
pwriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
okitgo 2004-08-14
  • 打赏
  • 举报
回复
<%
String filepath="D:\\";
String filename="test.doc";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath+filename);
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
%>
醉马不肖 2004-08-09
  • 打赏
  • 举报
回复
ftp or http?
醉马不肖 2004-08-09
  • 打赏
  • 举报
回复
哈哈
hangzhoufeihu 2004-08-09
  • 打赏
  • 举报
回复
我不知道哦~

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧