Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

tải hình ảnh lên máy chủ trong Spring MVC và lưu trữ tham chiếu trong cơ sở dữ liệu mysql

Lưu trữ trong đĩa và lưu trữ trong MySQL có những điều cần lưu ý. Tại đây là một cuộc thảo luận tốt về nó.

Để lưu trữ nó trong hệ thống tệp, bạn có thể sử dụng Tải lên tệp của Commons . Đây là một mẫu

pom.xml

     <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>${release.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${release.version}</version>
    </dependency>

JSP

<h2>Spring MVC file upload example</h2>

<form method="POST" action="<c:url value='/upload' />"
    enctype="multipart/form-data">


    Please select a file to upload : <input type="file" name="file" />
    <input type="submit" value="upload" />

</form>

Bộ điều khiển

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFormUpload( 
    @RequestParam("file") MultipartFile file) throws IOException{
if (!file.isEmpty()) {
 BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
 File destination = new File("File directory with file name") // something like C:/Users/tom/Documents/nameBasedOnSomeId.png
 ImageIO.write(src, "png", destination);
 //Save the id you have used to create the file name in the DB. You can retrieve the image in future with the ID.
 }  
}

Và xác định điều này trong ngữ cảnh ứng dụng của bạn

 <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

Tôi hy vọng điều này sẽ hữu ích.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Sử dụng thư mục dữ liệu MySQL trước đó khi cài đặt MySQL mới

  2. mysql cho python 2. 7 cho biết không tìm thấy Python v2.7

  3. cách sử dụng hàm string left trong hql

  4. Truy vấn tự động chạy trong mysql

  5. Cơ sở dữ liệu đang ngủ và gây ra ngoại lệ