package com.netvox.lora.controller;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @Package: com.netvox.lora.controller
* @ClassName: UploadController
* @Author: ZhanWeiFeng
* @Description: //TODO
* @Date: 2020.11.12 9:23
* @Version: 1.0
*/
@Controller
public class UploadController {
@RequestMapping("upload")
public StringuploadIndex() {
return "upload";
}
@RequestMapping(value ="/uploadSource", method = RequestMethod.POST)
@ResponseBody
public MapuploadSource(@RequestParam("file") MultipartFile file, @RequestParam("password") String password) {
Map result =new HashMap<>();
if (!password.equals("netvox5717188")){
result.put("code",-1);
result.put("msg","口令错误");
return result;
}
System.out.println(file);
String pathString =null;
try {
File directory =new File(Resource.class.getClassLoader().getResource("device.json").getPath());
pathString = directory.getCanonicalPath();
File files =new File(pathString);
//打印查看上传路径
System.out.println(pathString);
if (files.exists()){
files.delete();
}
file.transferTo(files);
result.put("code", 200);
result.put("msg", "success");
}catch (Exception e) {
result.put("code", -2);
e.printStackTrace();
}
return result;
}
}
前端代码