jspmysql上傳圖片
一般來說有兩種形式
1.file上傳到伺服器,然後將地址保存到資料庫。
2.將文件改為base64格式的字元串,將字元串存到資料庫。
2. jsp怎麼實現上傳照片已經在網頁上查看照片
java從資料庫讀取圖片的信息轎歲,一般是圖片的臘槐名稱,然後在網頁上綁定一下圖片的路徑即可顯示,綁定在img標簽的src屬性裡面。不會就網路百輪帆友度,這種東西很小兒科的。
3. jsp如何上傳照片到mysql,再查詢在jsp中顯示
1 mysql存儲大容量的二進制文件的格式是longblob ,其實除了圖片還可以存別的
CREATE TABLE `abc`.`images` (
`name` varchar(10) NOT NULL,
`chang` int(10) unsigned NOT NULL,
`content` longblob NOT NULL,
PRIMARY KEY (`name`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
2 要向資料庫存儲二進制的文件一定要把要存儲的數據轉換成二進制流
廢話就不多說了,大家看看代碼很容易明白,先來看一個app程序,當然首先您要在資料庫中先建立一個用於保存圖片的表和相應的列,
數據格式為blob
package com.lizhe;
import java.io.*;
import java.sql.*;
public class PutImg {
public void putimg() {
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
//stmt.execute("insert into imgt (id) values (5)");
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
File file = new File("c:\\blog.jpg");
InputStream photoStream = new FileInputStream(file);
//sql = " UPDATE imgt SET img = ? ";
sql = "INSERT INTO imgtable (img) VALUES (?)";
pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, photoStream, (int) file.length());
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]){
PutImg pi=new PutImg();
pi.putimg();
}
}
InputStream photoStream = new FileInputStream(file);
可以很清楚的看到我們首先把一個圖片文件(當然也可以是別的什麼文件)轉換成了一個二進制輸慎和型入流
pstmt.setBinaryStream(1, photoStream, (int) file.length());
這個方法建議大家去查一下API文檔,第一個參數是通配符位置沒的說,第二個參數是流,這和以往的string類型的參數不太一樣,
我剛看到的時候也覺得豁然開朗了,但是到這里還沒完,不同於以往的字元串寬猜參數,這里我們還需要第三個參數來棚祥設置這個流的長度,
這里也就是這個文件的長度,導出資料庫中的sql,一切都清楚了
INSERT INTO `m_diy` VALUES (2,?\0 JFIF\0 \0H\0H\0\0?? Exif\0\0MM\0*\0\0\0 \0 \0 \0\0\0 \0 \0\0 \0 \0\0\0 \0\0\0b
\0 \0\0\0 \0\0\0j (\0 \0\0\0 \0 \0\0 1\0 \0\0\0 \0\0\0r 2\0 \0\0\0 \0\0\0?i\0 \0\0\0 \0\0\0\0\0\0\0\0\0H\0\0\0
\0\0\0H\0\0\0 Adobe Photoshop CS Windows\02007:03:18 23:08:15\0\0\0\0\0 ?\0 \0\0\0 ??\0\0?\0 \0\0\0 \0\0\0? \0
........等等
其實就是將文件先轉換成了二進制的流,然後插入到了sql語言中,向資料庫寫入了很長很長的一段sql語句
然後我們再來寫一個app程序將這個文件讀出來,存儲成一個圖片文件
package com.lizhe;
import java.io.*;
import java.sql.*;
class GetImg {
private static final String URL = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;
public void blobRead(String outfile, int picID) throws Exception {
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select img from imgt where id=?");
pstmt.setInt(1, picID); // 傳入要取的圖片的ID
rs = pstmt.executeQuery();
rs.next();
file = new File(outfile);
if (!file.exists()) {
file.createNewFile(); // 如果文件不存在,則創建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("img");
int size = 0;
while ((size = is.read(Buffer)) != -1) {
// System.out.println(size);
fos.write(Buffer, 0, size);
}
} catch (Exception e) {
System.out.println( e.getMessage());
} finally {
// 關閉用到的資源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}
public static void main(String[] args) {
try {
GetImg gi=new GetImg();
gi.blobRead("c:/getimgs/1.jpg", 5);
} catch (Exception e) {
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}
這里需要注意的是
is = rs.getBinaryStream("img");
img是資料庫中相應的列名,其實和rs.getString()方法差不多,只不過這個方法是讀取二進制流的
最後在帖兩個bs系統上用的文件給大家參考
通過struts的action向資料庫寫入二進制圖片
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.lizhe.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.lizhe.struts.form.UpimgForm;
/**
* MyEclipse Struts
* Creation date: 05-18-2007
*
* XDoclet definition:
* @struts.action path="/upimg" name="upimgForm" input="/userhomepage.jsp"
* @struts.action-forward name="userhome" path="/userhomepage.jsp" redirect="true" contextRelative="true"
*/
public class UpimgAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
* @throws FileNotFoundException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
UpimgForm upimgForm = (UpimgForm) form;// TODO Auto-generated method stub
FormFile file=upimgForm.getFile();
InputStream is=file.getInputStream();
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql://localhost/blog?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
//stmt.execute("insert into img (id) values (5)");
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
//File file = new File("c:\\blog.jpg");
//InputStream photoStream = new FileInputStream(file);
//sql = " UPDATE imgt SET img = ? ";
sql = "INSERT INTO img (img) VALUES (?)";
pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, is, (int) file.getFileSize());
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("userhomepage");
}
}
和app的方式幾乎是一樣的
第二個文件是通過jsp將資料庫中的圖片顯示在頁面上
這個有些不同
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.awt.*"%>
<html>
<body>
<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url="jdbc:mysql://localhost/img?user=root&password=root";
Connection con = DriverManager.getConnection(url);
String sql = "select * from imgt where id=5";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
InputStream in = rs.getBinaryStream("img");
ServletOutputStream op = response.getOutputStream();
int len;
byte[] buf=new byte[1024];
while((len= in.read(buf))!=-1) {
op.write(buf, 0, len);
}
op.close();
in.close();
}
rs.close();
stmt.close();
con.close();
%>
</body>
</html>
4. 從jsp頁面傳圖片路徑到mysql資料庫路徑不正常了!急啊
字數隱符串轉義的問題
windows下的路徑分割運旅符是 \ 這個在程序中是個轉義旁畢凳字元,所以你要用 \\
兩個
5. jsp如何上傳圖片到資料庫
jsp上傳圖片到數據,在資料庫中有一種類型就是blob存儲類型,就是用於儲存二進制的。在java.sql裡面的PreparedStatment有個setBlob()方法存入資料庫,還有ResultSet里的getBlob()就是讀取,詳情你可以看JDBC Blob如何使用。
在jsp里上傳圖片很少用上述方式存儲到資料庫中,一般是將圖片上傳到伺服器項目目錄文件夾中,然後資料庫中保存該圖片文件的地址,如/item/upload/images/我上傳的圖片.jpg
6. 怎麼在JSP中插入圖片
1、首先需要新建Dynamic web project項目,建好之後在WebContent裡面的內容如圖所示。
(6)jspmysql上傳圖片擴展閱讀:
JSP將Java代碼和特定變動內容嵌入到靜態的頁面中,實現以靜態頁面為模板,動態生成其中的部分內容。JSP引入了被稱為「JSP動作」的XML標簽,用來調用內春枯建功能。另外,可以創建JSP標簽庫,然後像使用標准HTML或XML標簽一樣使用它們。
標簽庫能增強功態清能和伺服器性能,而且不受跨平台問題的限制。JSP文件在運行時會被其編譯器轉換成更原始的Servlet代碼。JSP編譯器可以把JSP文件編譯成用Java代碼寫的Servlet,然後再由Java編譯器來編譯成能快速執行的二進制機器碼,也可以直接編譯成二進制碼。
7. 請教下jsp頁面怎麼插入圖片到mysql
插入圖片,存的是圖片的路徑,JSP把路徑存到mysql就可以了,和插入其他的是一樣的
8. jsp如何將圖片上傳到伺服器某個文件夾裡面,而路徑存到mysql資料庫中,然後將資料庫中的圖片顯示到另一頁面
你把圖片存到資料庫?還是只存的圖片名? 用smartupload 控制項來完成 你{ // 上傳操作 mySmartUpload.upload(); //以原文件名存儲在web伺服器虛擬
9. 在JSP中怎樣將圖片上傳到資料庫中
到資料庫?
你可氏激以建一個文件夾來保存上傳的圖片, 然後將圖片的文件名保存到正猜資料庫中。 要用的時候在根據圖片的文件名到該文殲清襪件夾下面去讀取顯示出來
10. 用jps做一個注冊後台,資料庫用的mysql,此後台會員需要上傳照片,請問jsp怎麼實現存入和調用資料庫里的圖
用過「風聲無組件」嗎?可以在網路上搜一下。