java文件上传学习

1.相关方法:

(1)后端用MutipartFile类来接受前台传过来的文件

public R upFile(HttpServletResponse res, HttpServletRequest req, @RequrstParam("file") MultipertFile file){
} 

(2)使用transferTo()方法将接收到的文件存储到相应路径:

file.transferTo(new File(path,filename));

(3)获取上传的文件名:

String fileName = file.getOriginalFilename;

(4)new File生成的对象:我们可以对对象进行解析和删除:

toFile = new File(path,file.getOriginalFilename());

//可以对toFile对象进行文件上传、删除、解析文件内容等操作

//删除

toFile.delete();

//上传
file.tansferTo(toFile);

//解析
xxxUtils.xxxparse(toFile);

(5)指定文件上传的路径:
 

String path = request.getSession().getServletContext().getRealPath("C://dengsir");

参考博客:https://www.jianshu.com/p/e623eb0dbcd6

Guess you like

Origin blog.csdn.net/kkkkkfffd/article/details/121517039