解決方法一般有兩種:

主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開發(fā)、微網(wǎng)站、小程序定制開發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。
1、將圖片保存的路徑存儲(chǔ)到數(shù)據(jù)庫(kù);
2、將圖片以二進(jìn)制數(shù)據(jù)流的形式直接寫入數(shù)據(jù)庫(kù)字段中。
以下為具體方法:
一、保存圖片的上傳路徑到數(shù)據(jù)庫(kù):
string
uppath="";//用于保存圖片上傳路徑
//獲取上傳圖片的文件名
string fileFullname =
this.FileUpload1.FileName;
//獲取圖片上傳的時(shí)間,以時(shí)間作為圖片的名字可以防止圖片重名
string
dataName =
DateTime.Now.ToString("yyyyMMddhhmmss");
//獲取圖片的文件名(不含擴(kuò)展名)
string
fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") +
1);
//獲取圖片擴(kuò)展名
string type =
fileFullname.Substring(fileFullname.LastIndexOf(".") +
1);
//判斷是否為要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg"
|| type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type ==
"GIF")
{
//將圖片上傳到指定路徑的文件夾
this.FileUpload1.SaveAs(Server.MapPath("~/upload")
+ "\\" + dataName + "." +
type);
//將路徑保存到變量,將該變量的值保存到數(shù)據(jù)庫(kù)相應(yīng)字段即可
uppath
= "~/upload/" + dataName + "." +
type;
}
二、將圖片以二進(jìn)制數(shù)據(jù)流直接保存到數(shù)據(jù)庫(kù):
引用如下命名空間:
using
System.Drawing;
using System.IO;
using
System.Data.SqlClient;
設(shè)計(jì)數(shù)據(jù)庫(kù)時(shí),表中相應(yīng)的字段類型為iamge
保存:
//圖片路徑
string
strPath = this.FileUpload1.PostedFile.FileName.ToString
();
//讀取圖片
FileStream fs = new System.IO.FileStream(strPath,
FileMode.Open, FileAccess.Read);
BinaryReader br = new
BinaryReader(fs);
byte[] photo =
br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection
myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User
ID=sa;Password=123");
string strComm = " INSERT INTO
stuInfo(stuid,stuimage) VALUES(107,@photoBinary
)";//操作數(shù)據(jù)庫(kù)語(yǔ)句根據(jù)需要修改
SqlCommand myComm = new SqlCommand(strComm,
myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,
photo.Length);
myComm.Parameters["@photoBinary"].Value =
photo;
myConn.Open();
if (myComm.ExecuteNonQuery()
0)
{
this.Label1.Text =
"ok";
}
myConn.Close();
讀取:
...連接數(shù)據(jù)庫(kù)字符串省略
mycon.Open();
SqlCommand
command = new
SqlCommand("select stuimage from stuInfo where stuid=107",
mycon);//查詢語(yǔ)句根據(jù)需要修改
byte[] image = (byte[])command.ExecuteScalar
();
//指定從數(shù)據(jù)庫(kù)讀取出來(lái)的圖片的保存路徑及名字
string strPath =
"~/Upload/zhangsan.JPG";
string strPhotoPath =
Server.MapPath(strPath);
//按上面的路徑與名字保存圖片文件
BinaryWriter bw = new
BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//顯示圖片
this.Image1.ImageUrl
= strPath;
采用這兩種方式可以根據(jù)實(shí)際需求靈活選擇。
你這思路就有問(wèn)題,保存圖片最好不要帶路徑,就保存圖片名字就夠了。你能保證項(xiàng)目遷移后你保存的路徑還和實(shí)際環(huán)境匹配嗎?
可以。存圖片的列需要設(shè)置成blob、mediumblob或longblob等數(shù)據(jù)類型。
但是以前基本上不會(huì)把圖片直接存在數(shù)據(jù)庫(kù)里,因?yàn)閿?shù)據(jù)庫(kù)里的數(shù)據(jù)是為了用來(lái)快速分析、快速存取的,圖片數(shù)據(jù)在mysql里既不能建立索引也不能和其他數(shù)據(jù)一起分析,存取速度和讀寫磁盤也沒(méi)什么區(qū)別,每次備份、導(dǎo)入導(dǎo)出數(shù)據(jù)庫(kù)時(shí)還增加了數(shù)據(jù)量,降低了效率。所以一般都是把圖片存在系統(tǒng)里,然后把圖片的存放路徑放在數(shù)據(jù)庫(kù)里。
在數(shù)據(jù)庫(kù)中,創(chuàng)建一張data表,用于測(cè)試。
請(qǐng)點(diǎn)擊輸入圖片描述
創(chuàng)建一個(gè)test.php文件,在文件內(nèi),使用header()方法將頁(yè)面的編碼格式設(shè)置為utf-8。
請(qǐng)點(diǎn)擊輸入圖片描述
在test.php文件內(nèi),連接mysql數(shù)據(jù)庫(kù),并使用mysqli_select_db選擇要操作的數(shù)據(jù)庫(kù)。
請(qǐng)點(diǎn)擊輸入圖片描述
在test.php文件內(nèi),創(chuàng)建一條插入數(shù)據(jù)的sql語(yǔ)句,向data表插入一張圖片地址。
請(qǐng)點(diǎn)擊輸入圖片描述
在test.php文件內(nèi),再使用mysqli_query執(zhí)行sql語(yǔ)句。
請(qǐng)點(diǎn)擊輸入圖片描述
在瀏覽器打開test.php文件,在數(shù)據(jù)庫(kù)查看結(jié)果。
請(qǐng)點(diǎn)擊輸入圖片描述
mysql create table t_test(
- id int primary key,
- filepath varchar(256));
mysql insert into t_test values(
- 1, 'd:\\abc');
mysql select * from t_test;
+----+----------+
| id | filepath |
+----+----------+
| 1 | d:\abc |
+----+----------+
把如”d:\abc"中的路徑中的\換成兩個(gè)\是可以的
但在java中寫的時(shí)候要注意這樣寫:
"insert into t_test values(1, 'd:\\\\abc')"
這里需要使用相對(duì)路徑,相對(duì)于你項(xiàng)目的路徑。
如果有幫助到你,請(qǐng)點(diǎn)擊采納
名稱欄目:mysql數(shù)據(jù)庫(kù)圖片路徑怎么寫,mysql數(shù)據(jù)庫(kù)圖片地址
文章網(wǎng)址:http://chinadenli.net/article10/dsidego.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、網(wǎng)頁(yè)設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、虛擬主機(jī)、品牌網(wǎng)站制作、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)