使用jdbc 下载 mysql的blob字段中存储的大文件总是不能下载成功!请问怎么优化?

使用jdbc 下载 mysql的blob字段中存储的大文件总是不能下载成功!请问怎么优化?,第1张

其实比较好奇的是为什么要用数据库存大文件。读取文件需要读取到内存中然后进行 *** 作,如果过大 可能出现 未读到头就没有内存了的情况。其次还有可能有响应时间的问题。如果超时也有可能出现读取不成功的情况。

建议按照长度 一次读取 部分内容 并写在文件中 最后返回这个文件。也许可以折中处理

如果要读出BLOB数据,首先你要知道这个字段里存的是哪种流,是图片,还是文件,这样你读取输出以后也知道文件类型。

ResultSet里面有getBlob方法,你可以查下API

文件保存到数据库或文件,数据库只是记录路径,当然是数据库记录路径好了。

你想想把,如果传一个电影2G,到你数据库一存,会有什么后果呢。

PHP下载代码:

<?php

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT')

header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT')

header('Cache-Control: no-cache, must-revalidate')

header('Pragma: no-cache')

header('Content-Encoding: none')

header('Content-Length: '.$filesize)

header('Content-Disposition: attachmentfilename='.$attach['filename'])

header('Content-Type: '.$attach['filetype'])

@$fp = fopen($filename, 'rb')

@flock($fp, 2)

$attachment = @fread($fp, $filesize)

@fclose($fp)

echo $attachment

?>

此实现为用java访问mysql的blob,对图片进行存取

/**

* Title: BlobPros.java

* Project: test

* Description: 把图片存入mysql中的blob字段,并取出

* Call Module: mtools数据库中的tmp表

* File: C:downloadsluozsh.jpg

* Copyright:Copyright (c) 2003-2003

* Company: uniware

* Create Date: 2002.12.5

* @Author: ChenQH

* @version 1.0 版本*

*

* Revision history

* Name Date Description

* ---- ---------------

* Chenqh 2003.12.5对图片进行存取

*

* note: 要把数据库中的Blob字段设为longblob

*

*/

//package com.uniware

import java.io.*

import java.util.*

import java.sql.*

public class BlobPros

{

private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true"

private Connection conn = null

private PreparedStatement pstmt = null

private ResultSet rs = null

private File file = null

public BlobPros()

{

}

/**

* 向数据库中插入一个新的BLOB对象(图片)

* @param infile 要输入的数据文件

* @throws java.lang.Exception

*/

public void blobInsert(String infile) throws Exception

{

FileInputStream fis = null

try

{

Class.forName("org.gjt.mm.mysql.Driver").newInstance()

conn = DriverManager.getConnection(URL)

file = new File(infile)

fis = new FileInputStream(file)

//InputStream fis = new FileInputStream(infile)

pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)")

pstmt.setString(1,file.getName()) //把传过来的第一个参数设为文件名

//pstmt.setBinaryStream(2,fis,(int)file.length()) //这种方法原理上会丢数据,因为file.length()返回的是long型

pstmt.setBinaryStream(2,fis,fis.available()) //第二个参数为文件的内容

pstmt.executeUpdate()

}

catch(Exception ex)

{

System.out.println("[blobInsert error : ]" + ex.toString())

}

finally

{

//关闭所打开的对像//

pstmt.close()

fis.close()

conn.close()

}

}

/**

* 从数据库中读出BLOB对象

* @param outfile 输出的数据文件

* @param picID 要取的图片在数据库中的ID

* @throws java.lang.Exception

*/

public void blobRead(String outfile,int picID) throws Exception

{

FileOutputStream fos = null

InputStream is = null

byte[] Buffer = new byte[4096]

try

{

Class.forName("org.gjt.mm.mysql.Driver").newInstance()

conn = DriverManager.getConnection(URL)

pstmt = conn.prepareStatement("select pic from tmp where id=?")

pstmt.setInt(1,picID)//传入要取的图片的ID

rs = pstmt.executeQuery()

rs.next()

file = new File(outfile)

if(!file.exists())

{

file.createNewFile()//如果文件不存在,则创建

}

fos = new FileOutputStream(file)

is = rs.getBinaryStream("pic")

int size = 0

/* while(size != -1)

{

size = is.read(Buffer) //从数据库中一段一段的读出数据

//System.out.println(size)

if(size != -1)//-1表示读到了文件末

fos.write(Buffer,0,size)

} */

while((size = is.read(Buffer)) != -1)

{

//System.out.println(size)

fos.write(Buffer,0,size)

}

}

catch(Exception e)

{

System.out.println("[OutPutFile error : ]" + e.getMessage())

}

finally

{

//关闭用到的资源

fos.close()

rs.close()

pstmt.close()

conn.close()

}

}

public static void main(String[] args)

{

try

{

BlobPros blob = new BlobPros()

//blob.blobInsert("C:Downloadsluozsh1.jpg")

blob.blobRead("c:/downloads/1.jpg",47)

}

catch(Exception e)

{

System.out.println("[Main func error: ]" + e.getMessage())

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8318484.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-15
下一篇 2023-04-15

发表评论

登录后才能评论

评论列表(0条)

保存