java 通用文件下载 excel,pdf,word,jpg,exe,rar
2009-07-09 17:23
453 查看
<%@ page contentType="text/html; charset=GBK" %>
<%@ page autoFlush="false" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%!
public static String getcontentType(String pextFileName) //获得扩展名 by gisoracle {
String pext=pextFileName.toLowerCase();
if (pext.equals(".xls"))
{
return "application/vnd.ms-excel";
}
if (pext.equals(".doc"))
{
return "application/msword";
}
if (pext.equals(".txt"))
{
return "text/plain";
}
if (pext.equals(".pdf"))
{
return "application/pdf";
}
if (pext.equals(".jpg")||pext.equals(".jpeg"))
{
return "image/jpeg";
}
if (pext.equals(".ppt"))
{
return "application/vnd.ms-powerpoint";
}
if (pext.equals(".gif"))
{
return "image/gif";
}
return "text/html";
}
%>
<%
String filename =request.getParameter("filename");
File downFile = new File(filename);
String fileext=FileOper.getFiletype(downFile);
System.out.println("fileext="+fileext);
String contentType1 =getcontentType(fileext);
System.out.println("contentType1="+contentType1);
try {
out.clear();
response.setContentType(contentType1);
filename = new String(filename.getBytes("GBK"), "ISO-8859-1");
response.setHeader("Content-Disposition","attachment; filename=" +downFile.getName());
response.addHeader("Cache-Control", "no-cache");
InputStream blobStream = new FileInputStream(downFile);
ServletOutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[10 * 1024];
int nbytes = 0;
while ( (nbytes = blobStream.read(buffer)) != -1) {
outStream.write(buffer, 0, nbytes);
}
outStream.flush();
outStream.close();
blobStream.close();
}
catch (Exception e) {
System.out.println(e);
}
%>
<%@ page autoFlush="false" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%!
public static String getcontentType(String pextFileName) //获得扩展名 by gisoracle {
String pext=pextFileName.toLowerCase();
if (pext.equals(".xls"))
{
return "application/vnd.ms-excel";
}
if (pext.equals(".doc"))
{
return "application/msword";
}
if (pext.equals(".txt"))
{
return "text/plain";
}
if (pext.equals(".pdf"))
{
return "application/pdf";
}
if (pext.equals(".jpg")||pext.equals(".jpeg"))
{
return "image/jpeg";
}
if (pext.equals(".ppt"))
{
return "application/vnd.ms-powerpoint";
}
if (pext.equals(".gif"))
{
return "image/gif";
}
return "text/html";
}
%>
<%
String filename =request.getParameter("filename");
File downFile = new File(filename);
String fileext=FileOper.getFiletype(downFile);
System.out.println("fileext="+fileext);
String contentType1 =getcontentType(fileext);
System.out.println("contentType1="+contentType1);
try {
out.clear();
response.setContentType(contentType1);
filename = new String(filename.getBytes("GBK"), "ISO-8859-1");
response.setHeader("Content-Disposition","attachment; filename=" +downFile.getName());
response.addHeader("Cache-Control", "no-cache");
InputStream blobStream = new FileInputStream(downFile);
ServletOutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[10 * 1024];
int nbytes = 0;
while ( (nbytes = blobStream.read(buffer)) != -1) {
outStream.write(buffer, 0, nbytes);
}
outStream.flush();
outStream.close();
blobStream.close();
}
catch (Exception e) {
System.out.println(e);
}
%>
相关文章推荐
- java 通用文件下载 excel,pdf,word,jpg,exe,rar
- java 通用文件下载 excel,pdf,word,jpg
- asp.net 文件下载(txt,rar,pdf,word,excel,ppt)
- JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
- 使用java将多种类型的文件如Word、PDF、JPG汇总到一个文档中(Word或者PDF)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- 综述:本专栏将介绍以下内容(专栏:Java代码操作word、excel、pdf等文件)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java利用jacob实现word,ppt,excel,jpg转pdf
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java实现word,ppt,excel,jpg转pdf
- java文本、表格word转换生成PDF加密文件代码下载
- JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
- java实现将文件(包含word,excel,图片等)转PDF
- JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
- JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例