在Spring启动时上传文件以在sql db中添加

时间:2017-05-21 03:49:09

标签: sql spring-boot blob

我正在处理代码但卡住了。它应该能够从Web浏览器读取图像文件并转换为blob for sql db。我使用的是spring boot,hibernate,sql。没有js或xml。控制器和html代码如下所示。

@Controller    
@RequestMapping("imagefile")
public class ImagefileController {@RequestMapping(value = "menu", method = RequestMethod.POST)
public String imageupload(Model model,  @RequestParam("id") int id, @RequestParam("uploadFile") MultipartFile uploadFile,
RedirectAttributes redirectAttributes) {

    //add photo upload coding here.
    String filename=uploadFile.getOriginalFilename();
    String uploadFilepath=Paths.get("." + File.separator, filename).toString();;

    //need to get the file into the input stream.

    //byte[] bytes = uploadFile.getBytes();
    //String filename1 = uploadFile.toString();
    //File f = new File(filename1);
    //f.getAbsolutePath();
    //FileInputStream fis = new FileInputStream(f.getAbsoluteFile());
    //f.getAbsolutePath();
    Byte [] imagefile;

    //InputStream is = new FileInputStream(new File(filename1));
    //File filename = new FileInputStream(filename1.getBytes());
    //String uploadFilename = uploadFile.getOriginalFilename();
    //createSessionFactory().openSession();
    //Session session = sessionFactory.getSessionFactory().openSession(); //getSessionFactory().getCurrentSession();
    //File uploadfile = new File(uploadfile);
   // Blob fileblob = Hibernate.getLobCreator(session).createBlob(filename.getBytes()); //new FileInputStream(uploadfile), file1.length()
    Menu menu = menuDao.findOne(id);
    model.addAttribute("title", "Add images to the menu: " + menu.getName());
    System.out.println("Original Filename is:" + uploadFile.getOriginalFilename());
    System.out.println("File Class is:" + uploadFile.getClass());
    System.out.println("Is the File Empty?:" + uploadFile.isEmpty());
    System.out.println("File size:" + uploadFile.getSize());
    System.out.println("File contentType:" + uploadFile.getContentType());
    //session.close();
    return "Imagefile/index";
}
}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/" xmlns:form="http://www.w3.org/1999/xhtml">

<head th:replace="fragments :: head"></head>
<body class="container">

<nav th:replace="fragments :: navigation"></nav>


<h1 th:text="${title}"></h1>

<form method="post" enctype="multipart/form-data"> 

<input type="file" name="uploadFile" id="file" title=" "  accept="image/*"/>

    <p></p>
    <input type="submit" value="add photo" />


</form>

</body>
</html>

----- ------ DAO

@Repository
@Transactional
 public interface MenuDao extends CrudRepository<Menu, Integer>{
 }

------- sql的实体----

@Entity
public class Imagefile {
@Id
@GeneratedValue
private int id;
private String filename;
@Lob
private Blob imagecontent;
@ManyToOne
private Menu menu;

//constructor
public Imagefile(int id, String filename, Blob imagecontent) {
    this.id = id;
    this.filename = filename;
    this.imagecontent = imagecontent;
}
public Imagefile() {}

//Getters and Setters - Accessors.

public int getId() {
    return id;
}

public String getFilename() {
    return filename;
}

public void setFilename(String filename) {
    this.filename = filename;
}

public Blob getImagecontent() {
    return imagecontent;
}

public void setImagecontent(Blob imagecontent) {
    this.imagecontent = imagecontent;
}

public Menu getMenu() {
    return menu;
}

public void setMenu(Menu menu) {
    this.menu = menu;
}
}

以下是测试的输出。

Original Filename is:40 Brookside.jpg File Class is:class org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile Is the File Empty?:false File size:473735 File contentType:image/jpeg

1 个答案:

答案 0 :(得分:0)

这里我将如何继续将文件存储在FileSystem和DB中的路径中。

public String imageupload(@RequestPart("uploadFile") MultipartFile uploadFile,...) {

   String FILES_FOLDER = "C:/MyFilesDirectory/";
   String PHOTOS_FOLDER= "PHOTOS/";
   String photoName = uploadFile !=null ? uploadFile.getOriginalFileName(): null;
   String DIRECTORY  = FILES_FOLDER+PHOTOS_FOLDER+photoName;

   //Now we transfer the file to DIRECTORY
   File file = new File(DIRECTORY);
   //check if 'file' does not exist then create it 
   // finally :
   uploadFile.transferTo(file);
   //Then save 'DIRECTORY' in your db 
   menuDao.save(DIRECTORY); // I don't know what is inside your menuDao

}

请注意,我使用@RequestPart代替@RequestParam

编辑:加载文件

显示图像的代码示例。我们假设你在视图中有这个

<img src="get-image" />

然后在您的处理程序方法中,您将拥有:

@RequestMapping("get-image" )
@ResponseBody
public byte[] getImage(){
//retrieve your image from DB
//transform it to byte[]
// then return it
}