Spring MVC 文件上传帮助 [英] Spring MVC File Upload Help

查看:25
本文介绍了Spring MVC 文件上传帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将 spring 集成到应用程序中,并且必须从表单中重做文件上传.我知道 Spring MVC 必须提供什么以及我需要做什么来配置我的控制器才能上传文件.我已经阅读了足够多的教程来做到这一点,但是这些教程都没有解释正确/最佳实践方法,即一旦拥有文件,如何/如何实际处理文件.下面是一些类似于在 Spring MVC Docs 上找到的关于处理文件上传的代码的代码,可以在
Spring MVC 文件上传

I have been integrating spring into an application, and have to redo a file upload from forms. I am aware of what Spring MVC has to offer and what I need to do to configure my controllers to be able to upload files. I have read enough tutorials to be able to do this, but what none of these tutorials explain is correct/best practice methods on how/what is to be done to actually handle the file once you have it. Below is some code similar to code found on the Spring MVC Docs on handling file uploads which can be found at
Spring MVC File Upload

在下面的示例中,您可以看到他们向您展示了获取文件的所有操作,但他们只是说用 bean 做一些事情

In the example below you can see that they show you everything to do to get the file, but they just say Do Something with the bean

我查阅了很多教程,它们似乎都让我走到了这一步,但我真正想知道的是处理文件的最佳方式.一旦我有了一个文件,将这个文件保存到服务器上的目录的最佳方法是什么?有人可以帮我解决这个问题吗?谢谢

I have checked many tutorials and they all seem to get me to this point, but what I really want to know is the best way to handle the file. Once I have a file at this point, what is the best way to save this file to a directory on a server? Can somebody please help me with this? Thanks

public class FileUploadController extends SimpleFormController {

protected ModelAndView onSubmit(
    HttpServletRequest request,
    HttpServletResponse response,
    Object command,
    BindException errors) throws ServletException, IOException {

     // cast the bean
    FileUploadBean bean = (FileUploadBean) command;

     let's see if there's content there
    byte[] file = bean.getFile();
    if (file == null) {
         // hmm, that's strange, the user did not upload anything
    }

    //do something with the bean 
    return super.onSubmit(request, response, command, errors);
}

推荐答案

这是我在上传时更喜欢的.我认为让 spring 处理文件保存,是最好的方法.Spring 用它的 MultipartFile.transferTo(File dest) 函数来做到这一点.

This is what i prefer while making uploads.I think letting spring to handle file saving, is the best way. Spring does it with its MultipartFile.transferTo(File dest) function.

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/upload")
public class UploadController {

    @ResponseBody
    @RequestMapping(value = "/save")
    public String handleUpload(
            @RequestParam(value = "file", required = false) MultipartFile multipartFile,
            HttpServletResponse httpServletResponse) {

        String orgName = multipartFile.getOriginalFilename();

        String filePath = "/my_uploads/" + orgName;
        File dest = new File(filePath);
        try {
            multipartFile.transferTo(dest);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return "File uploaded failed:" + orgName;
        } catch (IOException e) {
            e.printStackTrace();
            return "File uploaded failed:" + orgName;
        }
        return "File uploaded:" + orgName;
    }
}

这篇关于Spring MVC 文件上传帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆