(高分求)保存图片到数据库问题

pcno1 2005-11-05 10:20:21
JSP在做上传图片时,如何将图片保存到数据库?
是不是要先将图片转化格式?
在控制过程中如何来获取上传的图片信息?文本时是request.getParatemer("..");来获取的,图片又是怎么样的呢?

...全文
320 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2005-11-10
  • 打赏
  • 举报
回复
存 图片 的路径多好呀
小粘人 2005-11-09
  • 打赏
  • 举报
回复
怎么设二进制~?
pcno1 2005-11-06
  • 打赏
  • 举报
回复
数据库的表是不是设置成二进制的?
小粘人 2005-11-05
  • 打赏
  • 举报
回复
顶下先
小粘人 2005-11-05
  • 打赏
  • 举报
回复
问楼上的,那数据库的数据类型设为什么呢~?
OnlyFor_love 2005-11-05
  • 打赏
  • 举报
回复
利用二进制去存取图片信息:
FileInputStream fis=null;
File file = new File("z.jpg");
try{
fis = new FileInputStream(file);
}
catch(FileNotFoundException e) {}
PreparedStatement ps = con.prepareStatement("insert into binary_data values (?,?,?,?,?,?)");

ps.setBinaryStream(3,fis,3098);
夕木双 2005-11-05
  • 打赏
  • 举报
回复
这是JSP应用开发技术(柳永坡等编)中的一个例子,希望对你有所帮助

package ch14.binaryFile;

import java.sql.*;
import java.io.*;
import java.nio.*;

/**
* 针对数据库字段的image和varbinary的存取
*/
public class UploadPicture {
// 定义数据库连接
private Connection conn;

/**
* 使用指定的连接构造
* @param conn 数据库连接
*/
public UploadPicture(Connection conn) {
this.conn = conn;
if (this.conn == null) //如果没有初始化连接,默认初始化
initConn();
}

/**
* 默认的数据库连接
* 当构造函数的conn为null时调用,做演示用
*/
private void initConn() {
String user = "root";
String password = "root";
String driverstr = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/sql_test";
try {
Class.forName(driverstr).newInstance();
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 保存图像文件到数据库指定的位置
* @param sqlstr 指定位置的sql语句
* @param file 指定图像文件
* @return 保存成功返回true,否则返回false
*/
public boolean storeImage(String sqlstr, File file) {
try {
//打开文件
FileInputStream fin = new FileInputStream(file);
//建一个缓冲保存数据
ByteBuffer nbf = ByteBuffer.allocate((int) file.length());
byte[] array = new byte[1024];
int offset = 0, length = 0;
//读存数据
while ((length = fin.read(array)) > 0) {
//nbf.put(array, offset, length);
if (length != 1024)
nbf.put(array, 0, length);
else
nbf.put(array);
offset += length;
}
//关闭文件
fin.close();
//新建一个数组保存要写的内容
byte[] content = nbf.array();
//保存数据
return setImage(sqlstr, content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//如果发生文件读写错误都会返回false
return false;
}

/**
* 保存字节数组到数据库指定位置
* @param sqlstr 描述位置的sql语句
* @param in 要保存的字节数组
* @return 返回是否成功保存
*/
private boolean setImage(String sqlstr, byte[] in) {
boolean flag = false;
if (sqlstr == null)
sqlstr = "select * from picture_table";
try {
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(sqlstr);
if (rs.next()) {
//rs.updateString(1, "01");
rs.updateBytes(2, in);
rs.updateRow();
}
else {
rs.moveToInsertRow();
rs.updateString(1, "01");
rs.updateBytes(2, in);
rs.insertRow();
}
rs.close();
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}

/**
* 从数据库指定位置获取图像文件
* @param sqlstr 描述数据库位置的sql语句
* @param file 图像文件保存路径
* @return 是否正确返回文件
*/
public boolean retrieveImage(String sqlstr, File file) {
boolean flag = false;
if (sqlstr == null)
sqlstr = "select content from picture_table";
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sqlstr);
while (rs.next()) {
byte[] content = rs.getBytes(1);
if (!rs.wasNull()) //找到数据
{
flag = true;
//保存到指定文件
FileOutputStream fout = new FileOutputStream(file);
fout.write(content);
;
fout.flush();
fout.close();
}
//保存第一条纪录,跳出
break;
}
//关闭连接
rs.close();
stat.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
flag = false; //如果有io错误则不成功
}
return flag;
}

//测试
public static void main(String[] args) {
String filePath = "";
UploadPicture iop = new UploadPicture(null);
try { //使用一个图片作为测试文件
filePath = FilePath.getFilePath("\\01.jpg");
File file = new File(filePath);
if (iop.storeImage(null, file))
System.out.println("true");
else
System.out.println("false");
filePath = FilePath.getFilePath("\\001.jpg");
file = new File(filePath);
if (iop.retrieveImage(null, file))
System.out.println("true");
else
System.out.println("false");
} catch (Exception e) {
e.printStackTrace();
}
}
}
pcno1 2005-11-05
  • 打赏
  • 举报
回复
数据库的数据类型是设成什么?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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