Solved the problem of fakepath uploading files in Google browsers,
Some time ago, when implementing a file upload function, it was found that mainstream browsers such as Google would change the address of the uploaded file to fakepath for security reasons. For example, a file in drive C, no matter which file it was originally in. After the file is uploaded, it automatically becomes the name of the C \ fakepath \ file.
The solution is as follows:
Front-end form submission file
<form name="Form2" action="/Import/ImportExcel" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="upload"/> </form>
The controller layer in the background obtains the file and uploads it to the edisk. The file name is converted into a timestamp to distinguish
@ Controller @ RequestMapping ("Import") public class ReadExcelController {@ Autowired private EmployeeService;/***** @ Description: import an Excel file * @ return */@ RequestMapping ("ImportExcel") @ ResponseBody public boolean readExcel (@ RequestParam ("file") CommonsMultipartFile file) throws IOException {ImportExcelUtil importExcelUtil = new ImportExcelUtil (); String path = "E:/" + new Date (). getTim E () + file. getOriginalFilename (); File newFile = new File (path); file. transferTo (newFile); List <Map <String, String> list = importExcelUtil. readExcel (newFile); List <Map <String, Object> busiID = EmployeeService. quarryBusiId (); Map <Object, Object> map = new HashMap <> (); for (int j = 0; j <busiID. size (); j ++) {map. put (busiID. get (j ). get ("busi_id"), 1) ;}list <Employee> employeeList = new ArrayList <> (); for (int I = 0; I <list. size (); I ++) {Employee employee = new Employee (); if (list. get (I ). get ("cell13") = null | "". equals (employee. getBusiId () {continue;} if (map. get (list. get (I ). get ("cell13 "))! = Null) {continue;} employee. setBusiId (list. get (I ). get ("cell13"); employee. setEmpId (list. get (I ). get ("cell1"); employee. setEmpName (list. get (I ). get ("cell2"); employee. setClassName (list. get (I ). get ("cell4") + list. get (I ). get ("cell3"); employee. setDeptName (list. get (I ). get ("cell4"); employee. setTranType (list. get (I ). get ("cell6"); employee. setTranName (list. get (I ). get ("cell7"); employee. setPostClass (list. get (I ). get ("cell8"); employee. setPostName (list. get (I ). get ("cell9"); employeeList. add (employee) ;}for (int I = 0; I <employeeList. size (); I ++) {EmployeeService. insert (employeeList. get (I) ;}return true ;}}
That is to say, the original address of the file is bypassed, and the file is directly uploaded to a fixed address for calling. Of course, this is only a test method. Later, you can directly go to the server.
View comments