打开若依主页后我们在实例演示->表单->文件上传中可以找到作者写好的上传模块,但是这个模块是不能跑的,也没有下载的功能。这次我们主要对他的upload.html和DemoFormController.java稍作补全,让它成功实现上传和下载的功能。
后端代码,只展示新增的片段:
private String prefix = "demo/form";
//上传的路径在这里配置,下载的路径就管不着了,由浏览器配置了。
private String uploadPath = "D:\\java\\Jweb\\RuoYi\\uploadPath\\";
/**
* 自己写的测试上传
*/
@PostMapping("/upload")
@ResponseBody
public String myupload(@RequestParam(value="file",required=false) MultipartFile[] file,ModelMap mmap) {
try {
InputStream is = file[0].getInputStream();
File f = new File(uploadPath);
if(!f.exists())f.mkdirs();
String filename = file[0].getOriginalFilename();
FileOutputStream os = new FileOutputStream(uploadPath+filename);
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1) {
os.write(b);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
//bootstrap-fileupload是把file塞到reques里面传过来的,如果想在后台接收file,需要借用spring的@requesparam注解找传过来的file
JSONArray list = new JSONArray();
JSONObject item = new JSONObject();
item.put("name", StringUtils.defaultIfBlank("HELLO FROM A.L.P.K.", "通过电话销售过程中了解各盛市的设备仪器使用、采购情况及相关重要追踪人"));
item.put("type", "新增");
item.put("date",