已注销用户的gravatar头像
已注销用户 2017-09-11 22:56:34

java Web文件下载如何做到不暴露真实的下载地址?

近期,打算做个下载功能,可是下载这块有的资源打算是付费的。也就是说,要下载这个资源必须要付费才可以下载的,那么问题来了,我怎么做才可以避免暴露文件资源的真实下载地址。

 

有任何想法的,或是伪代码的都可以回答,高牛币回报!

所有回答列表(1)
最代码官方的gravatar头像
最代码官方  LV167 2017年9月12日

给你分享下最代码的下载代码的片段

@RequestMapping(value = {"/code/{id}/download"}, method = {RequestMethod.GET})
    public String download(@PathVariable("id") Long id,
                           @RequestHeader("User-Agent") String userAgent,
                           HttpServletRequest request, HttpServletResponse response,
                           HttpSession session, final RedirectAttributes redirectAttributes)
            throws IOException {
        User user = (User) session
                .getAttribute(GlobalConstants.SESSION_LOGIN_USER_NAME);
        Answer answer = answerService.findOneById(id);
        if (answer == null) {
            return "redirect:/";
        }
        Project project = (Project) answer.getTarget();
        ModuleDesc moduleDesc = ModuleConstants.PROJECT_TYPE_DESC_MAP
                .get(project.getType());
        String url = moduleDesc.getUrl();
        if (user == null) {
            redirectAttributes.addFlashAttribute("download_error", "请登录后再下载代码");
            return "redirect:/" + url + "/" + project.getUuid() + ".htm";
        }

        if (user == null) {
            redirectAttributes.addFlashAttribute("download_error", "请登录后再下载代码");
            return "redirect:/" + url + "/" + project.getUuid() + ".htm";
        }

        String _extendJson = answer.getExtendJson();
        JSONObject extendJson = JSONObject.fromObject(_extendJson);
        String fileName = extendJson
                .getString(ModuleConstants.ANSWER_EXTEND_JSON_FILE_NAME);
        String fileId = extendJson
                .getString(ModuleConstants.ANSWER_EXTEND_JSON_FILE_ID);
        int indexStart = fileName.lastIndexOf(".") + 1;
        String suffix = fileName.substring(indexStart).toLowerCase();
        String dir = fileId.substring(0, 3) + "/";
        File codeFile = new File(codePath + dir + fileId + "." + suffix);
        if (suffix.equals(ModuleConstants.CODE_TYPE_JAVA)) {
            codeFile = new File(codePath + dir + fileId + "/" + fileId + "."
                    + suffix);
        }

        // 下载成功后再扣牛币
        int type = ModuleConstants.EVENT_TYPE_RULE_ANSWER_GET;
        String lock = answerService.getUserSourceTypeLock(user.getId(), answer.getId(), type);
        synchronized (lock) {
            MethodRetrun methodRetrun = answerService.synchronizedDownloadAnswer(user, answer);
            if (!methodRetrun.isSuccess()) {
                String msg = methodRetrun.getMsg();
                String _url = methodRetrun.getUrl();
                redirectAttributes.addFlashAttribute("download_error", msg);
                return "redirect:" + _url;
            }
        }

        BufferedInputStream in = null;
        OutputStream out = null;
        try {
            if (suffix.equals(ModuleConstants.CODE_TYPE_ZIP)) {
                response.setContentType("application/x-zip-compressed");
            } else if (suffix.equals(ModuleConstants.CODE_TYPE_RAR)) {
                response.setContentType("application/octet-stream");
            } else if (suffix.equals(ModuleConstants.CODE_TYPE_JAVA)) {
                response.setContentType("text/plain;charset=UTF-8");
            }
            long fileLength = codeFile.length();
            String length = String.valueOf(fileLength);
            response.setHeader("Content_Length", length);
            if (userAgent.toLowerCase().indexOf("firefox") != -1) {
                fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf-8");
            }
            response.setHeader("Content-disposition", "attachment; filename="
                    + fileName);
            in = new BufferedInputStream(new FileInputStream(codeFile));
            out = response.getOutputStream();
            IOUtils.copy(in, out);
        } catch (Exception e) {
            logger.error("Fail to download code:" + answer + ",Exception:"
                    + e.getMessage());
            redirectAttributes.addFlashAttribute("download_error", "下载出错");
            return "redirect:/" + url + "/" + project.getUuid() + ".htm";
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

请求地址是诸如

http://www.zuidaima.com/code/11620/download.htm

11620就是一条数据库记录,这条记录中保存有文件的信息,比如存储位置,创建时间,大小等等。

评论(1) 最佳答案
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友