ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 人生中最难过的一天。Jsp 中下载文件问题!

人生中最难过的一天。Jsp 中下载文件问题!

原创 Linux操作系统 作者:279531451 时间:2009-10-10 17:44:07 0 删除 编辑

问题下载的下的文件比原始文件大,导致验证文件出错

原始代码

<%@page import="java.io.*"%>  --空格行

<% String type = request.getParameter("type");
 String gz_file_name = "/tmp/";
 if ("0".endsWith(type)) {
  String order_no = request.getParameter("order_no");
  gz_file_name = gz_file_name + order_no + ".zip";

 } else {
  String certificate_file = request.getParameter("certificate_file");
  gz_file_name = gz_file_name + certificate_file + ".zip";
 }
 File file = new File(gz_file_name);
 if (file.exists()) {
  String fileName = file.getName();
  InputStream is = new FileInputStream(file);
  //response.reset();
  OutputStream s = response.getOutputStream();
        BufferedOutputStream output   =   new   BufferedOutputStream(response.getOutputStream());  
        BufferedInputStream input   =   new   BufferedInputStream(is);     
  response.setContentType("application/octet-stream");
  //response.setContentLength((int) file.length());
  System.out.println("3333333333333");
  System.out.println("length====="+file.length());
  response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  int bytesRead = 0;
  byte[] buffer = new byte[4096];
  while ((bytesRead = input.read(buffer,   0,   4096)) != -1) {
   output.write(buffer,0,bytesRead);//
  }
  System.out.println("xdmmmmmmmmmmmmmmmmmmmmmmmsssssddddffeeff");
  output.close();
  input.close();
  is.close();
  //os.flush();
  os.close();
  response.flushBuffer();
 }
%>

 

Jsp中用流直接下载文件,整个jsp页面中的空格行都被写到流中。导致文件比原始文件大

改成如下:

<%@page import="java.io.*"%><%        String type = request.getParameter("type");  --合在在一行

         String gz_file_name = "/tmp/";

         if ("0".endsWith(type)) {

                   String order_no = request.getParameter("order_no");

                   gz_file_name = gz_file_name + order_no + ".zip";

 

         } else {

                   String certificate_file = request.getParameter("certificate_file");

                   gz_file_name = gz_file_name + certificate_file + ".zip";

         }

         File file = new File(gz_file_name);

         if (file.exists()) {

                   String fileName = file.getName();

                   InputStream is = new FileInputStream(file);

                   //response.reset();

                   OutputStream s = response.getOutputStream();

        BufferedOutputStream output   =   new   BufferedOutputStream(response.getOutputStream());  

        BufferedInputStream input   =   new   BufferedInputStream(is);               

                   response.setContentType("application/octet-stream");

                   //response.setContentLength((int) file.length());

                   System.out.println("3333333333333");

                   System.out.println("length====="+file.length());

                   response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

                   int bytesRead = 0;

                   byte[] buffer = new byte[4096];

                   while ((bytesRead = input.read(buffer,   0,   4096)) != -1) {

                            output.write(buffer,0,bytesRead);//

                   }

                   System.out.println("xdmmmmmmmmmmmmmmmmmmmmmmmsssssddddffeeff");

                   output.close();

                   input.close();

                   is.close();

                   //os.flush();

                   os.close();

                   response.flushBuffer();

         }

%>

 

这个文件折腾了我一天。哎。。难受!!!!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7551038/viewspace-616239/,如需转载,请注明出处,否则将追究法律责任。

上一篇: DBA 常问50 题
请登录后发表评论 登录
全部评论

注册时间:2009-09-01

  • 博文量
    100
  • 访问量
    153798