日期:2014-05-18  浏览次数:20896 次

求文件下载?
我要做一个文件上传和下载的页面,上传部分已经写好了,(使用jspsmart组件写的)。上传的文件放在了c:/soft目录下面。
现在不会写下载部分的程序,
下载的程序要求:
jsp中列出c:/soft目录下的所有文件,点击一个文件开始下载,
(比如c:/soft有两个文件,则页面上要列出这两个文件,其中的一个文件就可以下载了)

高分请高手帮帮忙!


------解决方案--------------------
File f=new File( "c:/soft ");
String[] lsa_files=f.list();
for(int i=0;i <lsa_files.length;i++)
{
<a href= '/servlet/Download?filename= '+ <%=lsa_files[i]%> > download </a>
}

Download里调用jspsmart的download函数。
------解决方案--------------------
在Download这个servlet里:
SmartUpload su=new SmartUpload(getConfig().getServletContext(),request,response);
String ls_file=request.getParameter( "filename ");//需要处理一下文件路径
try
{
su.setContentDisposition(null);
su.downloadFile(ls_file);
}
catch(Exception e)
{}

------解决方案--------------------
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

public class up_downloadSevelet extends HttpServlet {

private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}

final public ServletConfig getServletConfig() {
return config;
}
/**
* Handles GET requests
*/

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

String filename=request.getParameter( "filename ");//需要处理一下文件路径
response.addHeader( "Content-Disposition ", "attachment;filename= "+filename);

//SmartUpload su=new SmartUpload(getConfig().getServletContext(),request,response);
SmartUpload mySmartUpload=new SmartUpload();
try
{
mySmartUpload.initialize(config,request,response);
mySmartUpload.setContentDisposition(null);
mySmartUpload.downloadFile( "d:/ "+filename);
}
catch(Exception e)
{}
}


public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}


}

------解决方案--------------------
页面部分:
<%@ page language= "java " import= "java.util.* " pageEncoding= "GB2312 "%>
<%@ page contentType= "text/html; charset=GBK " %>
<%@ taglib uri= "/WEB-INF/struts-bean.tld " prefix= "bean " %>
<%@ taglib uri= "/WEB-INF/struts-html.tld " prefix= "html " %>
<%@ taglib uri= "/WEB-INF/struts-logic.tld " prefix= "logic " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<base href= " <%=basePath%> ">
</head>
<body