inputstream转multipartfile多个方法
时间: 2023-09-29 09:03:15 浏览: 78
1. 使用Apache Commons IO库
```
import org.apache.commons.io.IOUtils;
public MultipartFile convert(InputStream inputStream, String fileName, String contentType) throws IOException {
byte[] bytes = IOUtils.toByteArray(inputStream);
return new MockMultipartFile(fileName, fileName, contentType, bytes);
}
```
2. 使用Spring框架的MultipartFile
```
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
public MultipartFile convert(InputStream inputStream, String fileName, String contentType) throws IOException {
return new CommonsMultipartFile(fileName, inputStream);
}
```
3. 使用Java原生的Part接口
```
import javax.servlet.http.Part;
public MultipartFile convert(InputStream inputStream, String fileName, String contentType) throws IOException, ServletException {
Part part = new MockPart(fileName, contentType, inputStream);
return new StandardMultipartFile(part);
}
```
阅读全文