zoukankan
html css js c++ java
JSP文件下载的实现
<%
@ page import
=
"
com.jstrd.htgl.common.Htglcommon
"
%>
<%
@ page import
=
"
java.net.*
"
%>
<%
String
mineType
=
"
text/plain
"
;
String
subject
=
request.getParameter(
"
subject
"
);
String
name
=
new
String
(request.getParameter(
"
name
"
).getBytes(
"
ISO8859-1
"
));
if
( subject !
=
null
)
{
Htglcommon bigText
=
new
Htglcommon();
byte
[] bts
=
bigText.selectText(subject,name);
if
( bts !
=
null
)
{
String
ext
=
bigText.docType;
if
( ext !
=
null
)
{
mineType
=
""
;
if
(
"
pdf
"
.equalsIgnoreCase(ext))
{
mineType
=
"
application/pdf
"
;
}
else
if
(
"
xls
"
.equalsIgnoreCase(ext))
{
mineType
=
"
application/vnd.ms-excel
"
;
}
else
if
(
"
doc
"
.equalsIgnoreCase(ext))
{
mineType
=
"
application/msword
"
;
}
else
if
(
"
gif
"
.equalsIgnoreCase(ext))
{
mineType
=
"
image/gif
"
;
}
else
if
(
"
jpg
"
.equalsIgnoreCase(ext))
{
mineType
=
"
image/jpeg
"
;
}
else
if
(
"
bmp
"
.equalsIgnoreCase(ext))
{
mineType
=
"
image/x-bmp
"
;
}
else
if
(
"
png
"
.equalsIgnoreCase(ext))
{
mineType
=
"
image/png
"
;
}
else
if
(
"
ppt
"
.equalsIgnoreCase(ext))
{
mineType
=
"
application/vnd.ms-powerpoint
"
;
}
else
if
(
"
txt
"
.equalsIgnoreCase(ext))
{
mineType
=
"
text/plain
"
;
}
else
if
(
"
zip
"
.equalsIgnoreCase(ext))
{
mineType
=
"
application/zip
"
;
}
else
if
(
"
rar
"
.equalsIgnoreCase(ext))
{
mineType
=
"
application/zip
"
;
}
if
( mineType.equals(
""
) )
{
mineType
=
application.getMimeType(
"
temp.
"
+
ext);
}
response.reset();
ServletOutputStream stream
=
response.getOutputStream();
//
response.addHeader(
"
Content-Disposition
"
,
"
attachment; filename=
"
+
URLEncoder.encode(bigText.docName,
"
gb2312
"
));
response.setContentType(mineType
+
"
; charset=gb2312
"
);
stream.write(bts);
stream.flush();
stream.close();
bts
=
null
;
}
}
}
%>
package
com.jstrd.htgl.common;
import
java.util.Calendar;
import
java.util.GregorianCalendar;
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
com.exp.dao.DBConnection;
import
com.exp.dao.DBConnectionFactory;
import
com.exp.dao.EXPSQLException;
import
com.exp.dao.dialect.Dialect;
import
com.exp.dao.dialect.OracleDialect;
public
class
Htglcommon
{
public
transient
String docType
=
""
;
public
transient
String docName
=
""
;
public
Htglcommon()
{
}
/** */
/**
* 查询数据(正文)
*
*
@param
subject
* 数据对应主题
*
@return
如果数据不存在,则返回null
*
@throws
EXPSQLException
*/
public
byte
[] selectText(String subject,String strName)
throws
EXPSQLException
{
DBConnection dbConnection
=
this
.getDBConnection();
Connection con
=
dbConnection.getConnection();
PreparedStatement ps
=
null
;
ResultSet rs
=
null
;
String strString
=
""
;
try
{
Dialect dialect
=
dbConnection.getDialect();
if
(dialect
instanceof
OracleDialect)
{
ps
=
con.prepareStatement(
"
select file_body,file_name from sys_file f,flow_inst s where f.flow_inid=s.flow_inid and f.file_name=s.doc_name and f.flow_inid=? and f.file_name=?
"
);
ps.setString(
1
, subject);
ps.setString(
2
, strName);
rs
=
ps.executeQuery();
if
(rs.next())
{
byte
[] bts
=
rs.getBytes(
1
);
//
Blob blob = rs.getBytes(1);
strString
=
rs.getString(
2
);
this
.docName
=
strString;
this
.docType
=
strString.substring(strString.lastIndexOf(
"
.
"
)
+
1
);
//
byte[] bts = blob.getBytes((long) 1, (int) blob.length());
rs.close();
ps.close();
return
bts;
}
rs.close();
ps.close();
}
else
{
ps
=
con.prepareStatement(
"
select file_body,file_name from sys_file f,flow_inst s where f.flow_inid=s.flow_inid and f.file_name=s.doc_name and f.flow_inid=? and f.file_name=?
"
);
ps.setString(
1
, subject);
ps.setString(
2
, strName);
rs
=
ps.executeQuery();
if
(rs.next())
{
byte
[] bts
=
rs.getBytes(
1
);
strString
=
rs.getString(
2
);
this
.docName
=
strString;
this
.docType
=
strString.substring(strString.lastIndexOf(
"
.
"
)
+
1
);
rs.close();
ps.close();
return
bts;
}
rs.close();
ps.close();
}
return
null
;
}
catch
(SQLException sqlE)
{
throw
new
EXPSQLException(sqlE);
}
finally
{
dbConnection.close();
}
}
protected
DBConnection getDBConnection()
throws
EXPSQLException
{
return
DBConnectionFactory
.createDBConnection(
"
WorkFlow
"
);
}
}
查看全文
相关阅读:
Redis常用配置说明
Redis入门知识
分布式理论基石CAP理论
MySQL之视图
MySQL之事务控制总结
MySQL之标识列(自增长列)设置起始值与步长
LeetCode 543. Diameter of Binary Tree(两节点最长路径)
LeetCode 110. Balanced Binary Tree(平衡树)
LeetCode 104. Maximum Depth of Binary Tree(求树的高度)
LeetCode 328. Odd Even Linked List(链表元素按奇偶聚集)
原文地址:https://www.cnblogs.com/ding0910/p/646338.html
最新文章
大数据-7Linux-shell编程-2
大数据-5Linux本地yum源
大数据-4Linux安装与更新
大数据-3Linux用户管理
GPS技术与移动开发应用
手机安全卫士 适用Android 8.0
代理对象之spring注解事务控制或shiro注解权限控制的理解
Eclipsemyeclipse加载项目building workspace过久
w10查询自己电脑ip
idea安装junit插件进行junit测试
热门文章
对象转json关联属性死循环问题
net.sf.json.JSONException: There is a cycle in the hierarchy!
Unable to instantiate Action
hibernate实体配置主键生成策略
object类型转换为int类型
浏览器前台页面调试-F12
SpringBoot对日志框架的统一处理以及日志设置
Redis的持久化方式RDB和AOF
MySQL之存储过程和函数
MySQL之变量
Copyright © 2011-2022 走看看