文件名未从响应标头中读取

时间:2017-02-28 08:58:28

标签: java file servlets download

从BLOB列下载文件时,文件名未正确设置标题中设置的下载文件,而是采用浏览器标签标题。以下是我用于下载的代码段

public static void downloadFile(ActionResponse actionResponse,ActionRequest  actionRequest, String type){


        HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(aResponse);  
        String fileName = "";
        InputStream in = null;

        System.out.println("type:- "+type);
            Connection conn = null;
            ResultSet rs = null;
            PreparedStatement pstmt = null;


            try {   
                conn = ConnectionManager.getJNDIConnection(JNDI);

                pstmt = conn.prepareStatement(DOWNLOAD_FILE);
                pstmt.setString(1,type);
                rs = pstmt.executeQuery();

                while (rs.next()) {
                    //Getting the filename from DB
                    fileName = rs.getString("FILE_NAME");

                    httpRes.reset();
                    if (fileName != null) {                 
                        String Mimetype="application/msword";                       
                        System.out.println("fileName:-  "+fileName);                        
                        //Getting data from DataBase of type BLOB
                        in = rs.getBinaryStream("FILE_BLOB");
                        ServletResponseUtil.sendFile(httpReq, httpRes, fileName, in,5242880, Mimetype,"attachment; filename  ="+fileName);
                        in.close();
                    }else
                    {
                        break;
                    }
                }

                System.out.println("::::::::file name:::::::::>"+fileName);

            } catch (Exception e) {
                e.printStackTrace();
            } 

1 个答案:

答案 0 :(得分:0)

根据RFC6266filename标题的Content-Disposition参数的值必须是引用的字符串,即:

ServletResponseUtil.sendFile(httpReq, httpRes, fileName, in, 5242880, Mimetype, "attachment; filename=\"" + fileName + "\"");