文章字数:39,阅读全文大约需要1分钟
简便方法
- 使用
file.transferTo("地址/文件名")
1
2
3
4
5
6
7
8public void upload(@RequestParam("file") MultipartFile file) {
try {
file.transferTo(new File("/file/upload/" + file.getName()));
} catch (Exception e) {
e.printStackTrace();
}
return;
} - IOUTils
import org.apache.commons.io.IOUtils;
1
2
3
4
5
6
7
8
9
10public void upload(@RequestParam("file") MultipartFile file){
file file1 = new File("c:\\xxx\\aaa","a.txt");
//原始文件输入流
InputStream inputStream = file.getInputStream();
//新文件输出流
FileOutputStream fosm = new FileOutputStream (file1);
IOUtils.copy(inputStream,fosm );
inputStream.close();
fosm .close();
}