简体   繁体   English

我们如何在Spring MVC Hibernate应用程序的JSP中上载图像?

[英]How can we upload an image in a JSP in a Spring MVC Hibernate application?

I need to save an image from jsp to database (MySql) in Spring MVC Hibernate web application. 我需要在Spring MVC Hibernate Web应用程序中将图像从jsp保存到数据库(MySql)。 Could somebody give me an example of uploading an image and then saving it into db ? 有人可以给我举一个上传图像然后保存到数据库的例子吗?

use any of the jquery based fileuploaders , and save just the path in db. 使用任何基于jquery的fileuploaders,仅将路径保存在db中。 that way you will be able to scale your app fast. 这样,您将能够快速扩展您的应用程序。 e..g valum uploader 例如valum上传器

Solution is first get path from jsp and with hibernet pass object as a parameter. 解决方案是首先从jsp获取路径,并使用hibernet pass对象作为参数。

UserVehicle.java UserVehicle.java

private byte[] image;
public UserVehicle(byte[] image)
{
  this.image=image;
}
---getter n setter
}

in Controller.java 在Controller.java中

File file = new File(/*your file path*/);
byte[] bFile = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
UserVehicle user = new UserVehicle(bFile)

your value will be inserted as blob file in your database. 您的值将作为Blob文件插入数据库中。

using the Spring MVC, so you can use spring form like this: 使用Spring MVC,因此您可以使用spring形式,如下所示:

<form:form method="POST" action="/spring-mvc-xml/uploadFile" enctype="multipart/form-data">
<table>
    <tr>
        <td><form:label path="file">Select a file to upload</form:label></td>
        <td><input type="file" name="file" /></td>
    </tr>
    <tr>
        <td><input type="submit" value="Submit" /></td>
    </tr>
</table>
</form>

the most important part is to add the enctype="multipart/form-data" and the type of the input must be "file" , then you can add for your rest controller the multipart param like this: 最重要的部分是添加enctype =“ multipart / form-data”并且输入的类型必须为 “ file” ,然后可以为rest控制器添加multipart参数,如下所示:

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String submit(@RequestParam("file") MultipartFile file, ModelMap 
modelMap) {
 // here u can find your uploaded file you can set the prop in your 
    object model ,then save object and this will save the file also in 
    blob.

return "fileUploadView";
}

finally you need to crate multipart Bean in your spring context like this: 最后,您需要在春天的上下文中像这样创建多部分Bean:

//you can defined it in xml way if your configuration in xml file 

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver()
{
 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
   multipartResolver.setMaxUploadSize(20848820);
 return multipartResolver;
}

and also you need to add the upload file library at your dependency to Spring can use it : 并且您还需要在依赖项上添加上载文件库,Spring可以使用它:

dependency>
 <groupId>commons-fileupload</groupId>
 <artifactId>commons-fileupload</artifactId>
 <version>1.3.1</version> // or you can find the last version
</dependency>

last Note : for the data base you can set the column is BLOB or any LOB type. 最后的提示:对于数据库,您可以将列设置为BLOB或任何LOB类型。 I usually use this way to upload files in my Spring applications , so I hope that you are need also. 我通常使用这种方式在Spring应用程序中上传文件,因此希望您也需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM