Solved the problem of fakepath uploading files in Google browsers,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.