小弟做了一个文件下载功能,windows环境下没问题,但是在linux环境下,在下文件,文件后带出一部分页面代码,求大神帮忙看看原因
String downpath = ServletActionContext.getServletContext().getRealPath(
"/downloads/NimbleJobPerson/");
;
String fileName = getParameter("fileName");
String downPath = downpath + "\\" + fileName;
String lSQL;
int i = fileName.lastIndexOf("-");
String bkd001 = fileName.substring(i+1,fileName.length());
if(fileName.startsWith("YB")){
lSQL = "select bkd201 as blob from kda5 where bkd001="+bkd001;
}else{
lSQL = "select bkd202 as blob from kda5 where bkd001="+bkd001;
}
List ls = DBHelper.executeList(lSQL.toString());
Map map = (Map) ls.get(0);
FileDownLoad fd = new FileDownLoad();
fd.downLoad(getRequest(), getResponse(), downPath,(Blob)map.get("blob"));public void downLoad(HttpServletRequest request, HttpServletResponse response,String filename,Blob blob) {
if(blob == null){
return;
}
try {
//设置response的编码方式
request.setCharacterEncoding("gbk");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType(CONTENT_TYPE);
response.setContentType("text/plain");
//发送到客户端的文章
response.setHeader("Content-Disposition","attachment;filename="+filename);
OutputStream myout = null;
try {
//读出文件到i/o流
InputStream fis = blob.getBinaryStream();
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//相当于我们的缓存
int k=0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
myout=response.getOutputStream();
//开始循环下载
while((k = buff.read(b,0,1024)) != -1){
//将b中的数据写到客户端的内存
myout.write(b,0,k);
}
//将写入到客户端的内存的数据,刷新到磁盘
myout.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
myout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
String downpath = ServletActionContext.getServletContext().getRealPath(
"/downloads/NimbleJobPerson/");
;
String fileName = getParameter("fileName");
String downPath = downpath + "\\" + fileName;
String lSQL;
int i = fileName.lastIndexOf("-");
String bkd001 = fileName.substring(i+1,fileName.length());
if(fileName.startsWith("YB")){
lSQL = "select bkd201 as blob from kda5 where bkd001="+bkd001;
}else{
lSQL = "select bkd202 as blob from kda5 where bkd001="+bkd001;
}
List ls = DBHelper.executeList(lSQL.toString());
Map map = (Map) ls.get(0);
FileDownLoad fd = new FileDownLoad();
fd.downLoad(getRequest(), getResponse(), downPath,(Blob)map.get("blob"));public void downLoad(HttpServletRequest request, HttpServletResponse response,String filename,Blob blob) {
if(blob == null){
return;
}
try {
//设置response的编码方式
request.setCharacterEncoding("gbk");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType(CONTENT_TYPE);
response.setContentType("text/plain");
//发送到客户端的文章
response.setHeader("Content-Disposition","attachment;filename="+filename);
OutputStream myout = null;
try {
//读出文件到i/o流
InputStream fis = blob.getBinaryStream();
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//相当于我们的缓存
int k=0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
myout=response.getOutputStream();
//开始循环下载
while((k = buff.read(b,0,1024)) != -1){
//将b中的数据写到客户端的内存
myout.write(b,0,k);
}
//将写入到客户端的内存的数据,刷新到磁盘
myout.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
myout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解决方案 »
- struts提示路径错误运行不起来
- 如何获得JSP页面已经得到的s:property value里的值
- 问一个spring mvc 中的beans的设置问题
- java虚拟内存如何限制
- tomcat 内存 溢出的问题求解 zk+spring+hibernate
- MyEclipse 6.0 add jstl WEB-INF 没有TLD文件如何解决??????
- JSF 关于导航的问题
- 有关集合的问题!Vector,Collection,ArryList,HashMap
- hibernate联合主键
- 超难问题:在struts action 中使用 common.fileupload
- 使用fineReport后tomcat内存只增不减
- 关于页面包含google的问题
response.setContentType("text/plain"); 只调用一次:
response.setContentType("application/force-download")
response.setHeader("Content-Transfer-Encoding", "binary");
或者
response.setContentType("application/octet-stream")
downPath = downpath + "\\" + fileName;
改成
downPath = downpath + "/" + fileName;也许有用