本文环境:windows10、layui2.5.6,本文适用于所有品牌的电脑。
思路:
首先获取原始文件名你,并截取文件名后缀;然后重新命名新文件名;最后判断文件路径是否存在该文件,如果存在则将文件写入即可。
代码示例:
@PostMapping("/json/up") public String uploadFujian2( MultipartFile file) { System.out.println(file.getSize()); //readIoStringToFile(file.toString(),"D://test/test.doc"); if (file != null) { try { //String fileRealName = file.getOriginalFilename();//获得原始文件名; //int pointIndex = fileRealName.lastIndexOf(".");//点号的位置 //String fileSuffix = fileRealName.substring(pointIndex);//截取文件后缀 //String fileNewName = DateUtils.getNowTimeForUpload();//新文件名,时间戳形式yyyyMMddHHmmssSSS //String saveFileName = fileNewName.concat(fileSuffix);//新文件完整名(含后缀) String filePath = "D:\\FileAll" ; File path = new File(filePath); //判断文件路径下的文件夹是否存在,不存在则创建 if (!path.exists()) { path.mkdirs(); } File savedFile = new File(filePath); savedFile = new File(filePath,"test.doc"); // 使用下面的jar包 FileUtils.copyInputStreamToFile(file.getInputStream(),savedFile); boolean isCreateSuccess = savedFile.createNewFile(); // 是否创建文件成功 if(isCreateSuccess){ //将文件写入 //第一种 //file.transferTo(savedFile); //第二种 } } catch (Exception e) { e.printStackTrace(); } }else { System.out.println("文件是空的"); } //IOUtils.read(file) //File dest = new File(""); return "ok"; }
以上就是layui上传文件data后台怎么获取的详细内容,更多请关注其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。