主要通过common-fileupload来实现
导入相关jar包common-fileupload、common-io
配置SpringMVC的文件解析器:
接着编写Controller:
@Controller
public class FileUploadController {
@RequestMapping("/file")
public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
InputStream is = file.getInputStream();
String path = request.getContextPath() + "/upload";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File toSave = new File(path, file.getOriginalFilename());
OutputStream os = new FileOutputStream(toSave);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
is.close();
System.out.println(toSave.getAbsolutePath());
return "success";
}
}
最后编写ui:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
上传文件
Please upload a file
<%
String path = request.getContextPath();
%>
总结:
终于学会好好地啾啾文档了。
有啥问题多看看文档哈哈哈哈