方法一:
java:
@RequestMapping("/testFile") public String fileUpload(MultipartFile multipartFile) { String path= "d:\upload\test\"; // 原名 String[] type = multipartFile.getOriginalFilename().split("\.");//为了获取图片类型 如:jpg String filename=new Date().getTime()+"."+multipartFile.getOriginalFilename().split("\.")[type.length-1];//改文件名 为:日期.xxx //路径 File file = new File(path); //不存在创建文件夹 if(!file.exists() && !file.isDirectory()){ file.mkdirs(); } File myfile = new File(path+filename); try { multipartFile.transferTo(myfile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return filename; }
自测:
文件会保存在d盘upload下test下。
方法二:
public static final String FileUpload(MultipartFile multipartFile,String path) throws IOException { String[] type = multipartFile.getOriginalFilename().split("\.");//为了获取图片类型 如:jpg String filename=new Date().getTime()+"."+multipartFile.getOriginalFilename().split("\.")[type.length-1];//改文件名 为:日期.xxx //路径 File file = new File(path); //不存在创建文件夹 if(!file.exists() && !file.isDirectory()){ file.mkdirs(); } DataOutputStream out = new DataOutputStream(new FileOutputStream(path+filename));//存放文件的绝对路径 InputStream is = null;// 附件输入流 try { is = multipartFile.getInputStream(); byte[] b=new byte[is.available()];//available 先得知数据流里有多少个字节可以读取 is.read(b); out.write(b); } catch (IOException exception) { exception.printStackTrace(); } finally { if (is != null) { is.close(); } if (out != null) { out.close(); } } return filename; }
方法三:HttpServletRequest
简单看一下前台:
upload.render({ elem: '#test3' ,url: '后台路径' ,accept: 'file' //普通文件 ,multiple: false // 禁止批量上传 ,field: "filePath" ,done: function(res){ layer.msg('上传成功'); //console.log(res); } });
后台:
public void upLoadFile( HttpServletRequest request)throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // 获取上传的文件 MultipartFile multiFile = multipartRequest.getFile("filePath"); String filename=multiFile.getOriginalFilename();//文件名 String classpath= this.getClass().getResource("/").getPath(); String webappRoot = classpath.replaceAll("WEB-INF/classes/", ""); path = classpath+File.separator+"assets"+File.separator+"pdf"+File.separator;// 拼接路径 这里是存放到tomcat 项目路径下 web-inf 下 //路径 File file = new File(path); if(!file.exists() && !file.isDirectory()){ file.mkdirs(); } DataOutputStream out = new DataOutputStream(new FileOutputStream(path+filename));//存放文件的绝对路径 InputStream is = null;// 附件输入流 try { is = multiFile.getInputStream(); byte[] b=new byte[is.available()];//available 先得知数据流里有多少个字节可以读取 is.read(b); out.write(b); } catch (IOException exception) { exception.printStackTrace(); } finally { if (is != null) { is.close(); } if (out != null) { out.close(); } } }
删文件
String classpath = this.getClass().getResource("/").getPath();// String webappRoot = classpath.replaceAll("WEB-INF/classes/", ""); String path = classpath+File.separator+"assets"+File.separator+"pdf"+File.separator; String newPath=path.concat(url); File file = new File(newPath); if (file.exists()) { //删除 file.delete(); }
@