用spring,,mysql存储我的图片,但取出来时只能显示部分画面?

javazhai 2006-06-09 08:27:27
package org.zhaij;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.util.FileCopyUtils;

public class MyMain extends JdbcDaoSupport{

private static LobHandler lobHandler=new DefaultLobHandler();
static DataSource dataSource;
public MyMain()
{
ApplicationContext ctx=new FileSystemXmlApplicationContext("jdbc.xml");
dataSource=(DataSource)ctx.getBean("dataSource");
this.setDataSource(dataSource);
}
//插入数据
private static void insertBlob(JdbcTemplate jt, final int newId,
final Date firstPerformance, final InputStream is,
final int blobLength, final int showId) {
//LobHandler lobHandler;
jt.execute(
"INSERT INTO Show_Poster " +
"(id, first_performance, poster_image, REF_Show_id) " +
"VALUES (?, ?, ?, ?)",
new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException {
ps.setInt(1, newId);
ps.setDate(2, firstPerformance);
lobCreator.setBlobAsBinaryStream(ps, 3, is, blobLength);
ps.setInt(4, showId);

}

}
);
}
//
/////////
private static void streamBlob(JdbcTemplate jt, final int id, final OutputStream os)
throws DataAccessException {
jt.query(
"SELECT poster_image FROM Show_Poster WHERE id = ?",
new Object[] {new Integer(id)},
new AbstractLobStreamingResultSetExtractor() {

protected void streamData(ResultSet rs) throws SQLException, IOException, DataAccessException {
FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs, 1), os);

}

}
);
}
//
public static void main(String[] args) {
MyMain j=new MyMain();
JdbcTemplate jt = new JdbcTemplate(dataSource);
System.out.println("jt="+jt);
int newId = 2;
Date firstPerformance = new Date(System.currentTimeMillis());
File in = new File("1.jpg");
InputStream is = null;
try {
is = new FileInputStream(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//
/*
List l=jt.queryForList("select * from student");
Iterator it=l.iterator();
LinkedHashMap m;
while(it.hasNext())
{
//m=(LinkedHashMap) it.next();
System.out.println(it.next()+"\t");
}
*/
//insertBlob(jt, newId, firstPerformance, is, (int) in.length(), 2);
//
//测试:看看能不能取出数据
int id = 2;
File out = new File("copy-of-spring2004.jpg");
OutputStream os = null;
try {
os = new FileOutputStream(out);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}

streamBlob(jt, id, os);
}

}
//帮我看看那里出了问题
...全文
249 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
javazhai 2006-06-11
  • 打赏
  • 举报
回复
不会吧,mysql的blob存储容量会那么小吗?
是多大??/
beyh 2006-06-10
  • 打赏
  • 举报
回复
mysql数据库的blog字段存储容量比较小..你看看是不是因为你的图片太大.

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧