spring 如何仅给定文件创建 CommonsMultipartFile 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2740905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:03:26  来源:igfitidea点击:

How to create CommonsMultipartFile object given only a file

springspring-mvcfile-uploadapache-commons

提问by wsams

I have a junit test method that takes a CommonsMultipartFile object as a parameter.

我有一个 junit 测试方法,它将 CommonsMultipartFile 对象作为参数。

I'm trying to create a FileItem object so I can pass it to the constructor,

我正在尝试创建一个 FileItem 对象,以便将其传递给构造函数,

CommonsMultipartFile(org.apache.commons.fileupload.FileItem fileItem)

To do that, I'm trying to create the FileItem object using the DiskFileItem constructor,

为此,我正在尝试使用 DiskFileItem 构造函数创建 FileItem 对象,

DiskFileItem(java.lang.String fieldName, java.lang.String contentType, boolean isFormField, java.lang.String fileName, int sizeThreshold, java.io.File repository)

but I'm not sure how to pass any of those parameters.

但我不确定如何传递任何这些参数。

I have all of this working in a Spring 3 MVC controller, but to do my junit tests, I need to pass a method two objects. One is the UploadItem object which looks like the following,

我在 Spring 3 MVC 控制器中完成了所有这些工作,但是要进行 junit 测试,我需要将一个方法传递给两个对象。一个是 UploadItem 对象,如下所示,

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class UploadItem {
 private String fileName;
 private String filePath;
 private CommonsMultipartFile fileData;

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

 public String getFilePath() {
  return filePath;
 }

 public void setFilePath(String filePath) {
  this.filePath = filePath;
 }

 public CommonsMultipartFile getFileData() {
  return fileData;
 }

 public void setFileData(CommonsMultipartFile fileData) {
  this.fileData = fileData;
 }
}

The setFileData() method requires the CommonsMultipartFile object which I'm trying to create just given a file in my src/test/resources directory.

setFileData() 方法需要我正在尝试创建的 CommonsMultipartFile 对象,只是在我的 src/test/resources 目录中给定了一个文件。

Would anyone know how I can take a file, create a FileItem object and pass that to the CommonsMultipartFile object constructor?

有谁知道我如何获取文件,创建一个 FileItem 对象并将其传递给 CommonsMultipartFile 对象构造函数?

Thanks. If anything is unclear, please let me know - I'm not that familiar with Spring MVC file uploads.

谢谢。如果有任何不清楚的地方,请告诉我 - 我对 Spring MVC 文件上传不太熟悉。

回答by Ralph

Use the more common interface org.springframework.web.multipart.MultipartFile. instead of org.springframework.web.multipart.commons.CommonsMultipartFilein your Command (UploadItem). (CommonsMultipartFile is a 1:1 implementation of the Interface).

使用更通用的接口org.springframework.web.multipart.MultipartFile。而不是 org.springframework.web.multipart.commons.CommonsMultipartFile在您的命令 (UploadItem) 中。(CommonsMultipartFile 是接口的 1:1 实现)。

Now you can create an instance of CommonsMultipartFilewith the mock class org.springframework.mock.web.MockMultipartFile. (which is element of spring-test.jar).

现在您可以CommonsMultipartFile使用模拟类创建一个实例org.springframework.mock.web.MockMultipartFile。(这是 的元素spring-test.jar)。

Then the creation of an MultipartFile in the tests is only one statement, without any cast:

那么在测试中创建 MultipartFile 只是一个语句,没有任何强制转换:

MockMultipartFile mockMultipartFile = new MockMultipartFile(
       "test.txt",                //filename
       "Hallo World".getBytes()); //content

回答by Popescu Marius-Vasile

How is this of any help? you don't set the file on the request, it should be used like this:

这有什么帮助?你没有在请求中设置文件,它应该像这样使用:

MultipartFile multipartFile = getMockCommonsMultipartFile(BULK_CSV);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.addFile(multipartFile);
CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) request.getFile(BULK_CSV);

I am using a method with the argument CommonsMultipartFile, otherwise I could have used MockMultipartFiledirectly.

我正在使用带有参数的方法CommonsMultipartFile,否则我可以MockMultipartFile直接使用。

 private MultipartFile getMockCommonsMultipartFile(String name, String path) throws IOException {
        InputStream is = getClass().getResourceAsStream(path);
        MultipartFile multipartFile = new MockMultipartFile(name, name, "", is);
        return multipartFile;
    }