<%@page import="java.io.*"%><%
downloadFile("c:\\mplayer.exe","application/x-zip-compressed","downloaded.zip",1024,response);
%><%!
public void downloadFile(String sourceFilePathName, String contentType, String destFileName, int blockSize,HttpServletResponse response)
throws IOException, ServletException
{
String m_contentDisposition = new String(); File file = new File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[blockSize];
//ホトシタ獎ヘ
if(contentType == null)
response.setContentType("application/x-msdownload");
else
if(contentType.length() == 0)
response.setContentType("application/x-msdownload");
else
response.setContentType(contentType); //ウ、カネ
response.setContentLength((int)fileLen);
//
m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
if(destFileName == null)
response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append( getFileName(sourceFilePathName) )));
else
//マツヤリコトホトシテ・
if(destFileName.length() == 0)
response.setHeader("Content-Disposition", m_contentDisposition);
else
response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append(destFileName)));
while((long)totalRead < fileLen)
{
readBytes = fileIn.read(b, 0, blockSize);
totalRead += readBytes;
response.getOutputStream().write(b, 0, readBytes);
}
fileIn.close();
}private String getFileName(String filePathName)
{
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
pos = filePathName.lastIndexOf('/');
if(pos != -1)
return filePathName.substring(pos + 1, filePathName.length());
pos = filePathName.lastIndexOf('\\');
if(pos != -1)
return filePathName.substring(pos + 1, filePathName.length());
else
return filePathName;
}
%>
downloadFile("c:\\mplayer.exe","application/x-zip-compressed","downloaded.zip",1024,response);
%><%!
public void downloadFile(String sourceFilePathName, String contentType, String destFileName, int blockSize,HttpServletResponse response)
throws IOException, ServletException
{
String m_contentDisposition = new String(); File file = new File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[blockSize];
//ホトシタ獎ヘ
if(contentType == null)
response.setContentType("application/x-msdownload");
else
if(contentType.length() == 0)
response.setContentType("application/x-msdownload");
else
response.setContentType(contentType); //ウ、カネ
response.setContentLength((int)fileLen);
//
m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
if(destFileName == null)
response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append( getFileName(sourceFilePathName) )));
else
//マツヤリコトホトシテ・
if(destFileName.length() == 0)
response.setHeader("Content-Disposition", m_contentDisposition);
else
response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append(destFileName)));
while((long)totalRead < fileLen)
{
readBytes = fileIn.read(b, 0, blockSize);
totalRead += readBytes;
response.getOutputStream().write(b, 0, readBytes);
}
fileIn.close();
}private String getFileName(String filePathName)
{
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
pos = filePathName.lastIndexOf('/');
if(pos != -1)
return filePathName.substring(pos + 1, filePathName.length());
pos = filePathName.lastIndexOf('\\');
if(pos != -1)
return filePathName.substring(pos + 1, filePathName.length());
else
return filePathName;
}
%>
解决方案 »
- 求JSP网站的毕业论文,
- jstl 遍历td中的table ,要求每7个列换一新行
- 求一个用js做的表单分页
- 如何实现,在java的黑屏下执行语句到最后提示输入yes后回到刚进来时的程序输入no后退出!
- 关于表单数据回显的问题!在线等!
- 当我从servlet传送session值到jsp中发生的错误,各位进来看看,谢谢!
- 小弟想学JSP但不知道怎样配置???今天搞了一天都没有配成。大虾们能不能详细解说一下要怎样配置。所需文件和配置过程。搞成之后一定给
- 请教一数据类型传递问题
- 如何在jsp中察看exception
- 如何用JSP实现聊天是中的自动滚屏功能?
- 如何实现让用户下载服务器上中文文件名的文档?
- 一个Log4j的DailyRollingFileAppender每日保存的问题
public void downloadFile(HttpServletResponse res,
String pathName, //路径文件名
String fileName, //下载初始默认文件名
String contentType) throws IOException {
InputStream inputStream = new FileInputStream(pathName);
res.setHeader("Pragma", "");
res.setHeader("Cache-Control", "");
res.setContentType(contentType);
String fnEncode = new String(fileName.getBytes("gb2312"), "8859_1");
res.setHeader("Content-Disposition", "attachment; filename="+fnEncode);
res.setHeader("Cache-Control", "max-age=0"); int size = 0;
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(inputStream);
output = new BufferedOutputStream(res.getOutputStream());
int c;
while ((c = input.read()) != -1) {
output.write(c);
size++;
}
output.flush();
res.getOutputStream().flush();
} finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
} }