gpt4 book ai didi

java - 我试图通过 spring 框架将图像路径存储在 mysql 数据库中...但无法做到这一点.. :/

转载 作者:行者123 更新时间:2023-11-29 17:41:20 26 4
gpt4 key购买 nike

<h3>File to upload</h3>

<form action ="upload" method="post" enctype="multipart/form-data">
<input type ="file" name ="file">
<input type ="submit" value ="submit">
</form>

这是我的 Controller

@RequestMapping(value="/upload",method=RequestMethod.POST)
public ModelAndView upload(@RequestParam("file") CommonsMultipartFile file,HttpSession session) throws IOException
{
String path = session.getServletContext().getRealPath("/");
String filename=file.getOriginalFilename();
ImagePOJO pojo = new ImagePOJO();
byte barr[]=file.getBytes();


pojo.setPath(path);
pojo.setFilename(filename);

//String q = pojo.setPath(path)+"/"+pojo.setFilename(filename);

String w = pojo.getPath()+""+pojo.getFilename();
//System.out.println(Arrays.toString(barr));


System.out.println(path+" "+filename);

System.out.println(w);


BufferedOutputStream bout;
try {
bout = new BufferedOutputStream(new FileOutputStream(path+"/"+filename));


Object o = bout;
bout.write(barr);
bout.flush();
bout.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}[enter image description here][1]

最佳答案

你为什么不给 Spring Content走吧?

http://start.spring.io/ 为自己生成一个 Spring Boot 应用程序。

将这些依赖项添加到您的 pom 中。

pom.xml

<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-fs-boot-starter</artifactId>
<version>0.0.11</version>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.0.11</version>
</dependency>

UploadStore 添加到您的主类:

SpringBootApplication.java

@SpringBootApplication
public class SpringContentApplication {

public static void main(String[] args) {
SpringApplication.run(SpringContentApplication.class, args);
}

@StoreRestResource(path="upload")
public interface UploadStore extends Store<String> {
}
}

每个上传的文件都需要一个唯一的名称,因此让我们生成一个 guid 并动态设置表单的操作。像这样更新 html:

form.html

<script language="JavaScript">
window.onload = function() {
document.myform.action = get_action();
}

function get_action() {
return "upload/" + guidGenerator();
}

function guidGenerator() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
</script>

<html>
<body>
<h3>File to upload</h3>

<form name=myform method="post" enctype="multipart/form-data">
<input type ="file" name ="file">
<input type ="submit" value ="submit">
</form>
</body>
</html>

运行您的应用程序并为 UploadStore 指定一个位置:

java -jar yourapp.jar --spring.content.fs.filesystemRoot=/path/to/your/store 

UploadStoreupload 并不是一个好名字,因为这是一个功能齐全的内容服务,支持 POST、PUT、GET 和 DELETE。 GET 甚至支持视频流。

UploadStore 还可以实现Renderable,以获取上传内容的再现;例如,获取上传的 Word 文档的 PDF。

和/或Searchable以启用全文索引(但这也需要Apache Solr)。

它可以与 Spring Data 结合使用,允许您关联 Spring Data 实体和 Spring 内容资源。

HTH

关于java - 我试图通过 spring 框架将图像路径存储在 mysql 数据库中...但无法做到这一点.. :/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49993737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com