【问题标题】:downloading a file from database table(Mysql,spring)从数据库表下载文件(Mysql,spring)
【发布时间】:2012-01-20 09:55:25
【问题描述】:

我已经编写了用于下载文件的代码(数据库表中的 blob)。我在下载时收到提示,但是当我下载时它的大小会增加并作为空白文件或损坏的文件打开。控制器调用 2 个服务方法下载文件( ):- 将 blob 类型转换为字节数组。

downloadfilename() :- 获取存储在数据库中的文件名。 控制器代码

  protected ModelAndView handleRequestInternal(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            byte[] fileBytes = userService.downloadFile();
            String filename = userService.downloadFileName();

            String fileType = filename.substring(filename.indexOf(".")+1,filename.length());
            System.out.println("FILETYPE IS :>>>>>>>>>>>>>>>"+fileType);

            if (fileType.trim().equalsIgnoreCase("txt"))
            {
            response.setContentType( "text/plain" );
            }
            else if (fileType.trim().equalsIgnoreCase("doc"))
            {
            response.setContentType( "application/msword" );
            }
            else if (fileType.trim().equalsIgnoreCase("xls"))
            {
            response.setContentType( "application/vnd.ms-excel" );
            }
            else if (fileType.trim().equalsIgnoreCase("pdf"))
            {
            response.setContentType( "application/pdf" );
            }
            else if (fileType.trim().equalsIgnoreCase("ppt"))
            {
            response.setContentType( "application/ppt" );
            }
            else
            {
            response.setContentType( "application/octet-stream" );
            }

            response.setHeader("Content-Disposition","attachment; filename=\""+filename+"\"");
            response.setHeader("cache-control", "no-cache");
            response.setHeader("cache-control", "must-revalidate");

            ServletOutputStream outs = response.getOutputStream();
            outs.write(fileBytes);
            outs.flush();
            outs.close();
            return null;

        }

DAO CLASS 是这样的..

public String downloadFile(){
String selectquery = "select * from upload_file where id=1";
                System.out.println("inside after query");
                ResultSet rs = stmt.executeQuery(selectquery);
                while (rs.next()) {
                    fileBytes = rs.getBytes("description");
                    fileName = rs.getString("upload_filename");
                    System.out.println("**************************************"
                            + fileName);    
                }
                return fileName;
            } catch (SQLException s) {
                System.out.println(s);
            }
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

【问题讨论】:

    标签: java mysql spring


    【解决方案1】:

    你的问题不是很清楚,但我想我知道你的意思。

    如果文件很大 (10MB),那么您必须在 http 标头中指定内容长度。如果不是,浏览器的行为会很糟糕。 (确切的限制 10MB 取决于浏览器)

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2020-05-02
      • 1970-01-01
      相关资源
      最近更新 更多