在sql server 2000里读7M多的东西要20多秒,要向数据库写入7M多的却不需要1秒,是哪里出问题了呢?缓冲区的大小也设置过不同的数值,但效果并不明显。
以下是部分代码: Connection con = new ConnectionProvider().getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = null;
rs = stmt.executeQuery(sql);
InputStream in = rs.getBinaryStream(2);
BufferedInputStream bis = new BufferedInputStream(in, 1024);
FileOutputStream fout = new FileOutputStream( file);
BufferedOutputStream bos = new BufferedOutputStream(fout, 1024);
byte[] buffer = new byte[1024];
int count = -1;
while ((count = bis.read(buffer))!= -1) {
bos.write(buffer, 0, count);
}
以下是部分代码: Connection con = new ConnectionProvider().getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = null;
rs = stmt.executeQuery(sql);
InputStream in = rs.getBinaryStream(2);
BufferedInputStream bis = new BufferedInputStream(in, 1024);
FileOutputStream fout = new FileOutputStream( file);
BufferedOutputStream bos = new BufferedOutputStream(fout, 1024);
byte[] buffer = new byte[1024];
int count = -1;
while ((count = bis.read(buffer))!= -1) {
bos.write(buffer, 0, count);
}
解决方案 »
- java操作数据表的问题
- java.io.StreamCorruptedException: invalid stream header
- 极品菜鸟的极品问题:怎么定义一个抽象类的实体?
- 不同版本的JdK中的Class loader有什么变化
- 紧急求助:关于JDBC数据库连接池的疑问
- 关于System.in.skip( 1 )对后续的System.in.read();的读取影响,求解
- 为什么出错啊,线程,,帮忙啊,简单非常啊,
- 怎样设计并调用两个不同的paint()方法?
- 一个我想了很久都没有想出来的解决办法
- 简单的stateless sessionbean为何报如下的错误!!!!
- 一个可能有点菜的HashMap问题
- ant mail发邮件的问题
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
换成:
Statement stmt = con.createStatement();
试一下。
数据库操作的快慢和很多因素有关,比如硬件,数据库版本,驱动,网络(分布的话),sql语句,索引等好多因素。
据说使用ms提供的sqlserver驱动要比部分厂商提供的sqlserver第三方驱动慢3/1~2/1。当然我本人没测试过。
String sql = "select * from CA_VEDIO";
数据库里只有三个字段,ID,Binary,NAME,里面的数据行数也不多。
现在就是不懂为什么上传时很快,但下载很慢,而且是在本机测试的,同一时刻只有一台电脑下载。
fout.close();
bis .close();
刚试了把close()写在循环外,但性能还是没提升。
java.io.IOException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
at com.microsoft.jdbc.base.BaseInputStreamWrapper.validateClosedState(Unknown Source)
at com.microsoft.jdbc.base.BaseInputStreamWrapper.read(Unknown Source)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at java.io.FilterInputStream.read(FilterInputStream.java:90)
at caseWindow.vedioUploadAndDownload.DownloadVedio.downloadVedioFile(DownloadVedio.java:135)
at Client.CaseThread.run(CaseThread.java:29)
at java.lang.Thread.run(Thread.java:619)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;public class UpAndDown {
//数据库配置-------------------------------
private String jdbcDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url = "localhost";
private String userName = "sa";
private String password = "12345";
//-----------------------------------------
private String DbUrl = "jdbc:microsoft:sqlserver://" + url + ":1433;DataBaseName=largeFile";
private Connection conn = null;
//文件上传到数据库,String filePath:文件名(包括路径)
public void uploadFile(String filePath) throws Exception {
PreparedStatement ps = null;
FileInputStream fis = null;
File uploadFile = new File(filePath);
if(!uploadFile.exists()) {
System.out.println("文件不存在");
return;
}
Class.forName(jdbcDriver);
conn = java.sql.DriverManager.getConnection(DbUrl, userName, password);
fis = new FileInputStream(uploadFile);
ps = conn.prepareStatement("INSERT INTO myFile (largeFile) VALUES (?)");
ps.setBinaryStream(1, fis, (int)uploadFile.length());
ps.executeUpdate();
fis.close();
System.out.println("文件上传成功");
} //文件下载到本地,String path:下载目的地,带"\\"
public void downloadFile(String path) throws Exception {
PreparedStatement ps = null;
ResultSet rs = null;
File filePath = new File(path);
File downloadFile = null;
InputStream is = null;
FileOutputStream fos = null;
if(!filePath.exists()) filePath.mkdir(); Class.forName(jdbcDriver);
conn=java.sql.DriverManager.getConnection(DbUrl, userName, password);
ps = conn.prepareStatement("SELECT * FROM myFile", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery();
while(rs.next()) {
byte[] buffer = new byte[1024];
downloadFile = new File(path + rs.getString("id") + ".mpg");
if(!downloadFile.exists()) {
fos = new FileOutputStream(downloadFile);
is = rs.getBinaryStream("largeFile");
int count = 0;
while((count = is.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
} else System.out.println("文件:" + downloadFile.getName() + "已存在");
} fos.close();
is.close();
System.out.println("文件下载成功");
} //清空文件所在的数据库表,因为在企业管理器里手动删不了,需要用SQL语句删除
public void clearDB() throws Exception {
java.sql.PreparedStatement ps = null;
Class.forName(jdbcDriver);
conn=java.sql.DriverManager.getConnection(DbUrl, userName, password);
ps = conn.prepareStatement("DELETE FROM myFile");
ps.executeUpdate();
System.out.println("清空成功");
}
public static void main(String[] args) {
UpAndDown ud = new UpAndDown();
try {
ud.uploadFile("d:\\33.mpg");//上传
//ud.downloadFile("d:\\down\\");//下载,参数要带"\\"
//ud.clearDB();//清空文件所在的数据库表
} catch (Exception e) {
e.printStackTrace();
}
}
}
数据库相关,请自行添加:
数据库名:largeFile
表: [id] [int] IDENTITY (1, 1) NOT NULL primary key,
[largeFile] [image] NULL 测试代码临时写的,请莫见怪。测试时上传很快,大概1秒,但下载好慢,7M多要20多秒。
数据库名:largeFile
表名: myFile
[id] [int] IDENTITY (1, 1) NOT NULL primary key,
[largeFile] [image] NULL 上面忘记写表名了。
你这段代码有点问题。
在uploadFile()方法中,ps, conn 都没有关闭。
如果继续运行downloadFile()方法,就会出错。在读写数据库时,一定要注意释放数据库资源,否则,程序会出现意想不到的问题。