现在想写个程序
向数据库中插入图片路径(或则插入图片也可以)
最好是插入图片的路径这样可插入任意大的图片...
请高手指点一下思路..
感激不尽
1.通过显示层向数据库中插入图片
2.在界面显示的时候是小图片(缩小过的)
3.当点击查看大图片会显示图片(原来的大小)
提供给你图片上传和显示的代码吧!希望对你有帮助
我在程序代码里贴了向Mysql数据库写入image代码的程序,可是好多人都是Java的初学者,对于这段代码,他们无法将它转换成jsp,所以我在这在写一下用jsp怎样向数据库写入图像文件。大家先在数据库建这样一张
,我下面的这些代码对任何数据库都通用,只要支持blob类型的
只要大家将连接数据库的参数改一下就可以了。
SQL> create table image(id int,content varchar(200),image blob);
如果在sqlserver2000的数据库中,可以将blob字段换为image类型,这在SqlServer2000中是新增的。
testimage.html文件内容如下:
Image File
我们在Form的action里定义了一个动作testimage.jsp,它的内容如下:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* " %>
<%@ page import= "java.util.* "%>
<%@ page import= "java.text.* "%>
<%@ page import= "java.io.* "%>
<%Class.forName( "org.gjt.mm.mysql.Driver ").newInstance();
String url= "jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1 ";
//其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改
Connection conn= DriverManager.getConnection(url);
String content=request.getParameter( "content ");
String filename=request.getParameter( "image ");
FileInputStream str=new FileInputStream(filename);
String sql= "insert into test(id,content,image) values(1,?,?) ";
PreparedStatement pstmt=dbconn.conn.prepareStatement(sql);
pstmt.setString(1,content);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
out.println( "Success,You Have Insert an Image Successfully ");
%>
下面我写一个测试image输出的例子看我们上面程序写的对不对,testimageout.jsp的内容如下:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* " %>
<%@ page import= "java.util.* "%>
<%@ page import= "java.text.* "%>
<%@ page import= "java.io.* "%>
<%Class.forName( "org.gjt.mm.mysql.Driver ").newInstance();
String url= "jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1 ";
//其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改
Connection conn= DriverManager.getConnection(url);
String sql = "select image from test where id=1 ";
Statement stmt=null;
ResultSet rs=null;
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException e){}
try {
while(rs.next()) {
res.setContentType( "image/jpeg ");
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
in.read(b);
}
sout.flush();
sout.close();
}
}
catch(Exception e){System.out.println(e);}
%>
你运行这个程序,你就会看到刚才你写入美丽的图片就会显示在你面前。怎么样,用jsp来试试。
这种
把图片写到数据库中会使数据库在短时间内容量飞涨,会影响性能的,另外一种做法将图片存上传到服务器上,
在数据库里只存放图片的路径,这是一个很好的方法。我建议大家采取后面一种方法。
我用的是2005的数据库
可是运行的时候出现异常javax.servlet.ServletException: No suitable driver
Image File
-------------------------------
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* " %>
<%@ page import= "java.util.* "%>
<%@ page import= "java.text.* "%>
<%@ page import= "java.io.* "%>
<%Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String url= "jdbc:sqlserver://localhost:1433;DatabaseName=haifei ";
String username = "sa " ;
String pass = "java ";
//其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改
Connection conn= DriverManager.getConnection(username,pass,url);
String content=request.getParameter( "content ");
String filename=request.getParameter( "image ");
FileInputStream str=new FileInputStream(filename);
String sql= "insert into [haifei].[dbo].[imageTable](id,content,tupian)values(1,?,?) ";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,content);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
out.println( "Success,You Have Insert an Image Successfully ");
%>
1.最直接最简单的,方式是把文件地址直接放到html页面的一个链接中。这样做的缺点是把文件在服务器上的路径暴露了,并且还无法对文件下载进行其它的控制(如权限)。这个就不写示例了。
2.在服务器端把文件转换成输出流,写入到response,以response把文件带到浏览器,由浏览器来提示用户是否愿意保存文件到本地。(示例如下)
<%
response.setContentType(fileminitype);
response.setHeader("Location",filename);
response.setHeader("Cache-Control", "max-age=" + cacheTime);
//filename应该是编码后的(utf-8)
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
response.setContentLength(filelength);
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(filepath);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
inputStream.close();
outputStream = null;
%>
3.既然是JSP的话,还有一种方式就是用Applet来实现文件的下载。不过客户首先得信任你的这个Applet小程序,由这个程序来接受由servlet发送来的数据流,并写入到本地。
servlet端示例
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ");
OutputStream outputStream = null;
try {
outputStream = res.getOutputStream();
//把文件路径为srcFile的文件写入outputStream中
popFile(srcFile, outputStream)) ;
} catch (IOException e) {
e.printStackTrace();
}
}
JApplet端示例
URLConnection con;
try {
//url是被调用的SERVLET的网址 如 *.do
con = url.openConnection();
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type",
"application/octet-stream");
InputStream in = con.getInputStream();
ProgressMonitorInputStream pmInputStream = new ProgressMonitorInputStream
(pane, "正在从服务器下载文件内容", in);
ProgressMonitor pMonitor = pmInputStream.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(3);
pMonitor.setMillisToPopup(3);
//localfilepath本地路径,localstr文件文件夹,filename本地文件名
String localfilepath = localstr + filename ;
//方法saveFilsaveFilee是把输入流pmInputStream写到文件localfilepath中
if(saveFilsaveFilee(localfilepath,pmInputStream)){
openLocalFile(localfilepath);
}
4.顺便把JApplet上传文件的代码也贴上来.
JApplet端示例
URLConnection con;
try {
con = url.openConnection();
//url是被调用的SERVLET的网址 如 *.do
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type","application/octet-stream");
OutputStream out = con.getOutputStream();
//localfilepath本地路径,localstr文件文件夹,filename本地文件名
String localfilepath = localstr + filename;
//文件getOutputStream是把文件localfilepath写到输出流out中
getOutputStream(localfilepath,out);
InputStream in = con.getInputStream();
return true;
}catch (IOException e) {
System.out.println("文件上传出错!");
e.printStackTrace();
}
servlet端代码示例
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(" text/plain ");
InputStream inputStream = null;
try {
inputStream = res.getInputStream();
//把输入流inputStream保存到文件路径为srcFile的文件中
writefile(srcFile, inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} // end service