當前位置:首頁 » 操作系統 » jsp圖片資料庫

jsp圖片資料庫

發布時間: 2022-06-29 10:20:07

① JSP頁面上傳圖片到資料庫怎麼實現更簡單

這個最好是用上傳控制項上傳,拿到地址付給<img/>標簽的src,就可以了,如果你不想用那個控制項,只是要簡單看下上傳的是什麼圖片,用js獲取圖片上傳框里的圖片地址,付給<img/>標簽的src也可以

② jsp如何上傳圖片到資料庫

jsp上傳圖片到數據,在資料庫中有一種類型就是blob存儲類型,就是用於儲存二進制的。在java.sql裡面的PreparedStatment有個setBlob()方法存入資料庫,還有ResultSet里的getBlob()就是讀取,詳情你可以看JDBC Blob如何使用。

在jsp里上傳圖片很少用上述方式存儲到資料庫中,一般是將圖片上傳到伺服器項目目錄文件夾中,然後資料庫中保存該圖片文件的地址,如/item/upload/images/我上傳的圖片.jpg

③ JSP實現圖片上傳並保存到資料庫

servlet裡面有一個request.getPart()方法,通過這個文件可以獲得圖片,前提是你的servlet版本必須是3.0以上+tomcat7,具體參考以下
@WebServlet("/articleManage")
@MultipartConfig(maxFileSize = 1024 * 1024 * 10)
// 最大10MB
public class ArticleManage extends HttpServlet {
private static final long serialVersionUID = 1L;

public ArticleManage() {
super();
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
doPost(request, response);

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if (method != null) {

if (method.equals("add")) {
addArticle(request, response);
}
}
private void addArticle(HttpServletRequest request,
HttpServletResponse response) {
ArticleService service = new ArticleServiceImpl();
ArticleTypeService typeService=new ArticleTypeServiceImpl();
String content = null;
String email = null;
String tag = null;
String type=null;
try {
email = request.getParameter("email");
content = request.getParameter("content");
tag = request.getParameter("tag");
type=request.getParameter("type");
Part img = request.getPart("img");
String imgName = null;
if (img != null) {
// 設置文件路徑,寫到硬碟

String head = img.getHeader("content-disposition");
int index = head.lastIndexOf("=") + 2;
imgName = head.substring(index, head.length() - 1); // 上傳文件時的文件名
imgName.lastIndexOf(".");
String suffix = imgName.substring(imgName.lastIndexOf(".")); // 文件後綴

if (!suffix.equals(".jpeg") && !suffix.equals(".jpg")
&& !suffix.equals(".png") && !suffix.equals(".gif")
&& !suffix.equals(".bmp")) {
System.out.println("innn******************");
// 非法文件

request.setAttribute("content", content);
request.setAttribute("email", email);
request.setAttribute("tag", tag);
request.setAttribute("type", type);
request.setAttribute("errorinfo",
"*您上傳的文件不合法,只能上後綴為jpg,bmp,png,gif,jpeg的圖片");
request.getRequestDispatcher("add.jsp").forward(request,
response); // 重新導航到表單頁
return;
}

imgName = System.currentTimeMillis() + suffix;
img.write(this.getServletContext().getRealPath("/image/upload")
+ File.separator + imgName); // 寫到硬碟
}
Article msg = new Article();
msg.setContent(content);
msg.setImg("image/upload/" + imgName);
msg.setEmail(email);
msg.setKeyWord(tag);
msg.setType(typeService.querySingle(Integer.parseInt(type)));
// 從session中取User
User user = (User) request.getSession().getAttribute("user");
msg.setUser(user);
service.addMsg(msg);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
response.getWriter()
.write("<html !DOCTYPE html><body><div style='margin;auto;font-weight:bold;'><span style='font-size;17px;'>投稿成功</span>兩秒後跳轉到首頁...</body></html>");
response.setHeader("refresh", "2;url=index.jsp");

} catch (Exception e) {
e.printStackTrace();
request.setAttribute("content", content);
request.setAttribute("email", email);
request.setAttribute("tag", tag);
request.setAttribute("type", type);
request.setAttribute("errorinfo", "投稿失敗,請檢查上傳文件的大小,不能大於10MB");
try {
request.getRequestDispatcher("add.jsp").forward(request,
response);
} catch (ServletException | IOException e1) {
e1.printStackTrace();
}
return;

} finally {
try {
service.closeConnResources();
} catch (SQLException e) {
e.printStackTrace();
}
}

}

這是我前幾天寫的,有問題再問我

④ 如何添加圖片,在JSP和資料庫中

一般來說是不用將圖片直接存在資料庫中的,可以將圖片的路徑存在資料庫中,然後在JSP
頁面中利用<img
src="圖片路徑">將圖片顯示出來

⑤ 在JSP中怎樣將圖片上傳到資料庫中

到資料庫?
你可以建一個文件夾來保存上傳的圖片,
然後將圖片的文件名保存到資料庫中。
要用的時候在根據圖片的文件名到該文件夾下面去讀取顯示出來

⑥ 資料庫中照片怎麼在jsp中顯示

用JSP從資料庫中讀取圖片並顯示在網頁上:
環境mysql+tomcat:
<1>先在mysql下建立如下的table. 並insert圖像
mysql.sql文件如下:
CREATE TABLE photo (
photo_no int(6) unsigned NOT NULL auto_increment,
image blob,
PRIMARY KEY (`photo_no`)
)

<2>把show.jsp放在tomcat的任意目錄下. show.jsp作用:從資料庫中讀出blob,並產生image/jpg.
show.jsp文件如下:
<%@ page contentType="text/html; charset=gbk" %>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.math.*"%>
<%
String photo_no = request.getParameter("photo_no");
//mysql連接
Class.forName("com.mysql.jdbc.Driver").newInstance();
String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111";
Connection con = DriverManager.getConnection(URL);

//oracle連接
//String URL="jdbc:oracle:thin@localhost:1521:orcl2";
//user="system";
//password="manager";
//Connection con = DriverManager.getConnection(URL,user,password);

try{
// 准備語句執行對象
Statement stmt = con.createStatement();
String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
Blob b = rs.getBlob("photo_image");
long size = b.length();
//out.print(size);
byte[] bs = b.getBytes(1, (int)size);
response.setContentType("image/jpeg");
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
rs.close();
}
else {
rs.close();
response.sendRedirect("./images/error.gif");
}
}
finally{
con.close();
}
%>

<3>把如下文件放在show.jsp的同一目錄下.
index.html文件如下:
<HTML>
<HEAD>
<TITLE> 圖像測試 </TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD>圖像測試</TD>
</TR>
<TR>
<TD><img src="show.jsp?photo_no=2"></TD>
</TR>
</TABLE>
</BODY>
</HTML>

⑦ jsp中 怎麼把圖片直接存入資料庫中,最好有例子

....首先..可以選擇Apache裡面的upload包....這個是把文件傳到伺服器上的上傳組件....然後是存到資料庫里....那就要看你什麼資料庫了....比如Oracle..就是Blob至Access...就是
對象
欄位....存取方法都是不一樣的..要分別對待
有個通用方法,你找到圖片文件之後,建立輸入流,然後創建bytearrayoutputstream,然後從輸入流中讀位元組到後面那個流中,並沖它裡面產生位元組數組保存到byte欄位中
其實建議你不要將圖片直接上傳到資料庫
圖片上傳到資料庫要用到
blob大對象(以oracle為例),這樣影響程序性能,你可以將圖片上傳到指定文件夾,同時將圖片保存的路徑+文件名上傳到資料庫,要顯示就讀取這個這個路徑找到圖片,然後顯示.剛做了個這個代碼

⑧ jsp中把圖片上傳到資料庫的問題

若 上傳 圖片

建議:

把圖片上傳到專門的文件夾 image

再把 圖片的路徑 image/*.jpg 用 String 存到 資料庫

到時候 圖片標簽的路徑 就放 資料庫 查出來的 路徑

圖片自動根據路徑找到相應 圖片文件 並 顯示

這樣 對 資料庫伺服器、站點伺服器 都有好處

因為 一張 圖片就有 幾十 Kb 到 幾 Mb 不等

這樣 資料庫 就 龐大 的太快

對於資料庫維護不方便(數據轉移 。。等操作)

當站點被訪問時,龐大的數據量將耗盡你站點伺服器的資源

資料庫伺服器 和 站點伺服器 很容易 崩潰

除非 只有你 訪問

⑨ jsp圖片插入資料庫並讀出頁面

2008-11-02 15:321.序
資料庫應用程序,特別是基於WEB的資料庫應用程序,常會涉及到圖片信息的存儲和顯示。
通常我們使用的方法是將所要顯示的圖片存在特定的目錄下,在資料庫中保存相應的圖片的名稱,在JSP中建立相應的數據源,利用資料庫訪問技術處理圖片信息。但是,如果我們想動態的顯示圖片,上述方法就不能滿足需要了。我們必須把圖片存入資料庫,然後通過編程動態地顯示我們需要的圖片。實際操作中,可以利用JSP的編程模式來實現圖片的資料庫存儲和顯示。

2. 建立後台資料庫

if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[p]
GO
CREATE TABLE [dbo].[p] (

[picid] [int] IDENTITY (1, 1) NOT NULL ,

[picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,

[pic] [image] NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

3.向資料庫存儲二進制圖片
啟動Dreamweaver MX後,新建一個JSP文件。其代碼如下所示。
<%@ page contentType="text/html;charset=gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'InputImage.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="testimage.jsp" method="POST"><br>
題目<input name="picname" type="text"><br>
圖片<input name="pic" type="file"><br>
<input type="Submit" name="button1" value="提交"><br>
</form>
</body>
</html>

將此文件保存為InputImage.jsp文件,其中testimage.jsp文件是用來將圖片數據存入資料庫的,具體代碼如下所示:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'testimage.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("gb2312");
//建立Statement對象
String picname=request.getParameter("picname");
String pic=request.getParameter("pic");
//獲得所要顯示圖片的標題、存儲路徑、內容,並進行中文編碼
FileInputStream str=new FileInputStream(pic);
String sql="insert into p(picname,pic) values(?,?)";
PreparedStatement pstmt=conn.getPreparedStatement(sql);
pstmt.setString(1,picname);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
//將數據存入資料庫
out.println("Success,You Have Insert an Image Successfully");
%>
</body>
</html>

4. 網頁中動態顯示圖片
接下來我們要編程從資料庫中取出圖片,其代碼如下所示。

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'testimageout.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
int id= Integer.parseInt(request.getParameter("picid"));
String sql = "select pic from p WHERE picid="+id;
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
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();
}
%>
</body>
</html>

將此文件保存為testimageout.jsp文件。下一步要做的工作就是使用HTML標記:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'lookpic.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String sql = "select * from p";
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
%>
<ccid_file values="testimageout" % />" width="100" height="100">
<br>
<%
}
rs.close();
%>
</body>
</html>

版權歸原版所有!!!

熱點內容
sqlserver執行時間 發布:2024-05-05 11:50:31 瀏覽:648
終端配置賬號該如何改密碼 發布:2024-05-05 11:24:37 瀏覽:824
成都存儲研發招聘 發布:2024-05-05 11:24:29 瀏覽:284
電腦伺服器名稱怎麼查找 發布:2024-05-05 10:49:37 瀏覽:470
電腦連到代理伺服器 發布:2024-05-05 10:40:02 瀏覽:250
華為安卓手機如何投屏到雷克薩斯 發布:2024-05-05 10:37:50 瀏覽:211
微博上傳原圖 發布:2024-05-05 10:20:05 瀏覽:749
伺服器換電腦需要什麼東西 發布:2024-05-05 09:52:28 瀏覽:754
老算盤演算法 發布:2024-05-05 09:43:10 瀏覽:841
ps存儲顯示不含通道 發布:2024-05-05 09:32:35 瀏覽:103