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
"
);
}
}
查看全文
相关阅读:
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(9)各种常用辅助类
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(7)声音播放、硬件信息、键盘模拟及钩子、鼠标模拟及钩子等设备相关
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(6)全屏截图、图标获取、图片打印、页面预览截屏、图片复杂操作等
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(4)CSV、Excel、INI文件、独立存储等文件相关
DevExpress控件使用经验总结
Winform开发框架之通用自动更新模块
详解在数据查看界面中增加记录导航功能,你应该需要的
使用Aspose.Cell控件实现多个Excel文件的合并
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(8)非对称加密、BASE64加密、MD5等常用加密处理
WCF开发框架形成之旅如何实现X509证书加密
原文地址:https://www.cnblogs.com/ding0910/p/646338.html
最新文章
[翻译]开发一个自己的HTML在线编辑器(二)
温故知新ASP.NET 2.0(C#)(4) Cache&SqlCacheDependency(缓存和SqlCacheDependency特性)
乐在其中设计模式(C#) 单例模式(Singleton Pattern)
C# 2.0 除了泛型
温故知新ASP.NET 2.0(C#)(7) Profile(存储用户配置)
乐在其中设计模式(C#) 策略模式(Strategy Pattern)
温故知新ASP.NET 2.0(C#)(6) Membership&RoleManager(成员资格和角色管理)
乐在其中设计模式(C#) 状态模式(State Pattern)
[翻译]如何使用webservice作为数据源去生成Microsoft Reporting Services 2005的报表
[翻译]在asp.net 2.0中使用WebParts
热门文章
[翻译]在GridView中插入新记录
乐在其中设计模式(C#) 适配器模式(Adapter Pattern)
[翻译]通过WebService调用SQLXML(SQL Server 2005)
乐在其中设计模式(C#) 代理模式(Proxy Pattern)
温故知新ASP.NET 2.0(C#)(3) SiteMap(站点地图)
乐在其中设计模式(C#) 外观模式(Facade Pattern)
[翻译]使用asp.net 2.0中的SqlBulkCopy类批量复制数据
温故知新ASP.NET 2.0(C#)(2) Themes(主题)
乐在其中设计模式(C#) 模板方法模式(Template Method Pattern)
[翻译]在asp.net 2.0中使用母版页和工厂方法模式
Copyright © 2011-2022 走看看