求用JSP实现文件的下载的代码

lijing1666 2005-09-27 03:16:37
求用JSP实现文件的下载的代码,点击一个链接则自动将该链接文件(.zip文件)保存到指定路径下
...全文
354 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijing1666 2005-10-07
  • 打赏
  • 举报
回复
其实简单的<a href=后面直接跟文件的地址就可以了,因为我要求的比较简单.
shoudu 2005-09-28
  • 打赏
  • 举报
回复
up
zdsxj2002 2005-09-28
  • 打赏
  • 举报
回复
你们在使用时。将AppFileUtil换成你们自己的就行了
zdsxj2002 2005-09-28
  • 打赏
  • 举报
回复
AppFileUtil aFileUpload = new AppFileUtil();
//UtFile.setFileId(strFileId);
UtFile uFile = new UtFile();
try {
uFile = aFileUpload.loadFile(strFileId);
得到的是一个文件对象。里边。其实也就是要下载的文件信息,和文件内容
zdsxj2002 2005-09-28
  • 打赏
  • 举报
回复
package com.sailing.office.workflow.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import com.sailing.office.workflow.app.AppFileUtil;
import com.agou.ss.SSTrace;
import com.agou.ss.SSLogiException;
import com.agou.ss.SSDeregulationException;
import com.agou.util.UtFile;
import com.agou.util.UtFormat;

/**
* <p>Title: 文件下载</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class SeFileDownload
extends HttpServlet {

/**
* 错误码定义
*/
/*下载的文件不存在*/
private static final int de40096 = 40096;

/**/
private static final String CONTENT_TYPE = "application/octet-stream";
/*文件缓存*/
private static final int bufferSize = 4096;

/**
* 初始化
* @throws ServletException
*/
public void init() throws ServletException {
}

/**
* doGet方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String strFileId = request.getParameter("FILEID");
String strMode = request.getParameter("MODE");
if (UtFormat.isEmpty(strMode)) {
strMode = "attachment";
}
else if (UtFormat.compareStr(strMode, "attachment") == 0) {
strMode = "attachment";
}
else {
strMode = "inline";
}
if (UtFormat.isEmpty(strFileId)) {
SSDeregulationException de = new SSDeregulationException(de40096, "下载的文件不存在", "SeFileDownload", "doPut()");
de.addDetail("要下载的文件ID是:" + strFileId);
throw de;
}
AppFileUtil aFileUpload = new AppFileUtil();
//UtFile.setFileId(strFileId);
UtFile uFile = new UtFile();
try {
uFile = aFileUpload.loadFile(strFileId);
if (uFile == null) {
SSDeregulationException de = new SSDeregulationException(de40096, "下载的文件不存在", "SeFileDownload", "doPut()");
de.addDetail("要下载的文件ID是:" + strFileId);
throw de;

}
}
catch (SSLogiException e) {
SSTrace.error(e);
SSDeregulationException de = new SSDeregulationException(de40096, "下载的文件不存在", "SeFileDownload", "doPut()");
de.addDetail("要下载的文件ID是:" + strFileId);
throw de;
}
//设置要下载的文件长度
response.reset();
response.setContentType(CONTENT_TYPE);
response.setContentLength( (int) uFile.getFileSize());
//表示在浏览器中打开(inline)/表示另存为(attachment)
response.setHeader("Content-Disposition", strMode + ";filename=\"" + new String(uFile.getClientFileName().getBytes(), "ISO-8859-1") + "\"");
//设置缓冲
byte[] buffer = new byte[bufferSize];
BufferedOutputStream output = null;
BufferedInputStream input = null;
// 写缓冲区:
try {
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new ByteArrayInputStream(uFile.getFileContent()));

int n = ( -1);
do {
n = input.read(buffer, 0, bufferSize);
if (n != ( -1)) {
output.write(buffer, 0, n);
}
}
while (n != ( -1));
response.flushBuffer();
}
catch (java.net.SocketException e) {
SSTrace.error("中止下载文件", e);
}
catch (Exception e) {
SSTrace.error("文件下载时出现异常:", e);
}
finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
}

}

/**
* 执行doPut方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

/**
*
*/
public void destroy() {
}

/**
* 根据后缀名取得当前Content_TYPE
* @param extFileName
* @return
* @throws java.lang.Exception
*/
public String findFilePostfix(String extFileName) {
String contentType = "contentType/octet-stream";
if (extFileName.equals("txt")) {
contentType = "text/plain";

}
if (extFileName.equals("html") || extFileName.equals("htm")) {
contentType = "text/html";

}
if (extFileName.equals("doc")) {
contentType = "application/msword";

}
if (extFileName.equals("xls")) {
contentType = "application/vnd.ms-excel";

}
if (extFileName.equals("ppt")) {
contentType = "application/vnd.ms-powerpoint";

}
if (extFileName.equals("pdf")) {
contentType = "application/pdf";

}
if (extFileName.equals("jpg") || extFileName.equals("gif") || extFileName.equals("bmp") || extFileName.equals("png") ||
extFileName.equals("psd")) {
contentType = "image/" + extFileName;

}
return contentType;
}
}
lijing1666 2005-09-28
  • 打赏
  • 举报
回复
ding
lijing1666 2005-09-28
  • 打赏
  • 举报
回复
谢谢!不过简单的HTML代码能否实现这个功能呢?

81,092

社区成员

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

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