excel无法通过jsp打开下载的文件

问题描述:

这是在浏览器上下载 excel 文件的代码,但是当我打开它时,excel 会抛出错误Excel 无法打开文件 'Location.xlsx',因为文件格式或文件扩展名无效.请验证该文件是否具有没有被损坏并且文件扩展名与文件格式匹配"但是当我简单地创建这个文件时,当我执行下载操作并且我正在使用 poi jar 来创建 excel 文件时,它的打开只会出现这个错误......???

this is the code for download excel file on browser but when i open it excel throw a error "Excel can not open the file 'Location.xlsx' because the file format or file extension is not valid. verify that the file has not been corrupted and that the file extension matches the format of the file" but when i simply create this file its opening only this error is coming when i perform download operation and i am using poi jar for excel file creation...???

<html>

    <body>
        <%
   LocationDownload.downloadLocation();
%>
<% 
  String filename = "Location.xlsx";   
  String filepath = "C:\\Users\\dsingh\\GlassFish_Server\\glassfish\\domains\\domain1\\config\\";   
  response.setContentType("application/vnd.ms-excel");   
  response.setHeader("Content-Disposition","attachment; filename=" + filename);   

  java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename);  

  int i;   
  while ((i=fileInputStream.read()) != -1) {  
    out.write(i);   
   }   
  fileInputStream.close();   
%>   

    </body>
</html>

首先将您的 File Download 编码移动到 Servlet(不要在 JSP 文件中进行)..

First move your File Download coding to Servlet (don't do inside JSP file)..

检查此代码它会有所帮助..它与我一起工作的目的与您的要求相同.

Check this code it will help.. It is working with me for the same purpose as u require.

package edu.zukrah.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class FileDownload
 */
@WebServlet("/FileDownload")
public class FileDownload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         File f = new File ("E:/pdftest/" + request.getParameter("file") );
         //set the content type(can be excel/word/powerpoint etc..)

         String type = request.getParameter("type");
         response.setContentType (type);

         //set the header and also the Name by which user will be prompted to save
         response.setHeader ("Content-Disposition", "attachment; filename=\""+request.getParameter("file")+"\"");

         //get the file name
         String name = f.getName().substring(f.getName().lastIndexOf("/") + 1, f.getName().length());

         //OPen an input stream to the file and post the file contents thru the 
         //servlet output stream to the client m/c
          InputStream in = new FileInputStream(f);

        try{
              ServletOutputStream outs = response.getOutputStream();

              int bytesRead;
              byte[] buf = new byte[4 * 1024]; // 4K buffer

              try {
                   while ((bytesRead = in.read(buf)) != -1){ 
                       outs.write(buf, 0, bytesRead);
                   }

              } catch (IOException ioe) {
                  ioe.printStackTrace(System.out);
              }

              System.out.printf(name + ",  %.2f kbs downloaded, %.2f mbs downloaded   \n", 
                                (f.length()/((double)(1024))), 
                                (f.length()/((double)(1024*1024))));

              outs.flush();
              outs.close();
              in.close(); 
        }catch(Exception e){
            e.printStackTrace();
        }
    }


}