2013-05-20 44 views
19

Có cách nào để chuyển đổi đối tượng Tệp thành MultiPartFile không? Để tôi có thể gửi đối tượng đó đến các phương thức chấp nhận các đối tượng của giao diện MultiPartFile?Chuyển đổi tệp thành MultiPartFile

File myFile = new File("/path/to/the/file.txt") 

MultiPartFile ....? 

def (MultiPartFile file) { 
    def is = new BufferedInputStream(file.getInputStream()) 
    //do something interesting with the stream 
} 
+2

Bạn sẽ có thể viết lớp của riêng bạn thực hiện ['FileItem'] (http://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html) nhưng phải mất một 'File' thực tế để ủy nhiệm, sau đó truyền thể hiện' FileItem' này tới hàm tạo của ['CommonsMultipartFile'] (http://static.springsource.org/spring/docs/1.2.x/api/org /springframework/web/multipart/commons/CommonsMultipartFile.html) thực hiện 'MultiPartFile' –

+0

Tôi đã tạo một lớp thực hiện FileItem nhưng tôi không biết cách thực hiện tất cả các phương thức của giao diện đó. Tôi đã tạo một biến trong lớp này là 'File myFile'. Tôi chỉ nên thực hiện 'getinputStream()' và 'getOutputStream()'? – birdy

+0

nói riêng, tôi không biết ý của bạn là gì "nhưng điều đó cần có một Tệp thực tế để ủy quyền cho". Đây là những gì tôi có cho đến nay: https://gist.github.com/birdy101/5616009 – birdy

Trả lời

4
File file = new File("src/test/resources/validation.txt"); 
    DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false, file.getName(), (int) file.length() , file.getParentFile()); 
    fileItem.getOutputStream(); 
    MultipartFile multipartFile = new CommonsMultipartFile(fileItem); 

Bạn cần

fileItem.getOutputStream(); 

bởi vì nó sẽ ném NPE khác.

+0

** fileItem.getOutputStream(); ** như ma thuật – gstackoverflow

+1

Đó là phép thuật Java;) – despot

16

MockMultipartFile tồn tại cho mục đích này. Như trong đoạn mã của bạn nếu đường dẫn tệp được biết, mã bên dưới hoạt động cho tôi.

import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import org.springframework.mock.web.MockMultipartFile; 

Path path = Paths.get("/path/to/the/file.txt"); 
String name = "file.txt"; 
String originalFileName = "file.txt"; 
String contentType = "text/plain"; 
byte[] content = null; 
try { 
    content = Files.readAllBytes(path); 
} catch (final IOException e) { 
} 
MultipartFile result = new MockMultipartFile(name, 
        originalFileName, contentType, content); 
+3

Có thể thực hiện việc này mà không lưu tệp vào đĩa không? – Tisha

6
File file = new File("src/test/resources/input.txt"); 
    FileInputStream input = new FileInputStream(file); 
    MultipartFile multipartFile = new MockMultipartFile("file", 
      file.getName(), "text/plain", IOUtils.toByteArray(input)); 
1

Trong trường hợp của tôi,

fileItem.getOutputStream(); 

không hoạt động. Vì vậy, tôi đã làm cho nó bản thân mình sử dụng IOUtils,

File file = new File("/path/to/file"); 
FileItem fileItem = new DiskFileItem("mainFile", Files.probeContentType(file.toPath()), false, file.getName(), (int) file.length(), file.getParentFile()); 

try { 
    InputStream input = new FileInputStream(file); 
    OutputStream os = fileItem.getOutputStream(); 
    IOUtils.copy(input, os); 
} catch (IOException ex) { 
    // do something. 
} 

MultipartFile multipartFile = new CommonsMultipartFile(fileItem); 
+0

Cảm ơn, đây chính xác là những gì tôi cần !! :) – gabowsky

3
MultipartFile multipartFile = new MockMultipartFile("test.xlsx", new FileInputStream(new File("/home/admin/test.xlsx"))); 

mã này wooks tốt cho me.May là bạn có thể có một thử.

Các vấn đề liên quan