路径是/还是\的区别?下载路径downloadPath和本地路径localPath的区别是什么?如何生成下载路径?

本地路径是 D:\  ..\..\

地址栏中的路径 是/  正的斜杠

示例

上传到本地的绝对路径

D:\soft\apache-tomcat-7.0.41\webapps\AppInfoSystem\statics\uploadfiles\com.doodleapps.powdertoy-V1.1.2.apk

String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");

得到的是:D:\soft\apache-tomcat-7.0.41\webapps\AppInfoSystem\statics\uploadfiles

              一、  什么servletContext()就是D:\soft\apache-tomcat-7.0.41\webapps\AppInfoSystem\

                  补充:WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,

                             它代表当前web应用。

                二、getRealPath(“ xxx”) 表示 web项目绝对路径衔接上一下级子目录xxx

                          D:\soft\apache-tomcat-7.0.41\webapps\AppInfoSystem\statics\uploadfiles


                           

数据库存放--下载路径

/AppInfoSystem/statics/uploadfiles/com.doodleapps.powdertoy-V1.1.2.apk

用户访问下载路径的时候

localhost:8080/AppInfoSystem/statics/uploadfiles/com.doodleapps.powdertoy-V1.1.2.apk

如何实现?

调用request.getContextPath() ,获得:  /AppInfoSystem  (   /  +项目名  ) 

       downloadLink = request.getContextPath() + "/statics/uploadfiles/" + apkFileName;

实战

@RequestMapping(value="/addversionsave", method=RequestMethod.POST)
  public String addVersionSave(AppVersion appVersion, HttpSession session, 
		  HttpServletRequest request, 
		  @RequestParam(value="a_downloadLink", required=false) MultipartFile attach)
  {
    String downloadLink = null;// 下载路径
    String apkLocPath = null; // apk当地路径
    String apkFileName = null;// apk文件名
    if (!(attach.isEmpty())) {
    	//在statics下创建uploadfiles目录,后形成上传文件的本地保存绝对路径
    	
      String supPath = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
      //上传的文件存放的绝对父级目录
      this.logger.info("uploadFile path: " + supPath);
      //文件的原始名
      String oldFileName = attach.getOriginalFilename();
      //获得原始名的后缀
      String suffix = FilenameUtils.getExtension(oldFileName);
      if (suffix.equalsIgnoreCase("apk")) {
        String apkName = null;//设置为null ,然后重新命名成唯一名字
       
       //获得app_inf表格中的APKName字段值,因为这个是唯一标识的(业务设计规定的)
        apkName = this.appInfoService.getAppInfo(appVersion.getAppId(), null).getAPKName();
       
        //url路径传参:error="error1":APK信息(APK名称)不完整!
        if ((apkName == null) || ("".equals(apkName)))
          return "redirect:/dev/flatform/app/appversionadd?id=" + appVersion.getAppId() + 
            "&error=error1";
        //拼接:com.doodleapps.test1  -   HV1.1.4.apk
        apkFileName = apkName + "-" + appVersion.getVersionNo() + ".apk";
        //将上级绝对本地路径supPath(/statics/uploadfiles)和apkFlieName拼接成一个文件路径,然后创建文件
        File targetFile = new File(supPath, apkFileName);
        if (!(targetFile.exists()))//目标文件名不存在的话,新建一个
        	//创建path的下一级目录:apkFileName
          targetFile.mkdirs();
        try
        {
        	//文件上传,本质就是将attach中绑定的文件数据,复制到新建的文件中
          attach.transferTo(targetFile);
        }
        catch (Exception e) {
          e.printStackTrace();
          //上传是出现异常的话,那么返回页面提示语:error2,上传失败
          return "redirect:/dev/flatform/app/appversionadd?id=" + appVersion.getAppId() + 
            "&error=error2";
        }
        // apk文件的服务器存储路径
        apkLocPath = supPath + File.separator + apkFileName;
        
        
        //下载链接,示例  /AppInfoSystem/statics/uploadfiles/com.doodleapps.powdertoy-V1.1.31.apk
        //request.getContextPath() 得到的就是项目名路径 /AppInfoSystem
        downloadLink = request.getContextPath() + "/statics/uploadfiles/" + apkFileName;

      } else {
    	  //若后缀名不是apk的话,页面返回 文件格式错误
        return "redirect:/dev/flatform/app/appversionadd?id=" + appVersion.getAppId() + 
          "&error=error3";
      }
    }
    //将数据存储到数据库中,使用setter()
    //创建人Id
    appVersion.setCreatedBy(((DevUser)session.getAttribute("devUserSession")).getId());
    //创建时间
    appVersion.setCreationDate(new Date());
    //下载路径(一定是绝对路径)如:/AppInfoSystem/statics/uploadfiles/com.kleientertainment.doNotStarvePocket-V1.1.2.apk
    appVersion.setDownloadLink(downloadLink);
/*    apk文件的服务器存储路径 如:D:\soft\apache-tomcat-7.0.41\webapps\
 *            AppInfoSystem/statics/uploadfiles/com.kleientertainment.doNotStarvePocket-V1.1.2.apk  
*/    appVersion.setApkLocPath(apkLocPath);
    //apk文件名,如:com.kleientertainment.doNotStarvePocket-V1.1.2.apk
    appVersion.setApkFileName(apkFileName);
    try {
    	//将appVersion对象添加到数据库中
      if (this.appVersionService.appsysadd(appVersion)){
        return "redirect:/dev/flatform/app/list";
      }
    }
    catch (Exception e){
      e.printStackTrace();
      return "redirect:/dev/flatform/app/appversionadd?id=" + appVersion.getAppId(); 
      }
      return "redirect:/dev/flatform/app/appversionadd?id=" + appVersion.getAppId();
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值