文件上傳資料庫
當你問到這個問題的時候,其實我是不想回答的
你應該是個小白,因為 「文件地址」這個其實就是一個URL,就是一個字元串。把「
文件地址上傳到資料庫」,其實就是把一個字元串插入到相應的表。如果你有基礎,文件都上傳了獲取到路徑了,就等插入資料庫,根本不會來問這個問題。如果你沒基礎,那不是我在這里三言兩語甚至長篇大論能教會你的。
我只能大概說一下思路:
1、首先,建表,資料庫新建一個表來存放這個文件地址
2、鏈接這個資料庫
3、把這個「文件地址」,插到資料庫裡面
完了!
Ⅱ 資料庫怎麼上傳的
直接把sql復制,在新資料庫運行
Ⅲ 如何實現將本地文件直接上傳到伺服器資料庫
將本地資料庫導入到伺服器資料庫的方法/步驟:
1、在本地和伺服器都安裝同樣的資料庫客戶端,如oracle常用SQLPlus、MySQL常用HeiDi sql或者navicat、mssql2005則常用SQL Server Management Studio;
2、在本地通過資料庫客戶端導出資料庫為sql文件;
3、將sql文件遠程傳遞到伺服器上;
4、在伺服器上用相同的資料庫客戶端將sql文件執行一遍即可將本地資料庫導入到伺服器上。
Ⅳ springboot上傳文件寫入資料庫
首先導入了相應的jar包
<!--thymeleaf--><dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId></dependency><dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId></dependency><!--資料庫連接--><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId></dependency><!--mybatis整合springboot--><dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version></dependency><!--druid數據源--><dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version></dependency>
對數據源,mybatis,和上傳文件進行配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource username: root password: shw123zxc url: jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver servlet:
multipart:
max-request-size: 10MB #上傳文件的最大總大小
max-file-size: 10MB #上傳單個文件的最大大小mybatis:
type-aliases-package: cn.codewei.pojo mapper-locations: classpath:/mapper/*.xml1234567891011121314
然後寫一個文件上傳的html,注意表單的==enctype屬性要設置為multipart/form-data==
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Title</title></head><body>
<h1>文件上傳</h1>
<form method="post" enctype="multipart/form-data" action="/upload">
<input type="file" name="file">
<input type="submit" value="上傳">
</form></body></html>1234567891011121314
然後寫一個Mapper和對應的Mapper.xml和service
@Mapper@Repositorypublic interface PhotoMapper {
// 向資料庫中添加圖片
public int addPhoto(Photo photo);
// 從資料庫中取出圖片
public Photo getPhotoById(@Param("id") int id);}123456789
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.codewei.mapper.PhotoMapper">
<insert id="addPhoto" parameterType="photo">
insert into image values (#{id},#{photo},#{photo_name},#{photo_type}) </insert>
<select id="getPhotoById" resultType="photo">
select * from image where id=#{id} </select></mapper>123456789101112
在Controller中進行調用
上傳
@Autowiredprivate PhotoService photoService;@Autowiredprivate Photo photo;@PostMapping("/upload")@ResponseBodypublic String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
byte[] bytes = file.getBytes();
photo.setPhoto(bytes);
photo.setPhoto_name(file.getOriginalFilename());
photo.setPhoto_type(".jpg");
photoService.addPhoto(photo);
return "上傳成功!";}123456789101112131415161718
取出,在頁面中顯示
@RequestMapping("/getPhoto")public String getImage(HttpServletResponse response) throws IOException {
Photo photo = photoService.getPhotoById(1);
byte[] photo1 = photo.getPhoto();
ServletOutputStream os = response.getOutputStream();
os.write(photo1);
os.close();
return "";}123456789
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Title</title></head><body>
<h1>首頁</h1>
<img src="/getPhoto" width="200px" height="200px"></body></html>
Ⅳ 如何上傳文件到資料庫
文件上傳到資料庫請參考以下示例:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="java.sql.*" %>
<%/////連接資料庫
java.sql.Connection conn;
java.sql.Statement stmt;
java.sql.ResultSet rs1;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:dababasename");///數據源
stmt=conn.createStatement();
%>
<%
String sqlin="insert into drawing values ;
File file = new File("c:/z.jpg");
long l1=file.length();
int l2=(int)l1;
FileInputStream is = new FileInputStream(file);
InputStream fis=(InputStream)is;
PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
ps.setString(1,file.getName());
ps.setBinaryStream(2,fis,file.length());
ps.executeUpdate();
ps.close();
fis.close();
//將圖片從資料庫中提取,進行顯示
//drawing欄位為IMAGE類型
stmt = conn.createStatement();
rs1 = stmt.executeQuery("SELECT * FROM drawing WHERE drawing_code ='0-515' and version=3");
if (rs1.next())
{
String dim_image = rs1.getString("file_name");
byte [] blocco = rs1.getBytes("drawing");
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
for(int i=0;i<blocco.length;i++)
{
op.write(blocco[i]);
}
}
rs1.close();
%>