首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java文件下载,鄙人载对话框中没有显示自己给定的文件名

2011-11-29 
java文件下载,在下载对话框中没有显示自己给定的文件名本人写了一个下载文件的类,下载文件时,文件能够被下

java文件下载,在下载对话框中没有显示自己给定的文件名
本人写了一个下载文件的类,下载文件时,文件能够被下载,但是默认的文件名不是我想要的文件名:

public   class   DownloadCSVFileAction   extends   Action{
        public   ActionForward   execute(ActionMapping   mapping,   ActionForm   form,   HttpServletRequest   request,   HttpServletResponse   response)   throws   Exception   {                

                try   {                        
                        String   fileName   =   request.getParameter( "fileName ");
                        long   maID   =   Long.parseLong(request.getParameter( "maID "));
                        String   filePath   =   request.getSession().getServletContext().getRealPath( "/ ")+ "csv/ "+maID+ "/ "+fileName;
                        File   fdown   =   new   File(filePath);
                        int   filelength   =   Integer.parseInt(String.valueOf(fdown.length()));
                     
                        response.setContentType( "application/text;charset=GB2312 ");
                        response.setHeader( "Content-Dispositon ", "attachment;filename=销售详细记录.csv ");     //销售详细记录.csv是我想要的下载文件的文件名,但是下载对话框中显示的是   downLoadCSVFile.do
                       
                        response.setContentLength(filelength);
                        byte   b[]=new   byte[filelength];
                        FileInputStream   fi=new   FileInputStream(fdown);                      
                       
                        OutputStream   o=response.getOutputStream();
                        int   n   =   0;
                        while((n=fi.read(b))!=-1)   {
                                o.write(b,0,n);
                        }
                        fi.close();
                        o.close();
                        return   null;
                }catch   (Exception   e)   {


                        request.setAttribute( "ERROR ",   e);
                        return   mapping.findForward( "error ");
                }

        }
}

[解决办法]
试试下面的写法
response.setHeader( "Content-disposition ", "attachment;filename=\ " "+new String(销售详细记录.csv.getBytes( "GB2312 "), "ISO8859_1 ")+ "\ " ")
[解决办法]
我以前都是用的servlet
下面是我下载pdf文档的一段代码,希望对你有帮助,
----------------------------------------------------
import ...........

public class OutPutDataAction extends HttpServlet{
............

public void doPost(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException{

//设置类型为pdf
response.setContentType( "application/pdf;charset=gb2312 ");

//Function.randomFileName()生成一个随机的文件名,如:13059853263
//最好不要为中文
String filepath = F:/pdffile/ "+Function.randomFileName()+ ".pdf ";

//这里设置下载框中的文件名
String title= "下载的文件名 ";

response.setHeader( "Content-disposition ", "attachment; filename= "

+new String( title.getBytes( "GBK "), "ISO8859_1 " )+ ".pdf ");

//---------------------------------
//下面建立pdf文档
Document document = new Document(PageSize.A4, 36,36,36,36);

java.io.FileOutputStream output = new java.io.FileOutputStream(filepath);

try{

PdfWriter writer = PdfWriter.getInstance(document, output);

document.addTitle(title);

document.addAuthor( "SWOKY ");

document.addSubject( "This example explains "+title);

document.addKeywords( "jishou university,swoky ");

document.addCreator( "Powder by :SWOKY ");

document.open();

Image img = Image.getInstance(SystemProperty.get( "WEBROOT ")+ "/jsp/file/temp.jpg ");

img.setAlignment(Image.ALIGN_CENTER);

document.add(img);

document.close();
------------------------
//以上为在服务器由filepath所指向的路径建立一个pdf文档

//downloadFile()为将刚才建立的文档输出到客户端
downloadFile(filepath,response);

}catch (DocumentException de){
//异常
}

}

private void downloadFile(String filepath,HttpServletResponse response) throws java.io.IOException {

java.io.BufferedInputStream bis =

new java.io.BufferedInputStream(

new java.io.FileInputStream(new String( filepath.getBytes( "GBK "), "ISO8859_1 " )));

javax.servlet.ServletOutputStream bos = response.getOutputStream();

byte[] buff = new byte[2048];

int bytesRead;


while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff,0,bytesRead);

}

bos.flush();

bis.close();

bos.close();

//timeout
response.setStatus(response.SC_OK );

response.flushBuffer();
}
..................

}
---------------------

热点排行