下载文件及下载路径获取(本地及服务器路径) |
您所在的位置:网站首页 › js怎样获取浏览器的下载路径位置 › 下载文件及下载路径获取(本地及服务器路径) |
应用场景
用户需要上传如execel等文件,我们下载文件到本地服务器并解析 下载地址 用户从页面上传文件,后台下载文件到指定文件夹 服务器是下载到项目路径下,本地springBoot是在resources下有upload文件夹,一般符合要求 // 准备文件夹,获取项目中upload文件夹的路径 String parentDir = request.getServletContext().getRealPath("upload"); // request.getSession().getServletContext().getRealPath(""); // request.getRealPath("");这个需要参数中传递request,这里接收文件的代码写到controller中 @RequestMapping("upload.do") @ResponseBody public Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception { boolean isEmpty = file.isEmpty(); if (isEmpty) { throw new RuntimeException("上传失败!上传的文件为空!"); } // 检查文件大小 long fileSize = file.getSize(); if (fileSize > 1 * 1024 * 1024) { throw new RuntimeException("上传失败!上传的文件大小超出了限制!"); } // 检查文件MIME类型 String contentType = file.getContentType(); List types = new ArrayList(); types.add("application/xls"); types.add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); if (!types.contains(contentType)) { throw new RuntimeException("上传失败!不允许上传此类型的文件!"); } // 准备文件夹,获取项目中upload文件夹的路径 String parentDir = request.getServletContext().getRealPath("upload"); File parent = new File(parentDir); if (!parent.exists()) { parent.mkdirs(); } // 获取原始文件名 String originalFilename = file.getOriginalFilename(); // 执行保存文件,源文件名保存 File dest = new File(parent, originalFilename); file.transferTo(dest); }这个因为我接收的上传文件是有日期的,一般不会重复,为了方便以后查看所以用的源文件名保存。 如果是很多客户使用,建议生成不同文件名UUID.randomUUID().toString(),有查看原名的需求可以把原名和生成文件名持久化到数据库 String filename = UUID.randomUUID().toString(); String suffix = ""; int beginIndex = originalFilename.lastIndexOf("."); if (beginIndex != -1) { suffix = originalFilename.substring(beginIndex); }前端代码: 请选择要上传的文件: 给下载链接,下载文件到自己服务器 通过这个命令获取属性列表,这个任意地方都能使用 属性列表System.getProperties().list(System.out)Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串。 ` System.getProperties().list(System.out);先看下idea运行结果,太长了只显示一部分 -- listing properties -- java.runtime.name=Java(TM) SE Runtime Environment sun.boot.library.path=C:\Program Files\Java\jdk1.8.0_181\jr... path.separator=; user.dir=E:\workspace\basic\basic file.separator=\里面的属性可以通过下面代码获取System.getProperty("user.dir") user.dir用的比较多,但是放到服务器上这个地址指向tomcat服务器的bin目录`,再在服务器上打印属性得到下面内容,只展示一部分: catalina.base=/home/corpwx/apache-tomcat-8.5.50 java.runtime.version=1.8.0_77-b03 user.name=corpwx java.version=1.8.0_77 file.separator=/ path.separator=: user.home=/home/corpwx catalina.home=/home/corpwx/apache-tomcat-8.5.50 user.dir=/home/corpwx/apache-tomcat-8.5.50/bin这里没有能获取到自己代码运行的位置,用user.dir有点不对劲,所以就用catalina.home先定位tomcat目录位置,再自己写要保存的位置了 file.separator保存了不同系统下的文件分隔符 @Log public class FileDownload { public static int downloadImage(String fileUrl ) { long l = 0L; int ec = 0; if (fileUrl != null) { //下载时文件名称后缀 String fileName = fileUrl.substring(fileUrl.lastIndexOf("=")+1); try { String dataStr = new SimpleDateFormat("yyyyMMdd").format(new Date()); //String uuidName = UUID.randomUUID().toString(); // 准备文件夹,获取项目中upload文件夹的路径System.getProperty("user.dir") String parentDir = System.getProperty("catalina.home")+File.separator+"webapps"+File.separator+"basic"+File.separator+"upload"+File.separator+dataStr; log.info("下载文件链接:"+fileUrl+"位置:"+parentDir + File.separator + fileName); l=System.currentTimeMillis(); HttpUtil.downloadFile(fileUrl, parentDir + File.separator + fileName); log.info("下载文件"+fileName+"共使用:"+(System.currentTimeMillis()-l)+"ms"); } catch (Exception e) { log.info("下载文件"+fileName+"失败:"); ec = 10001; e.printStackTrace(); } } return ec; } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |