import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*; public class InsertBlob extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
try{
setFileName(req.getParameter("picture"));
insertBlob();
}catch(Exception e){
e.printStackTrace();
}
}
public void setFileName(String fname){
this.filename = fname;
}
public String getFileName(){
return this.filename;
}
public void setName(String aname){
this.thename = aname;
}
public String getName(){
return this.thename;
}
public void insertBlob() throws Exception, IOException{
InputStream is = null;
FileInputStream fis = null;
try {
try{
fis = new FileInputStream(getFileName());
is = new BufferedInputStream(fis,102400);
System.out.println(fis);
} catch(Exception e) {
System.out.println("IO "+e.getMessage());
e.printStackTrace();
}
try {
String url = "jdbc:odbc:dbf";
String driver = "jdbc:odbc:JdbcOdbcDriver";
String userid = null;
String password = null;
Connection con = null;
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url, userid, password); String sql = "INSERT INTO tablename(picture,name) VALUES(?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setBinaryStream (1, is, is.available());
pstmt.setInt(2,getName());
pstmt.executeUpdate();
pstmt.close();
is.close();
fis.close();
} catch (Exception e) {
System.out.println("SQLStatement "+e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//属性
private String filename;
private String thename;
}//前面get/set方法可不用,没时间改了,不好意思
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*; public class InsertBlob extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
try{
setFileName(req.getParameter("picture"));
insertBlob();
}catch(Exception e){
e.printStackTrace();
}
}
public void setFileName(String fname){
this.filename = fname;
}
public String getFileName(){
return this.filename;
}
public void setName(String aname){
this.thename = aname;
}
public String getName(){
return this.thename;
}
public void insertBlob() throws Exception, IOException{
InputStream is = null;
FileInputStream fis = null;
try {
try{
fis = new FileInputStream(getFileName());
is = new BufferedInputStream(fis,102400);
System.out.println(fis);
} catch(Exception e) {
System.out.println("IO "+e.getMessage());
e.printStackTrace();
}
try {
String url = "jdbc:odbc:dbf";
String driver = "jdbc:odbc:JdbcOdbcDriver";
String userid = null;
String password = null;
Connection con = null;
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url, userid, password); String sql = "INSERT INTO tablename(picture,name) VALUES(?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setBinaryStream (1, is, is.available());
pstmt.setInt(2,getName());
pstmt.executeUpdate();
pstmt.close();
is.close();
fis.close();
} catch (Exception e) {
System.out.println("SQLStatement "+e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//属性
private String filename;
private String thename;
}//前面get/set方法可不用,没时间改了,不好意思
然后放在binary类型中嘛
1. 把图片, 以binray格式, 原原本本的存入.
2. 只存入图片的存储位置, 如filename 或 internet上的url.
问题是在於目标数据库对, 极大的binary的图片支援情况而定, 试想想100张100K
的jpg便要 100x100K = 10M了, (假设不需要先把jpg化作bmp来存.)
这些图若无做任何优化的处理, 都会对数据库的运作有负面的影响.