用jsp+servlet+javabean上传图片到数据库中,不是存路径

把图片转换成二进制,然后储存到数据库中,当然也包括读取和显示图片的功能,不过在上传的功能不能写成insert应该是用update的,毕竟以后还是要换图片的。请问谁有这方面... 把图片转换成二进制,然后储存到数据库中,当然也包括读取和显示图片的功能,不过在上传的功能不能写成insert应该是用update的,毕竟以后还是要换图片的。请问谁有这方面的例子或者代码,请发给我一下,网上有很多,我也都看过了,他们都是写在页面里的,当然最好要包括图片过滤,以及限制大小等等,没有也没关系,如果发给我可以用,或不需要我改动太多代码的话我追分200 展开
 我来答
匿名用户
2013-03-30
展开全部
jsp - 上传图片到数据库中 方法如下: 第一步:建立数据库
create table test_img(id number(4),name varchar(20),img long raw);
第二步:(NewImg.html)
<html><head><title>添加图片</title></head>
<body>
添加图片<br>
<form method="post" action="insertNews.jsp">
图像ID:<input name="id" size="10">
<br>
选择图像:<input type="file" name="image">
<br>
<input type="submit" value="上传" name="submit" size="25">
<input type="reset" value="清除" name="clear" size="25">
<br>
</form>
</body></html>
第三步:插入数据库(InsertImg.jsp)
<%@ page language="java"%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //com.microsoft.jdbc.sqlserver.SQLserveDriver
String url="jdbc:oracle:thin:@lubin:1521:b2bdb"; //jdbc:microsoft:sqlserver://127.0.0.1:1433; jdbc:microsoft.sqlserver://127.0.0.1:1433
Connection con=DriverManager.getConnection(url,"demo","demo");
//插入数据库
String sql="insert into test_img values (?,?,?)";
//获取传值ID
String id=request.getParameter("id");
//获取image的路径
String kk=request.getParameter("image");
//转换成file格式
File filename=new File(kk);
//将文件的长度读出,并转换成Long型
long l1=filename.length();
int l2=(int)l1;
//以流的格式赋值
FileInputStream fis=new FileInputStream(filename);
PreparedStatement ps =con.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2,filename.getName());
ps.setBinaryStream(3,fis,l2);
//ps.setBinaryStream(3,fis,fis.available());
ps.executeUpdate();
//ps.execute();
ps.close();
fis.close();
out.println("ok!!!");
%>
第四步:显示图片(ShowImg.jsp)
<%@ page language="java" import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@lubin:1521:b2bdb";
String image_id = (String) request.getParameter("ID");
Connection con=DriverManager.getConnection(url,"demo","demo");
PreparedStatement sql=con.prepareStatement("select * from test_img WHERE id = " + image_id);
ResultSet rs = sql.executeQuery();
rs.next();
//输入img字段内容到in
InputStream in = rs.getBinaryStream("img");
//以下可是任何处理,比如向页面输出:
response.reset();
response.setContentType("image/jpeg");
byte[] b = new byte[1024];
int len;
while((len=in.read(b)) >0)
response.getOutputStream().write(b,0,len);
in.close();
rs.close();
%>
匿名用户
2013-03-30
展开全部
%@ page language="java"%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //com.microsoft.jdbc.sqlserver.SQLserveDriver
String url="jdbc:oracle:thin:@lubin:1521:b2bdb"; //jdbc:microsoft:sqlserver://127.0.0.1:1433; jdbc:microsoft.sqlserver://127.0.0.1:1433
Connection con=DriverManager.getConnection(url,"demo","demo");

String sql="insert into test_img values (?,?,?)";

String id=request.getParameter("id");
String kk=request.getParameter("image");

File filename=new File(kk);

long l1=filename.length();
int l2=(int)l1;

FileInputStream fis=new FileInputStream(filename);
PreparedStatement ps =con.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2,filename.getName());
ps.setBinaryStream(3,fis,l2);
//ps.setBinaryStream(3,fis,fis.available());
ps.executeUpdate();
//ps.execute();
ps.close();
fis.close();
out.println("ok!!!");
%>
显示图片
<%@ page language="java" import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@lubin:1521:b2bdb";
String image_id = (String) request.getParameter("ID");
Connection con=DriverManager.getConnection(url,"demo","demo");
PreparedStatement sql=con.prepareStatement("select * from test_img WHERE id = " + image_id);
ResultSet rs = sql.executeQuery();
rs.next();

InputStream in = rs.getBinaryStream("img");

response.reset();
response.setContentType("image/jpeg");
byte[] b = new byte[1024];
int len;
while((len=in.read(b)) >0)
response.getOutputStream().write(b,0,len);
in.close();
rs.close(); javabean上传实例package com.brainysoftware.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileUploadBean {

private String savePath, filepath, filename, contentType;
private Dictionary fields;

public String getFilename() {
return filename;
}

public String getFilepath() {
return filepath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

public String getContentType() {
return contentType;
}

public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null)
return null;
return (String) fields.get(fieldName);
}

private void setFilename(String s) {
if (s==null)
return;

int pos = s.indexOf("filename=\"");
if (pos != -1) {
filepath = s.substring(pos+10, s.length()-1);
// Windows浏览器发送完整的文件路径和名字
// 但Linux/Unix和Mac浏览器只发送文件名字
pos = filepath.lastIndexOf("\\");
if (pos != -1)
filename = filepath.substring(pos + 1);
else
filename = filepath;
}
}
private void setContentType(String s) {
if (s==null)
return;

int pos = s.indexOf(": ");
if (pos != -1)
contentType = s.substring(pos+2, s.length());
}

public void doUpload(HttpServletRequest request) throws IOException {
ServletInputStream in = request.getInputStream();

byte[] line = new byte[128];
int i = in.readLine(line, 0, 128);
if (i < 3)
return;
int boundaryLength = i - 2;

String boundary = new String(line, 0, boundaryLength); //-2丢弃换行字符
fields = new Hashtable();

while (i != -1) {
String newLine = new String(line, 0, i);
if (newLine.startsWith("Content-Disposition: form-data; name=\"")) {
if (newLine.indexOf("filename=\"") != -1) {
setFilename(new String(line, 0, i-2));
if (filename==null)
return;
//文件内容
i = in.readLine(line, 0, 128);
setContentType(new String(line, 0, i-2));
i = in.readLine(line, 0, 128);
//空行
i = in.readLine(line, 0, 128);
newLine = new String(line, 0, i);
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter((savePath==null? "" : savePath) + filename)));
while (i != -1 && !newLine.startsWith(boundary)) {
// 文件内容的最后一行包含换行字符
// 因此我们必须检查当前行是否是最
// 后一行
i = in.readLine(line, 0, 128);
if ((i==boundaryLength+2 || i==boundaryLength+4)
&& (new String(line, 0, i).startsWith(boundary)))
pw.print(newLine.substring(0, newLine.length()-2));
else
pw.print(newLine);
newLine = new String(line, 0, i);

}
pw.close();

}
else {
// 普通表单输入元素
// 获取输入元素名字
int pos = newLine.indexOf("name=\"");
String fieldName = newLine.substring(pos+6, newLine.length()-3);

i = in.readLine(line, 0, 128);
i = in.readLine(line, 0, 128);
newLine = new String(line, 0, i);
StringBuffer fieldValue = new StringBuffer(128);
while (i != -1 && !newLine.startsWith(boundary)) {
// 最后一行包含换行字符
// 因此我们必须检查当前行是否是最后一行
i = in.readLine(line, 0, 128);
if ((i==boundaryLength+2 || i==boundaryLength+4)
&& (new String(line, 0, i).startsWith(boundary)))
fieldValue.append(newLine.substring(0, newLine.length()-2));
else
fieldValue.append(newLine);
newLine = new String(line, 0, i);
}
fields.put(fieldName, fieldValue.toString());
}
}
i = in.readLine(line, 0, 128);

}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式