/** * <p> * Description:[存储文件函数] * </p> *前端form表单提交文件,并且action到/api..../savefile(具体根据你的api路径), * */ @RequestMapping(value = "/savefile", method = RequestMethod.POST) @ResponseBody public RestMsg<Object> savefile(HttpServletRequest request) { RestMsg<Object> rm = new RestMsg<Object>(); try { //license_file_path的路径在message.propeties中配置,比如license_file_path = /data
log.error(e.getLocalizedMessage()); log.error("上传license失败",e); rm.errorMsg("上传license失败!"); } return rm; } /** * <p> * Description:[存储文件函数] * </p> * * @param storefilepath:文件路径,相对路径 * @return 存储的文件名,用逗号隔开 */ private String StoreFile(HttpServletRequest request,String storefilepath) { StringBuffer filepaths = new StringBuffer(); //所有存储的文件名 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; List<MultipartFile> fileList = multipartRequest.getFiles("file"); //获得前端表单中提交的所有文件列表 if(fileList == null || fileList.size() == 0){ return ""; } String path = request.getSession().getServletContext().getRealPath("")+storefilepath; //获取文件夹路径 File file = new File(path); //每个登录名都有一个文件夹 if(!file.exists()){ file.mkdir();//如果文件夹不存在,先生成文件夹 } for (MultipartFile mf : fileList) {//遍历文件列表 try { if(!mf.isEmpty()){ File filetemp = new File(path,mf.getOriginalFilename()); if(!filetemp.exists()){//如果文件不存在,生成文件 filetemp.createNewFile(); } filepaths.append(filetemp.getName()+","); //加入存储的文件名 InputStream in = mf.getInputStream(); OutputStream out = new FileOutputStream(filetemp); int length = 0 ; byte [] buf = new byte[1024] ; while( (length = in.read(buf) ) != -1) { //在 buf 数组中 取出数据 写到 (输出流)磁盘上 out.write(buf, 0, length); } in.close(); out.close(); } } catch (Exception e) { e.printStackTrace(); } } filepaths.delete(filepaths.length()-1,filepaths.length()); return filepaths.toString(); }String storefilepath = StoreFile(request, Messages.getString("license_file_path")); //存储上传文件,并返回文件名,用逗号隔开 rm.successMsg("上传license成功!"); } catch (Exception e) {