解决谷歌浏览器文件上传fakepath问题

本文介绍了一种解决浏览器在文件上传过程中显示fakepath路径的方法。通过前后端配合,将文件上传至指定位置,并利用时间戳确保文件名唯一性。同时展示了如何读取Excel文件并插入数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    前段时间在实现一个文件上传功能时发现谷歌等主流浏览器会出于安全考虑将上传文件的地址换为fakepath。比如c盘的某个文件,无论其原先处于哪个文件之下。在上传之后其会自动变为C\fakepath\文件名字。

  解决方案如下:

前台form表单提交文件

<form name="Form2" action="/Import/ImportExcel" method="post"  enctype="multipart/form-data">

                        <input type="file" name="file">
                        <input type="submit" value="upload"/>
                    </form>

后台controller层获取文件并将其上传至E盘,文件名转换成时间戳以便区分

@Controller
@RequestMapping("Import")

public class ReadExcelController {
    @Autowired
    private EmployeeService EmployeeService;
    /**
     *
     * @Description: 导入Excel文件
     * @return
     */
    @RequestMapping("ImportExcel" )
    @ResponseBody
    public boolean readExcel(@RequestParam("file") CommonsMultipartFile file) throws IOException {
        ImportExcelUtil importExcelUtil = new ImportExcelUtil();
        String path="E:/"+new Date().getTime()+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;


    }
}

也就是说绕过文件的原地址,直接将文件上传到一个固定的地址以便调用。当然这只是测试方法,后期可以直接上次到服务器。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值