import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.*;public class DownloadChannel
{
protected String filenameEncoding = "GBK";
/**
*
*/
public DownloadChannel()
{
} /**
*
* @param showName
* @param fileName
* @param response
* @return
*/
public boolean display(String showName, String fileName,
HttpServletResponse response)
{
FileInputStream in = null;
OutputStream out = null; try
{
if (fileName == null)
return false;
if (showName == null)
showName = fileName.substring(fileName.lastIndexOf(File.
separator) + 1, fileName.length()); showName = new String(showName.getBytes(filenameEncoding),
"iso-8859-1");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition",
"attachment; filename=" + showName);
long fileSize = new File(fileName).length();
response.setContentLength( (int) fileSize);
in = new FileInputStream(fileName);
out = response.getOutputStream();
byte[] buffer = new byte[4096];
int len = -1;
while ( (len = in.read(buffer)) != -1)
{
out.write(buffer, 0, len);
} response.flushBuffer(); } catch (Exception ex)
{
return false;
} finally
{
if (in != null)
{
try
{
in.close();
} catch (Exception e)
{
}
}
if (out != null)
{
try
{
out.close();
} catch (Exception e)
{
}
}
return true;
}
}
}
在servlet里调用
解决方案 »
- 求助:根据有限点序列,推出最接近的非线性函数
- Exception in thread "main" java.lang.UnsatisfiedLinkError
- 有正在学习或者使用ServiceMix的朋友吗?大家互相交流一下。
- Java对象引用的问题
- java 说爱你真的不容易
- 关于SpringMVC下载不弹窗的问题,谢谢。
- servlet的web.xml配置问题!
- tomcat下虚拟目录出现的找不到文件的问题,望各位能帮眼看看,谢了。
- 请教redhat下如何安装weblogic7的WLS Domain
- 求帮忙解决服务器架设问题
- 对各种j2ee服务器的疑问(不够再加分)
- 谁有会话BEAN调用实体BEAN的例子?我非常需要!
错误:不能读:DownloadChannel.java
1 个错误C:\> 汗~~~
老大我是java新手,能告诉我怎么调吗?
我想在网页里运行它