`
diaolanshan
  • 浏览: 173720 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

jsp下载文件

    博客分类:
  • JAVA
阅读更多
<%@ page language="java" contentType="application/bin"
    import="java.io.FileInputStream,java.io.OutputStream,java.io.IOException,java.io.File"%><%@ page import="java.net.URLEncoder"%><% 
        String exportfilepath = (String) request.getAttribute("exportfilepath");
        String exportfilename = (String) request.getAttribute("exportfilename");
        request.removeAttribute("exportfilepath");
        request.removeAttribute("exportfilename");
            
            if (exportfilepath != null && exportfilename != null) {
                response.reset();
                response.setContentType("application/bin");

                String filenamedownload = exportfilepath;
                String filenamedisplay = exportfilename;

                filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + filenamedisplay);

                OutputStream output = null;
                FileInputStream fis = null;
                try {
                    output = response.getOutputStream();
                    fis = new FileInputStream(filenamedownload);

                    byte[] b = new byte[1024];
                    int i = 0;

                    while ((i = fis.read(b)) > 0) {
                        output.write(b, 0, i);
                    }
                    output.flush();
                }catch(IOException ex){
                    ex.printStackTrace();
                }finally {
                    if (fis != null) {
                        fis.close();
                        fis = null;
                    }
                    if (output != null) {
                        output.close();
                        output = null;
                    }
                }

//                try{
//                    File thefile=new File(icalendarfilepath);
//                    if (thefile.exists()){
//                        thefile.delete();
//                    }
//                }catch(Exception e){
//                    
//                }
            }else{
                out.print("Sorry, there are some errors on the server and the application could not generate"
                            +" the ICalendar file successfully. Please contact with the administrator.");
            }
%>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics