当前位置: 首页 > 知识库问答 >
问题:

使用SpringMVC4和SpringBoot的html5多文件上传

薛鹏飞
2023-03-14

我试图上传多个文件使用Spring mvc 4,Spring引导和thymeleaf作为模板引擎,但我无法访问上传的文件,文件被处理为一个多部分文件与内容类型的应用程序/octet-stream.

    <form name="offer-form" th:action="@{/submit-property}" method="POST" enctype="multipart/form-data">

    <!--   .. other inputs .. -->

         <div class="col-xs-12 margin-top-60">
            <input id="file-upload"name="files[]" type="file" multiple="multiple"/>
        </div>
         <div class="col-xs-12">
            <div class="center-button-cont margin-top-60">
                <button type="submit" class="button-primary button-shadow">
                    <span>submit property</span>
                    <div class="button-triangle"></div>
                    <div class="button-triangle2"></div>
                    <div class="button-icon"><i class="fa fa-lg fa-home"></i></div>
                </button>
            </div>
        </div>
    </form>

以及控制器代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.aq.domain.Property;
import com.aq.service.AddPropertyFormDataInitializerService;

@Controller
public class PorpertySubmissionController 
{
    public static final Resource PICTURES_DIR = new FileSystemResource("./uploadedPictures");

    //some unrelated code

    @RequestMapping(value="/submit-property", method=RequestMethod.GET)
    public String getSubmitPropertyForm(Model model)
    {
        model.addAttribute("property", new Property());

        return "submit-property";
    }

    @RequestMapping(value="/submit-property", method=RequestMethod.POST)
    public String submitProperty
    (
            @ModelAttribute(value="property") Property property,
            @RequestParam("files[]") MultipartFile[] uploadedImages
    )
    {
        //some unrelated code

        if(uploadedImages != null && uploadedImages.length > 0)
            {
                System.out.println("uploadedImages length: " + uploadedImages.length);
                for(MultipartFile imageFile : uploadedImages)
                {
                    try 
                    {
                        copyFileToPictures(imageFile);
                        System.out.println("copied File: " + imageFile.getOriginalFilename() + " successfully.");
                    } 
                    catch (IOException e) 
                    {
                        System.err.println(e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                System.out.println("no images were uploaded");
            }

        return "redirect:/submit-property";
    }

    private Resource copyFileToPictures(MultipartFile file) throws IOException {
        System.out.println("File Original Name: " + file.getOriginalFilename());
        System.out.println("File Name: " + file.getName());
        System.out.println("File size: " + file.getSize());
        System.out.println("File Content type: " + file.getContentType());

        String fileExtension = getFileExtension(file.getOriginalFilename());

        File tempFile = File.createTempFile("pic", fileExtension, PICTURES_DIR.getFile());
        try (InputStream in = file.getInputStream();
                OutputStream out = new FileOutputStream(tempFile)) {
            IOUtils.copy(in, out);
        }
        return new FileSystemResource(tempFile);
    }

    private static String getFileExtension(String name) {
        return name.substring(name.lastIndexOf("."));
    }
}

sysout的输出:

上传图像长度:1(即使我上传了多个文件)

文件原始名称(使用getOrialFileName):

文件名(使用getName):文件[]

文件大小: 0

文件内容类型:应用程序/八位流

然后在空的原始文件名上调用subsctring时出现异常。

我尝试将commons文件上载添加到我的POM中,并配置了commons MultipartResolver bean,但它总是打印没有上载的图像(这意味着它为null或长度=0)

共有1个答案

谢旻
2023-03-14

为了防止有人面临同样的问题,结果证明它与spring和thymeleaf无关。这仅仅是因为我使用的主题使用了bootstrapfileinput插件,它可以在两种模式下工作:ajax和表单提交。事实证明,主题在文件上传中使用的大多数功能在表单提交模式下都不可用。所以这是第三方lib的问题,但我认为我应该分享。

 类似资料:
  • 问题内容: 我遇到了这个简单的js ajax上传代码(由于某种原因,jquery 似乎不适用于HTML5), 但是我这里有两个问题, 这条线在物体之后是什么意思? 为什么在那里需要ID?我能做些什么使用jQuery 用? 此ajax仅用于单个文件上传,如何更改多个文件上传? 问题答案: 这行在对象FormData之后是什么意思? 在得到由它的ID元件。该抓住从元件中的第一选择的文件。将其追加到与字

  • 本文向大家介绍php 使用html5实现多文件上传实例,包括了php 使用html5实现多文件上传实例的使用技巧和注意事项,需要的朋友参考一下 首先向大家介绍一下html5中file的multiple属性 定义和用法 multiple 属性规定输入字段可选择多个值。如果使用该属性,则字段可接受多个值。 实例: 上面实例中的input file 可接受多个文件上传字段。 了解了html5中file的

  • 问题内容: 诚然,Stack Overflow周围也存在类似的问题,但似乎没有一个完全符合我的要求。 这是我想要做的: 上载整个数据格式,其中一个是 单个 文件 使用Codeigniter的文件上传库 到这里为止,一切都很好。数据根据需要进入我的数据库。但我也想通过AJAX帖子提交表单: 使用原生HTML5 File API,而不是Flash或iframe解决方案 最好与低级jQuery方法接口

  • 本文向大家介绍详解SpringBoot文件上传下载和多文件上传(图文),包括了详解SpringBoot文件上传下载和多文件上传(图文)的使用技巧和注意事项,需要的朋友参考一下 最近在学习SpringBoot,以下是最近学习整理的实现文件上传下载的Java代码: 1、开发环境: IDEA15+ Maven+JDK1.8 2、新建一个maven工程:   3、工程框架   4、pom.xml文件依赖项

  • 问题内容: 我正在尝试使用html5的File API批量上传文件,然后在php的服务器端重新组装它。我正在上传视频,但是当我在服务器端合并文件时,文件大小增加了,并且变成了无效文件。请注意,以下html5代码仅适用于chrome浏览器。在Chrome 9中进行了测试,因为它使用了文件API的slice函数。有人可以指导我吗?谢谢 HTML来源 问题答案: Slice Function接受第二个参

  • 我试图创建一个页面,用户可以张贴图像及其细节。现在,当我测试来自postman的spring boot服务时,我能够成功地在服务中获取文件。当我试图从angular5中做同样的事情时,多部分文件在服务中没有被识别,并且总是得到空数组。 我的角服务代码如下 } 我已经尝试添加标头,如multipart/form-data,并将其设置为un定义。无论哪种方式,我都收到了错误。在发布到这里之前,我已经广