1. 程式人生 > 其它 >JAVA file轉MultipartFile方法和可能存在的依賴衝突問題

JAVA file轉MultipartFile方法和可能存在的依賴衝突問題

file轉MultipartFile的時候會用到MockMultipartFile

當匯入spring-test依賴的時候 會跟某些依賴衝突

新增依賴:

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.5.5</version>
<scope>test</scope>
</dependency>

解決方法:為避免衝突不能使用,所以需要重寫一個類去實現MultipartFile介面

& 直接用MockMultipartFile的原始碼

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MultipartFileConfig implements MultipartFile {
private final String name;

private final String originalFilename;

private final String contentType;

private final byte[] content;

/**
* Create a new MultipartFileDto with the given content.
*
* @param name the name of the file
* @param content the content of the file
*/
public MultipartFileConfig(String name, byte[] content) {
this(name, "", null, content);
}

/**
* Create a new MultipartFileDto with the given content.
*
* @param name the name of the file
* @param contentStream the content of the file as stream
* @throws IOException if reading from the stream failed
*/
public MultipartFileConfig(String name, InputStream contentStream) throws IOException {
this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
}

/**
* Create a new MultipartFileDto with the given content.
*
* @param name the name of the file
* @param originalFilename the original filename (as on the client's machine)
* @param contentType the content type (if known)
* @param content the content of the file
*/
public MultipartFileConfig(String name, String originalFilename, String contentType, byte[] content) {
this.name = name;
this.originalFilename = (originalFilename != null ? originalFilename : "");
this.contentType = contentType;
this.content = (content != null ? content : new byte[0]);
}

/**
* Create a new MultipartFileDto with the given content.
*
* @param name the name of the file
* @param originalFilename the original filename (as on the client's machine)
* @param contentType the content type (if known)
* @param contentStream the content of the file as stream
* @throws IOException if reading from the stream failed
*/
public MultipartFileConfig(String name, String originalFilename, String contentType, InputStream contentStream)
throws IOException {

this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
}

@Override
public String getName() {
return this.name;
}

@Override
public String getOriginalFilename() {
return this.originalFilename;
}

@Override
public String getContentType() {
return this.contentType;
}

@Override
public boolean isEmpty() {
return (this.content.length == 0);
}

@Override
public long getSize() {
return this.content.length;
}

@Override
public byte[] getBytes() throws IOException {
return this.content;
}

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(this.content);
}

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
FileCopyUtils.copy(this.content, dest);
}
}

file轉MultipartFile轉換方法:

import org.apache.http.entity.ContentType;
/*
file轉MultipartFile
*/
private MultipartFile getMultipartFile(File file){
FileInputStream fileInputStream = null;
MultipartFile multipartFile = null;
try {
fileInputStream = new FileInputStream(file);
multipartFile = new MultipartFileConfig(file.getName(),file.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
} catch (Exception e) {
e.printStackTrace();
}
return multipartFile;
}