JSP中的文件下载
2004-07-28 18:39
351 查看
JSP中文件下载
今天折腾了一会才搞出来
需要的文件:Aattchment.java和downAttach.jsp
Attachment.java
public static void downloadAttachment(String fileName,HttpServletResponse response)
{
//filename=Test.doc .....
//转换成硬盘里实际地址
String fileOnDisk = ;
InputStream input = null;
OutputStream output = null;
File f = null;
try
{
try
{
input = new FileInputStream(fileOnDisk);
f = new File(fileOnDisk);
} catch(IOException e)
{
System.out.println("can not get attchment on disk");
}
byte[] buffer = getBytes(input);
input.close();
input = null;
output = response.getOutputStream();
response.setContentType("application/octet-stream");
response.setHeader("Location", fileName);
response.setHeader("Content-Disposition", "attachment; filename=/"" + fileName+"/"");
response.setContentLength((int)f.length());
output.write(buffer);
output.flush();
output.close();
output = null;
}
catch(IOException e)
{
System.out.println("error download attachment.");
}
finally
{
if (input != null) {
try {
input.close();
} catch (IOException ex) { }
}
if (output != null) {
try {
output.close();
} catch (IOException ex) { }
}
}
}
public static byte[] getBytes(InputStream inputStream) throws IOException
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] block = new byte[512];
while (true) {
int readLength = inputStream.read(block);
if (readLength == -1) break;
byteArrayOutputStream.write(block, 0, readLength);
}
byte[] retValue = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return retValue;
}
downAttach.jsp:
<%@ page language="java" pageEncoding="GB2312" %>
<%@ page import="your.package"%>
<%
response.setContentType("xxxxxxx");
//这里不用setContentType()的话,每次浏览器会自动打开文件,出现一堆乱码,参数值任意。
String fileName = request.getParameter("fileName");
if (fileName==null||fileName.length()==0) {
return;
}
Attachment.downloadAttachment(fileName,response);
%>
今天折腾了一会才搞出来
需要的文件:Aattchment.java和downAttach.jsp
Attachment.java
public static void downloadAttachment(String fileName,HttpServletResponse response)
{
//filename=Test.doc .....
//转换成硬盘里实际地址
String fileOnDisk = ;
InputStream input = null;
OutputStream output = null;
File f = null;
try
{
try
{
input = new FileInputStream(fileOnDisk);
f = new File(fileOnDisk);
} catch(IOException e)
{
System.out.println("can not get attchment on disk");
}
byte[] buffer = getBytes(input);
input.close();
input = null;
output = response.getOutputStream();
response.setContentType("application/octet-stream");
response.setHeader("Location", fileName);
response.setHeader("Content-Disposition", "attachment; filename=/"" + fileName+"/"");
response.setContentLength((int)f.length());
output.write(buffer);
output.flush();
output.close();
output = null;
}
catch(IOException e)
{
System.out.println("error download attachment.");
}
finally
{
if (input != null) {
try {
input.close();
} catch (IOException ex) { }
}
if (output != null) {
try {
output.close();
} catch (IOException ex) { }
}
}
}
public static byte[] getBytes(InputStream inputStream) throws IOException
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] block = new byte[512];
while (true) {
int readLength = inputStream.read(block);
if (readLength == -1) break;
byteArrayOutputStream.write(block, 0, readLength);
}
byte[] retValue = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return retValue;
}
downAttach.jsp:
<%@ page language="java" pageEncoding="GB2312" %>
<%@ page import="your.package"%>
<%
response.setContentType("xxxxxxx");
//这里不用setContentType()的话,每次浏览器会自动打开文件,出现一堆乱码,参数值任意。
String fileName = request.getParameter("fileName");
if (fileName==null||fileName.length()==0) {
return;
}
Attachment.downloadAttachment(fileName,response);
%>
相关文章推荐
- jsp+servlet文件下载
- jsp文件的上传和下载操作代码
- 通过流或jspsmartupload.jar实现文件下载
- jsp文件下载中的文件名等属性设置
- struts2文件下载 与 jsp文件下载的方式
- javaweb jsp页面中实现文件下载
- 使用jspSmartUpload组件进行文件上传、下载
- JSP/servlet实现文件上传下载和删除
- jsp中文件下载的实现
- 使用jxl,Servlet,JSP 动态生成excel文件并提示下载的简单方法
- 一个jsp文件实现从数据库中下载任意类型文件
- jsp文件上传下载
- JSP制作文件上传下载全攻略~!~
- 解决JSPSMARTUPLOAD下载文件中文乱码(有实例)
- jsp如何下载FTP上的文件---ftp协议方式
- Struts2+jsp上传和下载文件
- jsp文件上传、下载
- Jsp/Servlet:实现文件上传与下载
- JSP文件下载代码示例
- jsp页面链接下载文件