1. Java 关于判断文件是否存在:
public int saveCSV(String username) { String savepath = "D:/test"; String savePathAddUser = savepath + "/" + username; File savePathFile = new File(savePathAddUser); try { // ======================check the file path exists.================= if (!savePathFile.exists() && !savePathFile.isDirectory()) { savePathFile.mkdirs(); } } catch (IOException e) { // TODO: handle eception e.printStackTrace(); } return 1; }
注意,这里的File的还一个方法是'mkdir',这个方法比起上面的那个方法,只能创建文件本身体,不能创建文件的父目录。“mkdirs” 如果没有父目录,可以创建父目录。
2. 利用MultipartFile 上传文件出现中文乱码:
public int saveCSV(MultipartFile multipartFile, String username) { String fileName = multipartFile.getOriginalFilename(); try { String fileNameUTF8 = new String(fileName.getBytes(), "UTF-8"); } catch (IOException e) { // TODO: handle eception e.printStackTrace(); } return 1; }
获取的文件名会出现乱码,所以要进行转码。
3. js 获取 ModelAndView 中的值:
var getfrommodel = '${vaueinmodel}';