Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring Boot上载映像_Java_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 使用Spring Boot上载映像

Java 使用Spring Boot上载映像,java,spring,spring-mvc,thymeleaf,Java,Spring,Spring Mvc,Thymeleaf,我使用Thymeleaf和Spring boot将图像上传到数据库。 我采取的第一步是创建上传页面和控制器方法,以获取作为字节数组的多部分文件(图像) 这就是我没有得到正确字节数组的地方 我的thymeleaf部件代码: <div class="col-sm-8"> <form action="#" th:action="url to the controller method" method="post" enctype="multipart/form-data">

我使用Thymeleaf和Spring boot将图像上传到数据库。 我采取的第一步是创建上传页面和控制器方法,以获取作为字节数组的多部分文件(图像)

这就是我没有得到正确字节数组的地方

我的thymeleaf部件代码:

<div class="col-sm-8">
  <form action="#" th:action="url to the controller method" method="post" enctype="multipart/form-data">

    <div class="form-group">
      <div class="col-md-2">
        <label for="image">Upload image</label>
      </div>
      <div class="col-md-8">
        <input id="image" type="file" name="image"/>
      </div>
    </div>

    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>
My to字符串返回:

[B@4fe2fee8
这似乎不是正确的值,因为当我在互联网上转换相同的图像时,我会得到一个长列表,其中有0和1

有人知道如何解决这个问题吗?我的多部分文件似乎不包含上载的图像

我的url在表单中的作用似乎是正确的,因为我可以在控制器中访问我的Post方法

编辑
使用image.isEmpty()检查控制器中的多部分文件不正确。

toString不会打印字节数组的内容。Object.toString的java文档声明如下:

class对象的toString方法返回一个字符串,该字符串由对象作为实例的类的名称、at符号字符“@”和对象哈希代码的无符号十六进制表示形式组成

这正是显示的内容。该数组包含所有0和1。您可以轻松地将内容写入一个文件并打开它以查看它是否是正确的图像

Files.write(Paths.get("image.jpg"), image.getBytes());

您可以使用
Arrays.stream(design).forEach(System.out::println)
打印内容。

如果要检查图像数据是否为空,只需执行以下操作:

if (design.length == 0) {
  System.out.println("design is empty");
}

在数组上运行println时,将打印对象而不是其内容。您是否调试并检查了数组包含的内容?不要担心
toString
返回的奇怪值,默认实现只返回类似于@like的内容。
if (design.length == 0) {
  System.out.println("design is empty");
}