|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
jsp中文件下载采用RequestDispatcher进行,从网上搜来了如下代码,问下在JSP页面中怎么指向该action。还有,其中
String filedownload = "/upload/helloworld.jar";
这句中的"/upload/helloworld.jar"是把upload保存在项目下,还是保存在WebRoot下?
采用RequestDispatcher进行文件下载代码如下:
package cn.jbit.download.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 6765085208899952414L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filedownload = "/upload/helloworld.jar";//即将下载的文件的相对路径
String filedisplay = "helloworld.jar";//下载文件时显示的文件保存名称
response.setContentType("application/x-download");//设置为下载application/x-download
//response.setContentType("application/x-msdownload");//设置为下载application/x-msdownload
//response.setContentType("application/octet-stream");//设置为下载application/octet-stream
response.addHeader("Content-Disposition", "attachment;filename="
+ filedisplay);
try {
RequestDispatcher rd = request.getRequestDispatcher(filedownload);
if(rd != null)
{
rd.forward(request,response);
}
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}
}
你这个是将那个webroot下的helloworld.jar 资源用转发给用户下载,没有保存这一说
/upload/helloworld.jar 指的是 localhost:8080/项目名称/upload/helloworld.jar 这个资源
问下在JSP页面中怎么指向该action:
在web.xml中配置 指向这个severlet的mapping呢,这个比较基础的内容,你学习javaweb应该有讲的 好好听课
|
|