File file = new File("https://www.baidu.com/img/baidu_jgylogo3.gif");
怎么把 File
类型转 MultipartFile
呀
MultipartFile是spring的一个接口,通常我们可以在controller定义方法使用MultipartFile接收form表单提交的文件,然后将MultipartFile可以转化成一个文件。
/**
* A representation of an uploaded file received in a multipart request.
*
* <p>The file contents are either stored in memory or temporarily on disk.
* In either case, the user is responsible for copying file contents to a
* session-level or persistent store as and if desired. The temporary storages
* will be cleared at the end of request processing.
*/
public interface MultipartFile {}
这个接口通常是用来接收上传的文件,要将文件转换成MultipartFile文件,那么实际上应该考虑的是将File转换成CommonsMultipartFile。CommonsMultipartFile的构造器需要一个FileItem的参数,FileItem有一个实现类叫DefaultFileItem,DefaultFileItem 是apache.commons.fileupload下面的一个类。问题回到了需要去看一下apache的fileupload的源码,然后自己实现文件的上传。这个源码我没有去仔细阅读。楼主可以自己去看看