 所有回答列表(1)
所有回答列表(1)
				
            最代码官方     LV168
            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)
            
                最佳答案
        
    - 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?
 相关问答
相关问答
				- 完 java web如何实现中英文国际化?
- 完 java web的开发过程中,用到多线程的场景有哪些?
- 等 java web开发用什么语言的文件写页面啊,除了jsp格式的文件和html格式的文件?
- 等 如何学习java web开发?
- 等 java web运行程序后台为什么老报这个错误?Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them
- 等 域名访问项目session为空,通过ip访问正常
- 完 java web现在流行的OA框架使用哪个?
- 等 有无比较好的java web框架啦,例如jeesite、jty这样的开源项目源码?
- 等 java web项目启动欢迎页为什么没有样式返回404?
- 等 java web在启动项目是报错 Error creating bean with name 'shiroFilter' defined in file
- 完 eclipse导入java web项目后报错java.lang.ClassNotFoundException?
- 等 javaWeb开源的聊天工具有那些?
 最近浏览
最近浏览
				
                mechanical     LV2
                2020年5月28日
            
            
        
                he1604498391     LV2
                2019年11月24日
            
            
        
                youwuzuichen     LV11
                2019年8月6日
            
            
        
                人间蒸发     LV23
                2019年7月23日
            
            
        
                2224947710     LV17
                2019年5月29日
            
            
        
                小凡的耿     LV2
                2019年4月12日
            
            
        
                故事_sun     LV26
                2019年3月22日
            
            
        
                兰心forward向远     LV1
                2019年3月6日
            
            
        
                2074199629     LV2
                2019年1月2日
            
            
        
                yangguangzhidi     LV3
                2018年9月4日
            
            
        




 
                 
                 
                