5 changed files with 112 additions and 31 deletions
@ -0,0 +1,65 @@ |
|||
package com.zdxt.knowledge.domain; |
|||
|
|||
import org.springframework.http.MediaType; |
|||
import org.springframework.util.FileCopyUtils; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import java.io.File; |
|||
import java.io.FileInputStream; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
|
|||
public class LocalFileMultipartFile implements MultipartFile { |
|||
|
|||
|
|||
private final File file; |
|||
|
|||
public LocalFileMultipartFile(File file) { |
|||
this.file = file; |
|||
} |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return file.getName(); |
|||
} |
|||
|
|||
@Override |
|||
public String getOriginalFilename() { |
|||
return file.getName(); |
|||
} |
|||
|
|||
@Override |
|||
public String getContentType() { |
|||
return MediaType.APPLICATION_OCTET_STREAM_VALUE; |
|||
} |
|||
|
|||
@Override |
|||
public boolean isEmpty() { |
|||
return file.exists(); |
|||
} |
|||
|
|||
@Override |
|||
public long getSize() { |
|||
try (FileInputStream fileInputStream = new FileInputStream(file)) { |
|||
return fileInputStream.available(); |
|||
} catch (IOException e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public byte[] getBytes() throws IOException { |
|||
return FileCopyUtils.copyToByteArray(file); |
|||
} |
|||
|
|||
@Override |
|||
public InputStream getInputStream() throws IOException { |
|||
return new FileInputStream(file); |
|||
} |
|||
|
|||
@Override |
|||
public void transferTo(File dest) throws IOException, IllegalStateException { |
|||
FileCopyUtils.copy(file, dest); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue