本文目錄一覽:
- 1、java中 怎樣吧 文件路徑存儲到mysql資料庫中。圖片如下(回答的好,追加分)謝謝了。
- 2、怎麼把圖片路徑存到mysql資料庫,求這樣的代碼
- 3、如何把文件存放在mysql資料庫中
- 4、如何將圖片儲存在MySQL資料庫里?
- 5、jsp如何將圖片上傳到伺服器某個文件夾裡面,而路徑存到mysql資料庫中,然後將資料庫中的圖片顯示到另一頁面
java中 怎樣吧 文件路徑存儲到mysql資料庫中。圖片如下(回答的好,追加分)謝謝了。
存到資料庫時將所有\替換成其他字串,如”|”,然後在使用的時候在替換回來就可以了。。加上驗證,這樣可以避免一些BUG
怎麼把圖片路徑存到mysql資料庫,求這樣的代碼
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”中的路徑中的\換成兩個\是可以的
但在java中寫的時候要注意這樣寫:
“insert into t_test values(1, ‘d:\\\\abc’)”
如何把文件存放在mysql資料庫中
在my.ini文件里可以設定數據文件的存放路徑將其從原來的目錄移動到指定的位置;修改配置文件my.ini,也就是把socket和data的目錄改到你移到的位置
如何將圖片儲存在MySQL資料庫里?
解決方法一般有兩種:
1、將圖片保存的路徑存儲到資料庫;
2、將圖片以二進位數據流的形式直接寫入資料庫欄位中。
以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
string
uppath=””;//用於保存圖片上傳路徑
//獲取上傳圖片的文件名
string fileFullname =
this.FileUpload1.FileName;
//獲取圖片上傳的時間,以時間作為圖片的名字可以防止圖片重名
string
dataName =
DateTime.Now.ToString(“yyyyMMddhhmmss”);
//獲取圖片的文件名(不含擴展名)
string
fileName = fileFullname.Substring(fileFullname.LastIndexOf(“\\”) +
1);
//獲取圖片擴展名
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);
//將路徑保存到變數,將該變數的值保存到資料庫相應欄位即可
uppath
= “~/upload/” + dataName + “.” +
type;
}
二、將圖片以二進位數據流直接保存到資料庫:
引用如下命名空間:
using
System.Drawing;
using System.IO;
using
System.Data.SqlClient;
設計資料庫時,表中相應的欄位類型為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
)”;//操作資料庫語句根據需要修改
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();
讀取:
…連接資料庫字元串省略
mycon.Open();
SqlCommand
command = new
SqlCommand(“select stuimage from stuInfo where stuid=107”,
mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar
();
//指定從資料庫讀取出來的圖片的保存路徑及名字
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;
採用這兩種方式可以根據實際需求靈活選擇。
jsp如何將圖片上傳到伺服器某個文件夾裡面,而路徑存到mysql資料庫中,然後將資料庫中的圖片顯示到另一頁面
你把圖片存到資料庫?還是只存的圖片名? 用smartupload 控制項來完成 你{ // 上傳操作 mySmartUpload.upload(); //以原文件名存儲在web伺服器虛擬
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/187872.html