当前位置: 技术问答>java相关
在JSP中如何实现鼠标左键单击执行下载任务?
来源: 互联网 发布时间:2015-11-06
本文导语: 现网页上有一链接指向一WORD文件,单击此链接刚直接在IE中打开WORD文件,现在想用左键单击实现下载此文件的功能,不知道怎么做,请高手指点。 | 写个servlet 我的程序你参考一下 import java.i...
现网页上有一链接指向一WORD文件,单击此链接刚直接在IE中打开WORD文件,现在想用左键单击实现下载此文件的功能,不知道怎么做,请高手指点。
|
写个servlet
我的程序你参考一下
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import perpss.common.*;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class FileDownload extends HttpServlet
{
public void doGet(HttpServletRequest req ,HttpServletResponse res)
throws IOException,ServletException
{
try
{
doPost(req,res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req ,HttpServletResponse res)
throws IOException,ServletException
{
res.setContentType("application/x-filler");
try
{
String downloadFile = "......your file";
String download_date = "sysdate";
String sHeaderString = "attachment;filename=" + sFileName;
res.setHeader("Content-Disposition",sHeaderString);
ServletOutputStream stream = res.getOutputStream();
BufferedInputStream fif = new BufferedInputStream(new
FileInputStream(downloadFile));
int data;
while((data = fif.read()) != -1)
{
stream.write(data);
stream.flush();
}
fif.close();
stream.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{}
catch (Exception ignore) {}
}
}
}
我的程序你参考一下
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import perpss.common.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class FileDownload extends HttpServlet
{
public void doGet(HttpServletRequest req ,HttpServletResponse res)
throws IOException,ServletException
{
try
{
doPost(req,res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req ,HttpServletResponse res)
throws IOException,ServletException
{
res.setContentType("application/x-filler");
try
{
String downloadFile = "......your file";
String download_date = "sysdate";
String sHeaderString = "attachment;filename=" + sFileName;
res.setHeader("Content-Disposition",sHeaderString);
ServletOutputStream stream = res.getOutputStream();
BufferedInputStream fif = new BufferedInputStream(new
FileInputStream(downloadFile));
int data;
while((data = fif.read()) != -1)
{
stream.write(data);
stream.flush();
}
fif.close();
stream.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{}
catch (Exception ignore) {}
}
}
}