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

给你分享下最代码的下载代码的片段@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就是一条数据库记录,这条记录中保存有文件的信息,比如存储位置,创建时间,大小等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值