我正在尝试通过spring framework将映像路径存储到mysql数据库中…但无法做到..:/

h43kikqp  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(406)
  1. <h3>File to upload</h3>
  2. <form action ="upload" method="post" enctype="multipart/form-data">
  3. <input type ="file" name ="file">
  4. <input type ="submit" value ="submit">
  5. </form>

这是我的控制器

  1. @RequestMapping(value="/upload",method=RequestMethod.POST)
  2. public ModelAndView upload(@RequestParam("file") CommonsMultipartFile file,HttpSession session) throws IOException
  3. {
  4. String path = session.getServletContext().getRealPath("/");
  5. String filename=file.getOriginalFilename();
  6. ImagePOJO pojo = new ImagePOJO();
  7. byte barr[]=file.getBytes();
  8. pojo.setPath(path);
  9. pojo.setFilename(filename);
  10. //String q = pojo.setPath(path)+"/"+pojo.setFilename(filename);
  11. String w = pojo.getPath()+""+pojo.getFilename();
  12. //System.out.println(Arrays.toString(barr));
  13. System.out.println(path+" "+filename);
  14. System.out.println(w);
  15. BufferedOutputStream bout;
  16. try {
  17. bout = new BufferedOutputStream(new FileOutputStream(path+"/"+filename));
  18. Object o = bout;
  19. bout.write(barr);
  20. bout.flush();
  21. bout.close();
  22. } catch (FileNotFoundException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }[enter image description here][1]
js81xvg6

js81xvg61#

你为什么不试一试Spring的内容呢?
为自己生成一个spring启动应用程序http://start.spring.io/.
将这些依赖项添加到pom中。
pom.xml文件

  1. <dependency>
  2. <groupId>com.github.paulcwarren</groupId>
  3. <artifactId>spring-content-fs-boot-starter</artifactId>
  4. <version>0.0.11</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.github.paulcwarren</groupId>
  8. <artifactId>spring-content-rest-boot-starter</artifactId>
  9. <version>0.0.11</version>
  10. </dependency>

添加 UploadStore 你的主要课程:
springbootapplication.java文件

  1. @SpringBootApplication
  2. public class SpringContentApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringContentApplication.class, args);
  5. }
  6. @StoreRestResource(path="upload")
  7. public interface UploadStore extends Store<String> {
  8. }
  9. }

每个上传的文件都需要一个唯一的名称,所以让我们生成一个guid并动态设置表单的操作。按以下方式更新html:
表单.html

  1. <script language="JavaScript">
  2. window.onload = function() {
  3. document.myform.action = get_action();
  4. }
  5. function get_action() {
  6. return "upload/" + guidGenerator();
  7. }
  8. function guidGenerator() {
  9. var S4 = function() {
  10. return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
  11. };
  12. return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
  13. }
  14. </script>
  15. <html>
  16. <body>
  17. <h3>File to upload</h3>
  18. <form name=myform method="post" enctype="multipart/form-data">
  19. <input type ="file" name ="file">
  20. <input type ="submit" value ="submit">
  21. </form>
  22. </body>
  23. </html>

运行应用程序并为uploadstore提供一个位置:

  1. java -jar yourapp.jar --spring.content.fs.filesystemRoot=/path/to/your/store
  2. ``` `UploadStore` 以及 `upload` 不是很好的名字,因为这是一个功能齐全的内容服务,支持post、put、get和delete。甚至支持视频流。
  3. uploadstore也可以实现 `Renderable` 使您能够获取上传内容的格式副本;i、 例如,获取已上载word文档的pdf。
  4. 和/或 `Searchable` 启用全文索引(但这也需要apachesolr)。
  5. 它可以与spring数据相结合,允许您关联spring数据实体和spring内容资源。
  6. hth公司
展开查看全部

相关问题