jsp servlet 向oracle,blob字段中插入数据时,总是不能将图片插入数据库。
这是核心代码StringstrSQL="updatejxfpsetfptp=?wheregfsh='"+sh+"'andfpdm='"+fpdm+"'andfphm='"...
这是
核心代码
String strSQL =
"update jxfp set fptp=? where gfsh='" + sh + "' and fpdm ='" + fpdm +
"' and fphm = '" + fphm + "'";
System.out.println("上传图像sql="+strSQL);
dbConnect db = new dbConnect("11");
文件上传成功已经打印了 但是用plsql查实图片总是没有存进去。
Connection conn = db.jkaddgetConnection();
try {
pstmt = conn.prepareStatement(strSQL);
System.out.println(fis.available());
pstmt.setBinaryStream(1, fis,fis.available());
if (pstmt.executeUpdate() == 1) {
System.out.println("文件上传成功");
pstmt.close();
conn.commit(); 展开
核心代码
String strSQL =
"update jxfp set fptp=? where gfsh='" + sh + "' and fpdm ='" + fpdm +
"' and fphm = '" + fphm + "'";
System.out.println("上传图像sql="+strSQL);
dbConnect db = new dbConnect("11");
文件上传成功已经打印了 但是用plsql查实图片总是没有存进去。
Connection conn = db.jkaddgetConnection();
try {
pstmt = conn.prepareStatement(strSQL);
System.out.println(fis.available());
pstmt.setBinaryStream(1, fis,fis.available());
if (pstmt.executeUpdate() == 1) {
System.out.println("文件上传成功");
pstmt.close();
conn.commit(); 展开
展开全部
这是我自己写的一个ORACLE的图片上传,这里用到了一个apache的一个组件,fileupload,你自己在网上下载吧,很方便用的。
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import oracle.sql.BLOB;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* @author Lenovo-User
*
*/
public class UploadFileAction extends Action {
/* (non-Javadoc)
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@SuppressWarnings("deprecation")
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
UploadFileForm fForm = (UploadFileForm)form;
DataSource ds = this.getDataSource(request);
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
//PersonBean pb = (PersonBean)request.getSession().getAttribute(Constants.PERSON_KEY);
String strId = request.getParameter("id");
int id = 0;
if(strId != null)
id = Integer.parseInt(strId);
ResultSet rs = stmt.executeQuery("select * from user_account where id=" + id + " for update");
rs.next();
oracle.sql.BLOB blob = (BLOB) rs.getBlob(4);
OutputStream out = blob.getBinaryOutputStream();
int bufferSize = blob.getBufferSize();
InputStream in = fForm.getMyFile().getInputStream();
byte bytes[] = new byte[bufferSize];
while(in.read(bytes) != -1 ) {
out.write(bytes, 0, bufferSize);
}
in.close();
out.flush();//后来添加的
out.close();
rs.close();
stmt.close();
conn.commit();//后来添加的!必须要提交,不管是不是数据库是不是自动 提交
conn.close();
return mapping.findForward("success");
}
}
不懂的可以问我
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import oracle.sql.BLOB;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* @author Lenovo-User
*
*/
public class UploadFileAction extends Action {
/* (non-Javadoc)
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@SuppressWarnings("deprecation")
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
UploadFileForm fForm = (UploadFileForm)form;
DataSource ds = this.getDataSource(request);
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
//PersonBean pb = (PersonBean)request.getSession().getAttribute(Constants.PERSON_KEY);
String strId = request.getParameter("id");
int id = 0;
if(strId != null)
id = Integer.parseInt(strId);
ResultSet rs = stmt.executeQuery("select * from user_account where id=" + id + " for update");
rs.next();
oracle.sql.BLOB blob = (BLOB) rs.getBlob(4);
OutputStream out = blob.getBinaryOutputStream();
int bufferSize = blob.getBufferSize();
InputStream in = fForm.getMyFile().getInputStream();
byte bytes[] = new byte[bufferSize];
while(in.read(bytes) != -1 ) {
out.write(bytes, 0, bufferSize);
}
in.close();
out.flush();//后来添加的
out.close();
rs.close();
stmt.close();
conn.commit();//后来添加的!必须要提交,不管是不是数据库是不是自动 提交
conn.close();
return mapping.findForward("success");
}
}
不懂的可以问我
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询