为什么我的DownloadServlet类下载zip文件而不是xlsx文件

时间:2019-05-26 02:37:43

标签: java jsp servlets download

我正在尝试使用带有http:// ***** / download的DownloadServlet类下载xlsx文件?file_id = 1&file_type = xlsx 但该文件在chrome中以.zip文件下载 没有扩展名的Firefox

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String file_id = request.getParameter("file_id");
    String filetype = request.getParameter("file_type");
    String fileName = UserDao.getFile(file_id);
    if (filetype == "xlsx") {
     filetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }

    response.setContentType(filetype);


    logger.debug(filetype);
    // Make sure to show the download dialog
    response.setHeader("Content-disposition",
            String.format("attachment; filename=\"%s\"", Excelwriter.FNWOPath));

    File my_file = new File(fileName);
    OutputStream out = response.getOutputStream();
    FileInputStream in = new FileInputStream(my_file);
    byte[] buffer = new byte[4096];
    int length;
    while ((length = in.read(buffer)) > 0){
       out.write(buffer, 0, length);
    }
    in.close();
    out.flush();
}

1 个答案:

答案 0 :(得分:0)

我只是忘记将Excelwriter.FNWOPath更改为fileName

相关问题