我用jspsmartupload组件完成文件下载,代码如下:
do_download.jsp
<%@ page contentType="text/html;charset=gb2312" import="com.jspsmart.upload.*"%><%
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setContentDisposition(null);
String id=request.getParameter("id");
if(id.equals("1")){
su.downloadFile("/download/bmgf.doc");
}else{
String destname="大学教程.doc";
destname=new String(destname.getBytes("GB2312"),"ISO-8859-1");
su.downloadFile("/download/大学教程.doc","text/html",destname);
}
%>
但是,点击下载的时候,迅雷上的另存为一栏写的文件名是do_download.jsp,这是怎么回事啊?希望各位指点帮忙!
恩,我下了一个可以支持中文的jspsmartupload的jar包,中文可以支持,只有上面这个问题了,谢谢大家,帮忙了!
do_download.jsp
<%@ page contentType="text/html;charset=gb2312" import="com.jspsmart.upload.*"%><%
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setContentDisposition(null);
String id=request.getParameter("id");
if(id.equals("1")){
su.downloadFile("/download/bmgf.doc");
}else{
String destname="大学教程.doc";
destname=new String(destname.getBytes("GB2312"),"ISO-8859-1");
su.downloadFile("/download/大学教程.doc","text/html",destname);
}
%>
但是,点击下载的时候,迅雷上的另存为一栏写的文件名是do_download.jsp,这是怎么回事啊?希望各位指点帮忙!
恩,我下了一个可以支持中文的jspsmartupload的jar包,中文可以支持,只有上面这个问题了,谢谢大家,帮忙了!
<%@page language="java" import="java.io.*,java.net.*"contentType="application/x-msdownload" pageEncoding="utf-8"%>
<%
//关于文件下载时采用文件流输出的方式处理:
//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
application.getRealPath("/main/mvplayer/CapSetup.msi");//获取的物理路径
String str=new String(request.getParameter("filename"));
str=new String(str.getBytes("iso8859-1"),"gb2312");
//byte tempB[]=str.getBytes("ISO-8859-1");
// String filenamedisplay=new String(tempB);
//new String(str.getBytes("ISO-8859-1"), "utf-8 ");
String filedisplay =str; String filedownload =request.getRealPath("/")+"/upload/"+filedisplay ;
filedisplay = java.net.URLEncoder.encode(filedisplay,"gb2312");
response.addHeader("Content-Disposition","attachment;filename=" + new String(filedisplay.getBytes("gb2312"),"iso8859-1"));
java.io.OutputStream outp = null;
java.io.FileInputStream in = null;
try
{
in = new FileInputStream(filedownload);
outp = response.getOutputStream();
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
//
outp.flush();
//要加以下两句话,否则会报错
//java.lang.IllegalStateException: getOutputStream() has already been called for //this response
out.clear();
out = pageContext.pushBody();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in != null)
{
in.close();
in = null;
}
//这里不能关闭
//if(outp != null)
//{
//outp.close();
//outp = null;
//}
}
%> 错误我是找不到了错误时:
<%@page language="java" import="java.io.*,java.net.*"contentType="application/x-msdownload" pageEncoding="utf-8"%>
<%
//关于文件下载时采用文件流输出的方式处理:
//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
application.getRealPath("/main/mvplayer/CapSetup.msi");//获取的物理路径
String str=new String(request.getParameter("filename"));
str=new String(str.getBytes("iso8859-1"),"gb2312");
//byte tempB[]=str.getBytes("ISO-8859-1");
// String filenamedisplay=new String(tempB);
//new String(str.getBytes("ISO-8859-1"), "utf-8 ");
String filedisplay =str; String filedownload =request.getRealPath("/")+"/upload/"+filedisplay ;
filedisplay = java.net.URLEncoder.encode(filedisplay,"gb2312");
response.addHeader("Content-Disposition","attachment;filename=" + new String(filedisplay.getBytes("gb2312"),"iso8859-1"));
java.io.OutputStream outp = null;
java.io.FileInputStream in = null;
try
{
in = new FileInputStream(filedownload);
outp = response.getOutputStream();
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
//
outp.flush();
//要加以下两句话,否则会报错
//java.lang.IllegalStateException: getOutputStream() has already been called for //this response
out.clear();
out = pageContext.pushBody();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(in != null)
{
in.close();
in = null;
}
//这里不能关闭
//if(outp != null)
//{
//outp.close();
//outp = null;
//}
}
%>
还有就是每次都会自动启动迅雷下载,而且名字不是原来的名字是download(n).doc.应为我下载的doc的文档,不知道有没有人帮帮忙啊
fileName 是下载的全路径文件名。 而newFileName则是弹出窗口 另存为时显示的文件名